From ae8f32e6b4c0cd44c7d71b6d22a9559e4b1070ce Mon Sep 17 00:00:00 2001 From: Hubert Date: Tue, 8 Aug 2023 07:18:59 -0300 Subject: [PATCH] Chore: Desktop: Fixes #8598 - Recent logs appear to be deleted (#8605) --- packages/lib/RotatingLogs.test.ts | 21 +++++++++++++++++++-- packages/lib/RotatingLogs.ts | 3 ++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/packages/lib/RotatingLogs.test.ts b/packages/lib/RotatingLogs.test.ts index cbae7776e..a8990a537 100644 --- a/packages/lib/RotatingLogs.test.ts +++ b/packages/lib/RotatingLogs.test.ts @@ -12,7 +12,7 @@ describe('RotatingLogs', () => { try { dir = await createTempDir(); await createTestLogFile(dir); - let files: string[] = await readdir(dir); + let files = await readdir(dir); expect(files.find(file => file.match(/^log.txt$/gi))).toBeTruthy(); expect(files.length).toBe(1); const rotatingLogs: RotatingLogs = new RotatingLogs(dir, 1, 1); @@ -26,7 +26,7 @@ describe('RotatingLogs', () => { } }); - test('should delete inative log file after 1ms', async () => { + test('should delete inactive log file after 1ms', async () => { let dir: string; try { dir = await createTempDir(); @@ -42,4 +42,21 @@ describe('RotatingLogs', () => { await remove(dir); } }); + + test('should not delete the log-timestamp.txt right after its be created', async () => { + let dir: string; + try { + dir = await createTempDir(); + await createTestLogFile(dir); + await msleep(100); + const rotatingLogs: RotatingLogs = new RotatingLogs(dir, 1, 100); + await rotatingLogs.cleanActiveLogFile(); + await rotatingLogs.deleteNonActiveLogFiles(); + const files = await readdir(dir); + expect(files.find(file => file.match(/^log-[0-9]+.txt$/gi))).toBeTruthy(); + expect(files.length).toBe(1); + } finally { + await remove(dir); + } + }); }); diff --git a/packages/lib/RotatingLogs.ts b/packages/lib/RotatingLogs.ts index ee6b62001..e1446f840 100644 --- a/packages/lib/RotatingLogs.ts +++ b/packages/lib/RotatingLogs.ts @@ -29,7 +29,8 @@ export default class RotatingLogs { const files: Stat[] = await this.fsDriver().readDirStats(this.logFilesDir); for (const file of files) { if (!file.path.match(/^log-[0-9]+.txt$/gi)) continue; - const ageOfTheFile: number = Date.now() - file.birthtime; + const timestamp: number = parseInt(file.path.match(/[0-9]+/g)[0], 10); + const ageOfTheFile: number = Date.now() - timestamp; if (ageOfTheFile >= this.inactiveMaxAge) { await this.fsDriver().remove(this.logFileFullpath(file.path)); }