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

Commit b040ed1

Browse files
committed
chore: clean logs periodically
1 parent dbc63a0 commit b040ed1

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-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: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { fileManagerService } from "@/infrastructure/services/file-manager/file-manager.service";
2+
import { existsSync, stat, unlink } 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+
unlink(filePath, (err) => {
37+
if (err) {
38+
console.error('Error deleting log file:', err)
39+
return
40+
}
41+
console.debug(
42+
`Deleted log file due to exceeding size limit: ${filePath}`
43+
)
44+
})
45+
} else {
46+
// Check age
47+
const creationDate = new Date(stats.ctime)
48+
const daysDifference = Math.floor(
49+
(currentDate.getTime() - creationDate.getTime()) /
50+
(1000 * 3600 * 24)
51+
)
52+
if (daysDifference > days) {
53+
unlink(filePath, (err) => {
54+
if (err) {
55+
console.error('Error deleting log file:', err)
56+
return
57+
}
58+
console.debug(`Deleted old log file: ${filePath}`)
59+
})
60+
}
61+
}
62+
})
63+
64+
// Schedule the next execution with doubled delays
65+
timeout = setTimeout(
66+
() => this.cleanLogs(maxFileSizeBytes, daysToKeep),
67+
logCleaningInterval
68+
)
69+
}

0 commit comments

Comments
 (0)