1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-08-24 20:19:10 +02:00

Compare commits

...

33 Commits

Author SHA1 Message Date
Hubert
4a6bca7924 Separate the tests to each part os the public api of the RotatingLogs. 2023-07-14 09:42:52 -03:00
Hubert
a3d14353fa Merge remote-tracking branch 'origin/dev' into issue-5521 2023-07-14 09:40:56 -03:00
Hubert
2f5f685bc9 Merge remote-tracking branch 'origin/issue-5521' into issue-5521 2023-07-13 16:24:56 -03:00
Hubert
007b2926e9 Merge remote-tracking branch 'origin/dev' into issue-5521 2023-07-13 16:24:20 -03:00
Hubert
8d817bc437 Wrapping the test inside a try, finally block to clean the temp directory even when the test fail. 2023-07-13 16:23:18 -03:00
Laurent Cozic
ce5abb7726 Merge branch 'dev' into issue-5521 2023-07-13 19:10:23 +01:00
Hubert
f9b445e0cf Applying the changes from review. 2023-07-13 14:37:21 -03:00
Hubert
03feb3af5d Merge remote-tracking branch 'origin/dev' into issue-5521 2023-07-12 14:49:45 -03:00
Hubert
1cdea38917 Merge remote-tracking branch 'origin/issue-5521' into issue-5521 2023-07-12 14:48:06 -03:00
Hubert
e906a1db75 Simplification of create file function. 2023-07-12 14:46:59 -03:00
Laurent Cozic
e0d0936234 Merge branch 'dev' into issue-5521 2023-07-12 18:10:59 +01:00
Hubert
d9e96d199c Removed the beforeEach and afterEach hooks. 2023-07-12 12:17:37 -03:00
Hubert
8535ba9812 Just type one variable. 2023-07-12 10:26:11 -03:00
Hubert
2ff14ba24b Changed the setTimeout to msleep. Now it's working well. 2023-07-12 09:38:23 -03:00
Hubert
dbfad8b14d Removed the BaseApplication tests. I', still not sure about the setTimeout inside the tests, it doesn't fail, but still seems that isn't working right. I'm committing it to be able to show the code and discuss it better. 2023-07-12 09:09:20 -03:00
Hubert
a9f8cd89db WIP - testing the BaseApplication, I'm committing it just to show the code and discuss it. It isn't finished yet! 2023-07-11 18:25:18 -03:00
Hubert
9e75cb3ee6 WIP - testing cleanActiveLogFile function, and fixes from code review. 2023-07-11 16:32:45 -03:00
Hubert
cb9c623d71 WIP - Testing cleanActiveLogFile function. 2023-07-11 11:54:35 -03:00
Hubert
e29f139c8c Merge remote-tracking branch 'origin/dev' into issue-5521 2023-07-05 15:05:21 -03:00
Hubert
e24fddbc5c Merge remote-tracking branch 'origin/dev' into issue-5521 2023-07-05 14:52:12 -03:00
Hubert
9422e66e65 Removed dayjs from the lib package. And implement the corrections from review. 2023-07-05 14:50:40 -03:00
Hubert
cae5e2671a Add the type in some variables/properties. 2023-07-03 19:00:54 -03:00
Hubert
4df9ce7530 Add the try/catch blocks and call using the appLogger to log the possible errors. Also, I did some minor improvements in the readability of the code. I believe that now the issue is finished. 2023-07-03 18:57:39 -03:00
Hubert
4a52a240e3 I've just rename a function and add two more consts for code clarification. 2023-07-03 18:29:48 -03:00
Hubert
d67b3ef9e6 Improvements from the review. 2023-07-03 16:25:40 -03:00
Hubert
044e1f6420 Move from moment to dayjs and fix some types. 2023-06-30 12:11:15 -03:00
Hubert
2af92ba550 Installed dayjs in the package/lib. 2023-06-30 12:09:00 -03:00
Hubert
c1684364ef Merge remote-tracking branch 'origin/dev' into issue-5521 2023-06-30 11:56:40 -03:00
Hubert
e8e0bdc15e Things from review. 2023-06-30 10:14:10 -03:00
Hubert
68fc749243 Improvements on the exception handling and implementation of the logic to call the 'rotational logs' logic. 2023-06-30 09:17:12 -03:00
Hubert
f9b842299e Undo the modifications. 2023-06-29 16:40:44 -03:00
Hubert
4b385fc43f It's working, but I still need to set fsDriver_ properly, and write the logic to call the functions once a day. 2023-06-29 16:37:05 -03:00
Hubert
b2e390c831 Creates another log file when the production log file reaches 5MB; And delete all log files older than 30 days. 2023-06-27 14:16:08 -03:00
3 changed files with 107 additions and 0 deletions

