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

Commit 967ed0e

Browse files
authored
chore: clean logs periodically (#972)
1 parent bb55bb8 commit 967ed0e

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

cortex-js/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ 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';
1011

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

2021
try {
22+
// Clean log periodically
23+
cleanLogs();
2124
await app.listen(sPort, sHost);
2225
const cortexUsecases = await app.resolve(CortexUsecases);
2326
await cortexUsecases.startCortex();

cortex-js/src/main.ts

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

89
async function bootstrap() {
910
const app = await getApp();
@@ -12,6 +13,8 @@ async function bootstrap() {
1213
const port = process.env.CORTEX_JS_PORT || defaultCortexJsPort;
1314

1415
try {
16+
// Clean logs periodically
17+
cleanLogs();
1518
await app.listen(port, host);
1619
console.log(chalk.blue(`Started server at http://${host}:${port}`));
1720
console.log(

cortex-js/src/utils/log.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { fileManagerService } from "@/infrastructure/services/file-manager/file-manager.service";
2+
import { existsSync, stat, unlink, writeFileSync } from "fs";
3+
4+
const logCleaningInterval: number = 120000
5+
let timeout: NodeJS.Timeout | undefined;
6+
7+
export async function cleanLogs(
8+
maxFileSizeBytes?: number | undefined,
9+
daysToKeep?: number | undefined,
10+
): Promise<void> {
11+
// clear existing timeout
12+
// in case we rerun it with different values
13+
if (timeout) clearTimeout(timeout)
14+
timeout = undefined
15+
16+
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()
25+
// Perform log cleaning
26+
const currentDate = new Date()
27+
if (existsSync(filePath))
28+
stat(filePath, (err, stats) => {
29+
if (err) {
30+
console.error('Error getting file stats:', err)
31+
return
32+
}
33+
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')
46+
}
47+
}
48+
})
49+
50+
// Schedule the next execution with doubled delays
51+
timeout = setTimeout(
52+
() => this.cleanLogs(maxFileSizeBytes, daysToKeep),
53+
logCleaningInterval
54+
)
55+
}

0 commit comments

Comments
 (0)