Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: attempt to release running services on exit #927

Merged
merged 2 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion cortex-js/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
CORTEX_CPP_PROCESS_DESTROY_URL,
CORTEX_JS_SYSTEM_URL,
defaultCortexJsHost,
defaultCortexJsPort,
Expand Down Expand Up @@ -36,5 +37,12 @@ export async function start(host?: string, port?: number) {
export async function stop(host?: string, port?: number) {
return fetch(CORTEX_JS_SYSTEM_URL(host, port), {
method: 'DELETE',
}).catch(() => {});
})
.catch(() => {})
.then(() =>
fetch(CORTEX_CPP_PROCESS_DESTROY_URL(host, port), {
method: 'DELETE',
}),
)
.catch(() => {});
}
18 changes: 14 additions & 4 deletions cortex-js/src/usecases/cortex/cortex.usecases.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { HttpStatus, Injectable } from '@nestjs/common';
import {
BeforeApplicationShutdown,
HttpStatus,
Injectable,
} from '@nestjs/common';
import { ChildProcess, fork } from 'child_process';
import { delimiter, join } from 'path';
import { CortexOperationSuccessfullyDto } from '@/infrastructure/dtos/cortex/cortex-operation-successfully.dto';
Expand All @@ -16,7 +20,7 @@ import {
import { openSync } from 'fs';

@Injectable()
export class CortexUsecases {
export class CortexUsecases implements BeforeApplicationShutdown {
private cortexProcess: ChildProcess | undefined;

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

const writer = openSync(await this.fileManagerService.getLogPath(), 'a+');

// Attempt to stop the process if it's already running
await this.stopCortex();
// go up one level to get the binary folder, have to also work on windows
this.cortexProcess = fork(join(__dirname, './../../utils/cortex-cpp'), [], {
detached: true,
Expand Down Expand Up @@ -103,8 +110,6 @@ export class CortexUsecases {
),
),
);
} catch (err) {
console.error(err.response.data);
} finally {
this.cortexProcess?.kill();
return {
Expand Down Expand Up @@ -211,4 +216,9 @@ export class CortexUsecases {
cortexCppPort: port,
});
}

async beforeApplicationShutdown(signal: string) {
console.log(`Received ${signal}, performing pre-shutdown tasks.`);
await this.stopCortex();
}
}
9 changes: 5 additions & 4 deletions cortex-js/src/usecases/engines/engines.usecase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,11 @@ export class EnginesUsecases {
}

if (
(engine === Engines.llamaCPP || engine === Engines.tensorrtLLM) &&
options?.runMode === 'GPU' &&
options?.gpuType === 'Nvidia' &&
!options?.vulkan
engine === Engines.tensorrtLLM ||
(engine === Engines.llamaCPP &&
options?.runMode === 'GPU' &&
options?.gpuType === 'Nvidia' &&
!options?.vulkan)
)
await this.installCudaToolkitDependency(
engine === Engines.tensorrtLLM
Expand Down
Loading