Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit e0746e7

Browse files
authored
chore: attempt to release running services on exit (#927)
1 parent 6bc7c84 commit e0746e7

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

cortex-js/src/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
CORTEX_CPP_PROCESS_DESTROY_URL,
23
CORTEX_JS_SYSTEM_URL,
34
defaultCortexJsHost,
45
defaultCortexJsPort,
@@ -36,5 +37,12 @@ export async function start(host?: string, port?: number) {
3637
export async function stop(host?: string, port?: number) {
3738
return fetch(CORTEX_JS_SYSTEM_URL(host, port), {
3839
method: 'DELETE',
39-
}).catch(() => {});
40+
})
41+
.catch(() => {})
42+
.then(() =>
43+
fetch(CORTEX_CPP_PROCESS_DESTROY_URL(host, port), {
44+
method: 'DELETE',
45+
}),
46+
)
47+
.catch(() => {});
4048
}

cortex-js/src/usecases/cortex/cortex.usecases.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { HttpStatus, Injectable } from '@nestjs/common';
1+
import {
2+
BeforeApplicationShutdown,
3+
HttpStatus,
4+
Injectable,
5+
} from '@nestjs/common';
26
import { ChildProcess, fork } from 'child_process';
37
import { delimiter, join } from 'path';
48
import { CortexOperationSuccessfullyDto } from '@/infrastructure/dtos/cortex/cortex-operation-successfully.dto';
@@ -16,7 +20,7 @@ import {
1620
import { openSync } from 'fs';
1721

1822
@Injectable()
19-
export class CortexUsecases {
23+
export class CortexUsecases implements BeforeApplicationShutdown {
2024
private cortexProcess: ChildProcess | undefined;
2125

2226
constructor(
@@ -44,6 +48,9 @@ export class CortexUsecases {
4448
const dataFolderPath = await this.fileManagerService.getDataFolderPath();
4549

4650
const writer = openSync(await this.fileManagerService.getLogPath(), 'a+');
51+
52+
// Attempt to stop the process if it's already running
53+
await this.stopCortex();
4754
// go up one level to get the binary folder, have to also work on windows
4855
this.cortexProcess = fork(join(__dirname, './../../utils/cortex-cpp'), [], {
4956
detached: true,
@@ -103,8 +110,6 @@ export class CortexUsecases {
103110
),
104111
),
105112
);
106-
} catch (err) {
107-
console.error(err.response.data);
108113
} finally {
109114
this.cortexProcess?.kill();
110115
return {
@@ -211,4 +216,9 @@ export class CortexUsecases {
211216
cortexCppPort: port,
212217
});
213218
}
219+
220+
async beforeApplicationShutdown(signal: string) {
221+
console.log(`Received ${signal}, performing pre-shutdown tasks.`);
222+
await this.stopCortex();
223+
}
214224
}

cortex-js/src/usecases/engines/engines.usecase.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,11 @@ export class EnginesUsecases {
120120
}
121121

122122
if (
123-
(engine === Engines.llamaCPP || engine === Engines.tensorrtLLM) &&
124-
options?.runMode === 'GPU' &&
125-
options?.gpuType === 'Nvidia' &&
126-
!options?.vulkan
123+
engine === Engines.tensorrtLLM ||
124+
(engine === Engines.llamaCPP &&
125+
options?.runMode === 'GPU' &&
126+
options?.gpuType === 'Nvidia' &&
127+
!options?.vulkan)
127128
)
128129
await this.installCudaToolkitDependency(
129130
engine === Engines.tensorrtLLM

0 commit comments

Comments
 (0)