From 6e47667333ce0f272cb9f8638889be640c06924b Mon Sep 17 00:00:00 2001 From: Louis Date: Mon, 5 Aug 2024 22:39:46 +0700 Subject: [PATCH] fix: missing utils reference --- cortex-js/src/app.ts | 4 ++- cortex-js/src/index.ts | 3 --- cortex-js/src/main.ts | 3 --- cortex-js/src/utils/log.ts | 50 ----------------------------------- cortex-js/src/utils/logs.ts | 52 +++++++++++++++++++++++++++++++++++-- 5 files changed, 53 insertions(+), 59 deletions(-) delete mode 100644 cortex-js/src/utils/log.ts diff --git a/cortex-js/src/app.ts b/cortex-js/src/app.ts index 58b87119b..c02edd428 100644 --- a/cortex-js/src/app.ts +++ b/cortex-js/src/app.ts @@ -4,6 +4,7 @@ import { AppModule } from './app.module'; import { fileManagerService } from './infrastructure/services/file-manager/file-manager.service'; import { ValidationPipe } from '@nestjs/common'; import { TelemetryUsecases } from './usecases/telemetry/telemetry.usecases'; +import { cleanLogs } from './utils/logs'; export const getApp = async (host?: string, port?: number) => { const app = await NestFactory.create(AppModule, { snapshot: true, @@ -25,7 +26,8 @@ export const getApp = async (host?: string, port?: number) => { enableDebugMessages: true, }), ); - + + cleanLogs(); const config = new DocumentBuilder() .setTitle('Cortex API') .setDescription( diff --git a/cortex-js/src/index.ts b/cortex-js/src/index.ts index 166a82941..601955bf4 100644 --- a/cortex-js/src/index.ts +++ b/cortex-js/src/index.ts @@ -7,7 +7,6 @@ import { import { getApp } from './app'; import chalk from 'chalk'; import { CortexUsecases } from './usecases/cortex/cortex.usecases'; -import { cleanLogs } from './utils/log'; /** * Start the API server @@ -19,8 +18,6 @@ export async function start(host?: string, port?: number) { const sPort = port || process.env.CORTEX_JS_PORT || defaultCortexJsPort; try { - // Clean log periodically - cleanLogs(); await app.listen(sPort, sHost); const cortexUsecases = await app.resolve(CortexUsecases); await cortexUsecases.startCortex(); diff --git a/cortex-js/src/main.ts b/cortex-js/src/main.ts index c87b40d31..1a43ca318 100644 --- a/cortex-js/src/main.ts +++ b/cortex-js/src/main.ts @@ -4,7 +4,6 @@ import { } from '@/infrastructure/constants/cortex'; import { getApp } from './app'; import chalk from 'chalk'; -import { cleanLogs } from './utils/log'; async function bootstrap() { const app = await getApp(); @@ -13,8 +12,6 @@ async function bootstrap() { const port = process.env.CORTEX_JS_PORT || defaultCortexJsPort; try { - // Clean logs periodically - cleanLogs(); await app.listen(port, host); console.log(chalk.blue(`Started server at http://${host}:${port}`)); console.log( diff --git a/cortex-js/src/utils/log.ts b/cortex-js/src/utils/log.ts deleted file mode 100644 index 4fd1579c9..000000000 --- a/cortex-js/src/utils/log.ts +++ /dev/null @@ -1,50 +0,0 @@ -import { fileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; -import { existsSync, stat, unlink, writeFileSync } from 'fs'; - -const logCleaningInterval: number = 120000; -let timeout: NodeJS.Timeout | undefined; - -export async function cleanLogs( - maxFileSizeBytes?: number | undefined, - daysToKeep?: number | undefined, -): Promise { - // clear existing timeout - // in case we rerun it with different values - if (timeout) clearTimeout(timeout); - timeout = undefined; - - console.log('Validating app logs. Next attempt in ', logCleaningInterval); - - const size = maxFileSizeBytes ?? 1 * 1024 * 1024; // 1 MB - const days = daysToKeep ?? 7; // 7 days - const filePath = await fileManagerService.getLogPath(); - // Perform log cleaning - const currentDate = new Date(); - if (existsSync(filePath)) - stat(filePath, (err, stats) => { - if (err) { - console.error('Error getting file stats:', err); - return; - } - - // Check size - if (stats.size > size) { - writeFileSync(filePath, '', 'utf8'); - } else { - // Check age - const creationDate = new Date(stats.ctime); - const daysDifference = Math.floor( - (currentDate.getTime() - creationDate.getTime()) / (1000 * 3600 * 24), - ); - if (daysDifference > days) { - writeFileSync(filePath, '', 'utf8'); - } - } - }); - - // Schedule the next execution with doubled delays - timeout = setTimeout( - () => this.cleanLogs(maxFileSizeBytes, daysToKeep), - logCleaningInterval, - ); -} diff --git a/cortex-js/src/utils/logs.ts b/cortex-js/src/utils/logs.ts index 8cd77f23d..3ee06be92 100644 --- a/cortex-js/src/utils/logs.ts +++ b/cortex-js/src/utils/logs.ts @@ -1,6 +1,9 @@ -import { createReadStream } from 'fs'; -import { join } from 'path'; +import { createReadStream, existsSync, stat, writeFileSync } from 'fs'; import { createInterface } from 'readline'; +import { fileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; + +const logCleaningInterval: number = 120000; +let timeout: NodeJS.Timeout | undefined; /** * Print the last N lines of a file that contain the word 'ERROR' @@ -30,3 +33,48 @@ export async function printLastErrorLines( errorLines.forEach((line) => console.log(line)); console.log('...'); } + +export async function cleanLogs( + maxFileSizeBytes?: number | undefined, + daysToKeep?: number | undefined, +): Promise { + // clear existing timeout + // in case we rerun it with different values + if (timeout) clearTimeout(timeout); + timeout = undefined; + + console.log('Validating app logs. Next attempt in ', logCleaningInterval); + + const size = maxFileSizeBytes ?? 1 * 1024 * 1024; // 1 MB + const days = daysToKeep ?? 7; // 7 days + const filePath = await fileManagerService.getLogPath(); + // Perform log cleaning + const currentDate = new Date(); + if (existsSync(filePath)) + stat(filePath, (err, stats) => { + if (err) { + console.error('Error getting file stats:', err); + return; + } + + // Check size + if (stats.size > size) { + writeFileSync(filePath, '', 'utf8'); + } else { + // Check age + const creationDate = new Date(stats.ctime); + const daysDifference = Math.floor( + (currentDate.getTime() - creationDate.getTime()) / (1000 * 3600 * 24), + ); + if (daysDifference > days) { + writeFileSync(filePath, '', 'utf8'); + } + } + }); + + // Schedule the next execution with doubled delays + timeout = setTimeout( + () => this.cleanLogs(maxFileSizeBytes, daysToKeep), + logCleaningInterval, + ); +}