View File

@@ -59,6 +59,8 @@ import Resource from './models/Resource';
import { ProfileConfig } from './services/profileConfig/types';
import initProfile from './services/profileConfig/initProfile';
import RotatingLogs from './RotatingLogs';
const appLogger: LoggerWrapper = Logger.create('App');
// const ntpClient = require('./vendor/ntp-client');
@@ -85,6 +87,8 @@ export default class BaseApplication {
protected store_: Store<any> = null;
private rotatingLogs: RotatingLogs;
public constructor() {
this.eventEmitter_ = new EventEmitter();
this.decryptionWorker_resourceMetadataButNotBlobDecrypted = this.decryptionWorker_resourceMetadataButNotBlobDecrypted.bind(this);
@@ -927,6 +931,18 @@ export default class BaseApplication {
await MigrationService.instance().run();
this.rotatingLogs = new RotatingLogs(profileDir);
const processLogs = async () => {
try {
await this.rotatingLogs.cleanActiveLogFile();
await this.rotatingLogs.deleteNonActiveLogFiles();
} catch (error) {
appLogger.error(error);
}
};
shim.setTimeout(() => { void processLogs(); }, 60000);
shim.setInterval(() => { void processLogs(); }, 24 * 60 * 60 * 1000);
return argv;
}
}

View File

@@ -0,0 +1,45 @@
import { writeFile, readdir, remove } from 'fs-extra';
import { createTempDir, msleep } from './testing/test-utils';
import RotatingLogs from './RotatingLogs';
const createTestLogFile = async (dir: string) => {
await writeFile(`${dir}/log.txt`, 'some content');
};
describe('RotatingLogs', () => {
test('should rename log.txt to log-TIMESTAMP.txt', async () => {
let dir: string;
try {
dir = await createTempDir();
await createTestLogFile(dir);
let files: string[] = 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);
await rotatingLogs.cleanActiveLogFile();
files = await readdir(dir);
expect(files.find(file => file.match(/^log.txt$/gi))).toBeFalsy();
expect(files.find(file => file.match(/^log-[0-9]+.txt$/gi))).toBeTruthy();
expect(files.length).toBe(1);
} finally {
await remove(dir);
}
});
test('should delete inative log file after 1ms', async () => {
let dir: string;
try {
dir = await createTempDir();
await createTestLogFile(dir);
const rotatingLogs: RotatingLogs = new RotatingLogs(dir, 1, 1);
await rotatingLogs.cleanActiveLogFile();
await msleep(1);
await rotatingLogs.deleteNonActiveLogFiles();
const files = await readdir(dir);
expect(files.find(file => file.match(/^log-[0-9]+.txt$/gi))).toBeFalsy();
expect(files.length).toBe(0);
} finally {
await remove(dir);
}
});
});

View File

@@ -0,0 +1,46 @@
import shim from './shim';
import { Stat } from './fs-driver-base';
export default class RotatingLogs {
private logFilesDir: string;
private maxFileSize: number = 1024 * 1024 * 100;
private inactiveMaxAge: number = 90 * 24 * 60 * 60 * 1000;
public constructor(logFilesDir: string, maxFileSize: number = null, inactiveMaxAge: number = null) {
this.logFilesDir = logFilesDir;
if (maxFileSize) this.maxFileSize = maxFileSize;
if (inactiveMaxAge) this.inactiveMaxAge = inactiveMaxAge;
}
public async cleanActiveLogFile() {
const stats: Stat = await this.fsDriver().stat(this.logFileFullpath());
if (stats.size >= this.maxFileSize) {
const newLogFile: string = this.logFileFullpath(this.getNameToNonActiveLogFile());
await this.fsDriver().move(this.logFileFullpath(), newLogFile);
}
}
private getNameToNonActiveLogFile(): string {
return `log-${Date.now()}.txt`;
}
public async deleteNonActiveLogFiles() {
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;
if (ageOfTheFile >= this.inactiveMaxAge) {
await this.fsDriver().remove(this.logFileFullpath(file.path));
}
}
}
private logFileFullpath(fileName = 'log.txt'): string {
return `${this.logFilesDir}/${fileName}`;
}
private fsDriver() {
return shim.fsDriver();
}
}