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

Commit 44efcfe

Browse files
authored
fix: missing utils reference (#978)
1 parent c046ed8 commit 44efcfe

File tree

5 files changed

+53
-59
lines changed

5 files changed

+53
-59
lines changed

cortex-js/src/app.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { AppModule } from './app.module';
44
import { fileManagerService } from './infrastructure/services/file-manager/file-manager.service';
55
import { ValidationPipe } from '@nestjs/common';
66
import { TelemetryUsecases } from './usecases/telemetry/telemetry.usecases';
7+
import { cleanLogs } from './utils/logs';
78
export const getApp = async (host?: string, port?: number) => {
89
const app = await NestFactory.create(AppModule, {
910
snapshot: true,
@@ -25,7 +26,8 @@ export const getApp = async (host?: string, port?: number) => {
2526
enableDebugMessages: true,
2627
}),
2728
);
28-
29+
30+
cleanLogs();
2931
const config = new DocumentBuilder()
3032
.setTitle('Cortex API')
3133
.setDescription(

cortex-js/src/index.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
import { getApp } from './app';
88
import chalk from 'chalk';
99
import { CortexUsecases } from './usecases/cortex/cortex.usecases';
10-
import { cleanLogs } from './utils/log';
1110

1211
/**
1312
* Start the API server
@@ -19,8 +18,6 @@ export async function start(host?: string, port?: number) {
1918
const sPort = port || process.env.CORTEX_JS_PORT || defaultCortexJsPort;
2019

2120
try {
22-
// Clean log periodically
23-
cleanLogs();
2421
await app.listen(sPort, sHost);
2522
const cortexUsecases = await app.resolve(CortexUsecases);
2623
await cortexUsecases.startCortex();

cortex-js/src/main.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
} from '@/infrastructure/constants/cortex';
55
import { getApp } from './app';
66
import chalk from 'chalk';
7-
import { cleanLogs } from './utils/log';
87

98
async function bootstrap() {
109
const app = await getApp();
@@ -13,8 +12,6 @@ async function bootstrap() {
1312
const port = process.env.CORTEX_JS_PORT || defaultCortexJsPort;
1413

1514
try {
16-
// Clean logs periodically
17-
cleanLogs();
1815
await app.listen(port, host);
1916
console.log(chalk.blue(`Started server at http://${host}:${port}`));
2017
console.log(

cortex-js/src/utils/log.ts

Lines changed: 0 additions & 50 deletions
This file was deleted.

cortex-js/src/utils/logs.ts

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
import { createReadStream } from 'fs';
2-
import { join } from 'path';
1+
import { createReadStream, existsSync, stat, writeFileSync } from 'fs';
32
import { createInterface } from 'readline';
3+
import { fileManagerService } from '@/infrastructure/services/file-manager/file-manager.service';
4+
5+
const logCleaningInterval: number = 120000;
6+
let timeout: NodeJS.Timeout | undefined;
47

58
/**
69
* Print the last N lines of a file that contain the word 'ERROR'
@@ -30,3 +33,48 @@ export async function printLastErrorLines(
3033
errorLines.forEach((line) => console.log(line));
3134
console.log('...');
3235
}
36+
37+
export async function cleanLogs(
38+
maxFileSizeBytes?: number | undefined,
39+
daysToKeep?: number | undefined,
40+
): Promise<void> {
41+
// clear existing timeout
42+
// in case we rerun it with different values
43+
if (timeout) clearTimeout(timeout);
44+
timeout = undefined;
45+
46+
console.log('Validating app logs. Next attempt in ', logCleaningInterval);
47+
48+
const size = maxFileSizeBytes ?? 1 * 1024 * 1024; // 1 MB
49+
const days = daysToKeep ?? 7; // 7 days
50+
const filePath = await fileManagerService.getLogPath();
51+
// Perform log cleaning
52+
const currentDate = new Date();
53+
if (existsSync(filePath))
54+
stat(filePath, (err, stats) => {
55+
if (err) {
56+
console.error('Error getting file stats:', err);
57+
return;
58+
}
59+
60+
// Check size
61+
if (stats.size > size) {
62+
writeFileSync(filePath, '', 'utf8');
63+
} else {
64+
// Check age
65+
const creationDate = new Date(stats.ctime);
66+
const daysDifference = Math.floor(
67+
(currentDate.getTime() - creationDate.getTime()) / (1000 * 3600 * 24),
68+
);
69+
if (daysDifference > days) {
70+
writeFileSync(filePath, '', 'utf8');
71+
}
72+
}
73+
});
74+
75+
// Schedule the next execution with doubled delays
76+
timeout = setTimeout(
77+
() => this.cleanLogs(maxFileSizeBytes, daysToKeep),
78+
logCleaningInterval,
79+
);
80+
}

0 commit comments

Comments
 (0)