Skip to content

Commit

Permalink
chore: clean logs periodically (#972)
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-jan authored Aug 5, 2024
1 parent bb55bb8 commit 967ed0e
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cortex-js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ 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
Expand All @@ -18,6 +19,8 @@ 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();
Expand Down
3 changes: 3 additions & 0 deletions cortex-js/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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();
Expand All @@ -12,6 +13,8 @@ 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(
Expand Down
55 changes: 55 additions & 0 deletions cortex-js/src/utils/log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
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<void> {
// 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
)
}

0 comments on commit 967ed0e

Please sign in to comment.