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

Commit c64574a

Browse files
authored
chore: return error on pulling an existing model (#975)
1 parent e139355 commit c64574a

File tree

5 files changed

+51
-44
lines changed

5 files changed

+51
-44
lines changed

cortex-js/src/infrastructure/controllers/models.controller.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ import {
99
UseInterceptors,
1010
BadRequestException,
1111
Patch,
12+
Res,
13+
HttpStatus,
1214
} from '@nestjs/common';
15+
import { Response } from 'express';
1316
import { ModelsUsecases } from '@/usecases/models/models.usecases';
1417
import { CreateModelDto } from '@/infrastructure/dtos/models/create-model.dto';
1518
import { UpdateModelDto } from '@/infrastructure/dtos/models/update-model.dto';
@@ -137,6 +140,7 @@ export class ModelsController {
137140
})
138141
@Post(':modelId(*)/pull')
139142
pullModel(
143+
@Res() res: Response,
140144
@Param('modelId') modelId: string,
141145
@Body()
142146
body?: {
@@ -145,7 +149,7 @@ export class ModelsController {
145149
},
146150
) {
147151
const { fileName, persistedModelId } = body || {};
148-
this.modelsUsecases
152+
return this.modelsUsecases
149153
.pullModel(
150154
modelId,
151155
false,
@@ -170,10 +174,17 @@ export class ModelsController {
170174
name: EventName.DOWNLOAD_MODEL,
171175
modelId,
172176
}),
177+
)
178+
.then(() =>
179+
res.status(HttpStatus.OK).json({
180+
message: 'Download model started successfully.',
181+
}),
182+
)
183+
.catch((e) =>
184+
res
185+
.status(HttpStatus.CONFLICT)
186+
.json({ error: { message: e.message ?? e }, code: 409 }),
173187
);
174-
return {
175-
message: 'Download model started successfully.',
176-
};
177188
}
178189

