-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { fileManagerService } from "@/infrastructure/services/file-manager/file-manager.service"; | ||
import { existsSync, stat, unlink } 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) { | ||
unlink(filePath, (err) => { | ||
if (err) { | ||
console.error('Error deleting log file:', err) | ||
return | ||
} | ||
console.debug( | ||
`Deleted log file due to exceeding size limit: ${filePath}` | ||
) | ||
}) | ||
} else { | ||
// Check age | ||
const creationDate = new Date(stats.ctime) | ||
const daysDifference = Math.floor( | ||
(currentDate.getTime() - creationDate.getTime()) / | ||
(1000 * 3600 * 24) | ||
) | ||
if (daysDifference > days) { | ||
unlink(filePath, (err) => { | ||
if (err) { | ||
console.error('Error deleting log file:', err) | ||
return | ||
} | ||
console.debug(`Deleted old log file: ${filePath}`) | ||
}) | ||
} | ||
} | ||
}) | ||
|
||
// Schedule the next execution with doubled delays | ||
timeout = setTimeout( | ||
() => this.cleanLogs(maxFileSizeBytes, daysToKeep), | ||
logCleaningInterval | ||
) | ||
} |