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

Commit bdf8ecb

Browse files
feat: unload engine when init engine (#1003)
1 parent 59090bb commit bdf8ecb

File tree

7 files changed

+62
-10
lines changed

7 files changed

+62
-10
lines changed

cortex-js/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,24 +53,26 @@
5353
"@nestjs/sequelize": "^10.0.1",
5454
"@nestjs/swagger": "^7.3.1",
5555
"@terascope/fetch-github-release": "^0.8.8",
56+
"@types/node-os-utils": "^1.3.4",
5657
"axios": "^1.6.8",
5758
"class-transformer": "^0.5.1",
5859
"class-validator": "^0.14.1",
5960
"cli-progress": "^3.12.0",
60-
"cortex-cpp": "0.5.0-36",
61+
"cortex-cpp": "0.5.0-40",
6162
"cpu-instructions": "^0.0.11",
6263
"decompress": "^4.2.1",
6364
"hyllama": "^0.2.2",
6465
"js-yaml": "^4.1.0",
6566
"nest-commander": "^3.13.0",
67+
"node-os-utils": "^1.3.7",
6668
"ora": "5.4.1",
6769
"readline": "^1.3.0",
6870
"reflect-metadata": "^0.2.0",
6971
"rxjs": "^7.8.1",
7072
"sequelize": "^6.37.3",
7173
"sequelize-typescript": "^2.1.6",
7274
"sqlite3": "^5.1.7",
73-
"systeminformation": "^5.22.11",
75+
"systeminformation": "^5.23.4",
7476
"ulid": "^2.3.0",
7577
"uuid": "^9.0.1",
7678
"whatwg-url": "^14.0.0",

cortex-js/src/infrastructure/commanders/engines/engines-init.command.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,13 @@ export class EnginesInitCommand extends BaseCommand {
4141
const configs = await fileManagerService.getConfig();
4242
const host = configs.cortexCppHost;
4343
const port = configs.cortexCppPort;
44-
// Should stop cortex before installing engine
45-
const stopCortexSpinner = ora('Stopping cortex...').start();
44+
// Should unload engine before installing engine
45+
const unloadCortexEngineSpinner = ora('Unloading cortex...').start();
4646
if (await this.cortexUsecases.healthCheck(host, port)) {
47-
await this.cortexUsecases.stopCortex();
47+
await this.cortexUsecases.unloadCortexEngine(engine as Engines);
4848
}
49-
stopCortexSpinner.succeed('Cortex stopped');
49+
50+
unloadCortexEngineSpinner.succeed('Cortex unloaded');
5051
console.log(`Installing engine ${engine}...`);
5152

5253
await this.cortex.engines.init(engine, params);

cortex-js/src/infrastructure/commanders/ps.command.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { firstValueFrom } from 'rxjs';
1212
import { HttpService } from '@nestjs/axios';
1313
import { CORTEX_CPP_MODELS_URL } from '../constants/cortex';
1414
import { fileManagerService } from '../services/file-manager/file-manager.service';
15+
import { getMemoryInformation } from '@/utils/system-resource';
1516

1617
@SubCommand({
1718
name: 'ps',
@@ -45,9 +46,9 @@ export class PSCommand extends BaseCommand {
4546
totalVram,
4647
});
4748
}
48-
const memoryData = await systeminformation.mem();
49+
const { total, used } = await getMemoryInformation()
4950
const memoryUsage = (
50-
(memoryData.active / memoryData.total) *
51+
(used / total) *
5152
100
5253
).toFixed(2);
5354
const consumedTable = {

cortex-js/src/infrastructure/constants/cortex.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ export const CORTEX_CPP_PROCESS_DESTROY_URL = (
2626
port: number = defaultCortexCppPort,
2727
) => `http://${host}:${port}/processmanager/destroy`;
2828

29+
export const CORTEX_CPP_UNLOAD_ENGINE_URL = (
30+
host: string = defaultCortexCppHost,
31+
port: number = defaultCortexCppPort,
32+
) => `http://${host}:${port}/inferences/server/unloadengine`;
33+
2934
export const CORTEX_CPP_HEALTH_Z_URL = (
3035
host: string = defaultCortexCppHost,
3136
port: number = defaultCortexCppPort,

cortex-js/src/infrastructure/services/resources-manager/resources-manager.service.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1+
import osUtils from 'node-os-utils'
12
import {
23
ResourceStatus,
34
UsedMemInfo,
45
} from '@/domain/models/resource.interface';
6+
import { getMemoryInformation, MemoryInformation } from '@/utils/system-resource';
57
import { Injectable } from '@nestjs/common';
68
import systemInformation, { Systeminformation } from 'systeminformation';
79

810
@Injectable()
911
export class ResourcesManagerService {
1012
async getResourceStatuses(): Promise<ResourceStatus> {
11-
const promises = [systemInformation.currentLoad(), systemInformation.mem()];
13+
const promises = [systemInformation.currentLoad(), getMemoryInformation()];
1214
const results = await Promise.all(promises);
1315

1416
const cpuUsage = results[0] as Systeminformation.CurrentLoadData;
15-
const memory = results[1] as Systeminformation.MemData;
17+
const memory = results[1] as MemoryInformation
1618
const memInfo: UsedMemInfo = {
1719
total: memory.total,
1820
used: memory.used,

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ import { fileManagerService } from '@/infrastructure/services/file-manager/file-
1313
import {
1414
CORTEX_CPP_HEALTH_Z_URL,
1515
CORTEX_CPP_PROCESS_DESTROY_URL,
16+
CORTEX_CPP_UNLOAD_ENGINE_URL,
1617
CORTEX_JS_SYSTEM_URL,
1718
defaultCortexJsHost,
1819
defaultCortexJsPort,
1920
} from '@/infrastructure/constants/cortex';
2021
import { openSync } from 'fs';
22+
import { Engines } from '@/infrastructure/commanders/types/engine.interface';
2123

2224
@Injectable()
2325
export class CortexUsecases implements BeforeApplicationShutdown {
@@ -123,6 +125,29 @@ export class CortexUsecases implements BeforeApplicationShutdown {
123125
}
124126
}
125127

128+
/**
129+
* Unload the engine
130+
*/
131+
async unloadCortexEngine(engine: Engines): Promise<CortexOperationSuccessfullyDto> {
132+
const configs = await fileManagerService.getConfig();
133+
try {
134+
await firstValueFrom(
135+
this.httpService.post(
136+
CORTEX_CPP_UNLOAD_ENGINE_URL(
137+
configs.cortexCppHost,
138+
configs.cortexCppPort,
139+
),
140+
),
141+
);
142+
} finally {
143+
return {
144+
message: `${engine} unloaded successfully`,
145+
status: 'success',
146+
};
147+
}
148+
}
149+
150+
126151
/**
127152
* Check whether the Cortex CPP is healthy
128153
* @param host
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import osUtils from 'node-os-utils'
2+
3+
export type MemoryInformation = {
4+
total: osUtils.MemUsedInfo['totalMemMb'];
5+
used: osUtils.MemUsedInfo['usedMemMb'];
6+
free: osUtils.MemFreeInfo['freeMemMb']
7+
}
8+
9+
export const getMemoryInformation = async (): Promise<MemoryInformation> => {
10+
const [usedMemoryInfo, freeMemoryInfo] = await Promise.all([osUtils.mem.used(), osUtils.mem.free()])
11+
return {
12+
total: usedMemoryInfo.totalMemMb,
13+
used: usedMemoryInfo.usedMemMb,
14+
free: freeMemoryInfo.freeMemMb
15+
}
16+
}

0 commit comments

Comments
 (0)