179190
@HttpCode(200)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ export class DownloadManagerService {
117117
this.eventEmitter.emit('download.event', this.allDownloadStates);
118118
};
119119
if (!inSequence) {
120-
return Promise.all(
120+
Promise.all(
121121
Object.keys(urlToDestination).map((url) => {
122122
const destination = urlToDestination[url];
123123
return this.downloadFile(downloadId, url, destination);
@@ -141,7 +141,7 @@ export class DownloadManagerService {
141141
const controller = new AbortController();
142142
// adding to abort controllers
143143
this.abortControllers[downloadId][destination] = controller;
144-
return new Promise<void>(async (resolve, reject) => {
144+
return new Promise<void>(async (resolve) => {
145145
const response = await firstValueFrom(
146146
this.httpService.get(url, {
147147
responseType: 'stream',

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ export class CortexUsecases implements BeforeApplicationShutdown {
129129
* @param port
130130
* @returns
131131
*/
132-
healthCheck(host: string, port: number): Promise<boolean> {
132+
async healthCheck(host: string, port: number): Promise<boolean> {
133133
return fetch(CORTEX_CPP_HEALTH_Z_URL(host, port))
134134
.then((res) => {
135135
if (res.ok) {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { StartModelSuccessDto } from '@/infrastructure/dtos/models/start-model-s
1010
import { ExtensionRepository } from '@/domain/repositories/extension.interface';
1111
import { EngineExtension } from '@/domain/abstracts/engine.abstract';
1212
import { isLocalModel, normalizeModelId } from '@/utils/normalize-model-id';
13-
import { fileManagerService, FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service';
13+
import { fileManagerService } from '@/infrastructure/services/file-manager/file-manager.service';
1414
import { AxiosError } from 'axios';
1515
import { TelemetryUsecases } from '../telemetry/telemetry.usecases';
1616
import { TelemetrySource } from '@/domain/telemetry/telemetry.interface';
@@ -507,6 +507,7 @@ export class ModelsUsecases {
507507
engine: Engines.llamaCPP,
508508
};
509509
if (!(await this.findOne(modelId))) await this.create(model);
510+
else throw 'Model already exists.';
510511
}
511512

512513
/**
@@ -531,7 +532,7 @@ export class ModelsUsecases {
531532
* Check whether the model is running in the Cortex C++ server
532533
*/
533534
async isModelRunning(modelId: string): Promise<boolean> {
534-
const model = await this.getModelOrThrow(modelId).catch((e) => undefined);
535+
const model = await this.getModelOrThrow(modelId).catch(() => undefined);
535536

536537
if (!model) return false;
537538

cortex-js/src/utils/log.ts

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { fileManagerService } from "@/infrastructure/services/file-manager/file-manager.service";
2-
import { existsSync, stat, unlink, writeFileSync } from "fs";
1+
import { fileManagerService } from '@/infrastructure/services/file-manager/file-manager.service';
2+
import { existsSync, stat, unlink, writeFileSync } from 'fs';
33

4-
const logCleaningInterval: number = 120000
4+
const logCleaningInterval: number = 120000;
55
let timeout: NodeJS.Timeout | undefined;
66

77
export async function cleanLogs(
@@ -10,46 +10,41 @@ export async function cleanLogs(
1010
): Promise<void> {
1111
// clear existing timeout
1212
// in case we rerun it with different values
13-
if (timeout) clearTimeout(timeout)
14-
timeout = undefined
13+
if (timeout) clearTimeout(timeout);
14+
timeout = undefined;
1515

16+
console.log('Validating app logs. Next attempt in ', logCleaningInterval);
1617

17-
console.log(
18-
'Validating app logs. Next attempt in ',
19-
logCleaningInterval
20-
)
21-
22-
const size = maxFileSizeBytes ?? 1 * 1024 * 1024 // 1 MB
23-
const days = daysToKeep ?? 7 // 7 days
24-
const filePath = await fileManagerService.getLogPath()
18+
const size = maxFileSizeBytes ?? 1 * 1024 * 1024; // 1 MB
19+
const days = daysToKeep ?? 7; // 7 days
20+
const filePath = await fileManagerService.getLogPath();
2521
// Perform log cleaning
26-
const currentDate = new Date()
22+
const currentDate = new Date();
2723
if (existsSync(filePath))
28-
stat(filePath, (err, stats) => {
29-
if (err) {
30-
console.error('Error getting file stats:', err)
31-
return
32-
}
24+
stat(filePath, (err, stats) => {
25+
if (err) {
26+
console.error('Error getting file stats:', err);
27+
return;
28+
}
3329

34-
// Check size
35-
if (stats.size > size) {
36-
writeFileSync(filePath, '', 'utf8')
37-
} else {
38-
// Check age
39-
const creationDate = new Date(stats.ctime)
40-
const daysDifference = Math.floor(
41-
(currentDate.getTime() - creationDate.getTime()) /
42-
(1000 * 3600 * 24)
43-
)
44-
if (daysDifference > days) {
45-
writeFileSync(filePath, '', 'utf8')
30+
// Check size
31+
if (stats.size > size) {
32+
writeFileSync(filePath, '', 'utf8');
33+
} else {
34+
// Check age
35+
const creationDate = new Date(stats.ctime);
36+
const daysDifference = Math.floor(
37+
(currentDate.getTime() - creationDate.getTime()) / (1000 * 3600 * 24),
38+
);
39+
if (daysDifference > days) {
40+
writeFileSync(filePath, '', 'utf8');
41+
}
4642
}
47-
}
48-
})
43+
});
4944

5045
// Schedule the next execution with doubled delays
5146
timeout = setTimeout(
5247
() => this.cleanLogs(maxFileSizeBytes, daysToKeep),
53-
logCleaningInterval
54-
)
48+
logCleaningInterval,
49+
);
5550
}

0 commit comments

Comments
 (0)