Skip to content

Commit e7cc0c5

Browse files
authored
fix: compress skip flipped logic (#22)
1 parent 3813d8a commit e7cc0c5

File tree

6 files changed

+48
-48
lines changed

6 files changed

+48
-48
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Rotated files are appended with the file's change date and the current UTC times
2626

2727
The environment variable `CRON_COMPRESS` is used to schedule the compression of the rotated files. The each file is compressed into a 'tgz' archive.
2828

29-
This stage can run frequently with little cost.
29+
This stage can run frequently with little cost. If you wish to skip this stage, set the environment variable `COMPRESS_SKIP` to be true.
3030

3131
### Stage 3 - Backup log file
3232

src/constants.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ export const CRON_COMPRESS = process.env.CRON_COMPRESS ?? '*/10 * * * *';
33
export const CRON_BACKUP = process.env.CRON_BACKUP ?? '*/20 * * * *';
44
export const CRON_JANITOR = process.env.CRON_JANITOR ?? '*/10 * * * *';
55

6-
export const RUN_ONCE = process.env.RUN_ONCE ?? 'false';
7-
8-
export const COMPRESS_ENABLED = process.env.COMPRESS_ENABLED ?? 'true';
6+
export const RUN_ONCE = process.env.RUN_ONCE === 'true';
7+
export const COMPRESS_SKIP = process.env.COMPRESS_SKIP === 'true';
98

109
export const LOGROTATE_DIRECTORY = process.env.LOGROTATE_DIRECTORY ?? 'logs';
1110
export const LOGROTATE_STATUSFILE =

src/cron/backup.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
BROKER_SERVICE,
1313
BROKER_USER,
1414
DB_FILE_STATUS,
15-
COMPRESS_ENABLED,
15+
COMPRESS_SKIP,
1616
OBJECT_STORAGE_ACCESS_KEY,
1717
OBJECT_STORAGE_BUCKET,
1818
OBJECT_STORAGE_ENABLED,
@@ -52,10 +52,9 @@ const objectstorageMetadata: ItemBucketMetadata =
5252
: undefined;
5353

5454
export async function backup(db: DatabaseService) {
55-
const dbFileStatus =
56-
COMPRESS_ENABLED === 'true'
57-
? DB_FILE_STATUS.Compressed
58-
: DB_FILE_STATUS.Rotated;
55+
const dbFileStatus = COMPRESS_SKIP
56+
? DB_FILE_STATUS.Rotated
57+
: DB_FILE_STATUS.Compressed;
5958
console.log('backup: start');
6059
const result = await db.all<{
6160
id: number;

src/index.broker.ts

Whitespace-only changes.

src/index.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { RUN_ONCE } from './constants';
12
import { DatabaseService } from './services/database.service';
23
import { JobService } from './services/job.service';
34

@@ -13,10 +14,12 @@ async function main() {
1314
'when launching node to enable forced garbage collection.',
1415
);
1516
}
16-
const runOnce = process.env.RUN_ONCE === 'true';
17-
const compressEnabled = process.env.COMPRESS_ENABLED === 'true';
1817

19-
jobService.run(runOnce, compressEnabled);
18+
if (RUN_ONCE) {
19+
await jobService.runOnce();
20+
} else {
21+
await jobService.runCron();
22+
}
2023
}
2124

2225
main();

src/services/job.service.ts

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Cron } from 'croner';
22
import {
3+
COMPRESS_SKIP,
34
CRON_BACKUP,
45
CRON_COMPRESS,
56
CRON_JANITOR,
@@ -24,36 +25,11 @@ export class JobService {
2425
this.db = db;
2526
}
2627

27-
rotate = async () => {
28-
await rotateLogs(this.db);
29-
runGarbageCollection();
30-
};
31-
32-
compress = async () => {
33-
console.log('start compress');
34-
await syncLogsDb(this.db);
35-
await compress(this.db);
36-
runGarbageCollection();
37-
};
38-
39-
backup = async () => {
40-
console.log('start backup');
41-
await syncLogsDb(this.db);
42-
await backup(this.db);
43-
runGarbageCollection();
44-
};
45-
46-
janitor = async () => {
47-
await syncLogsDb(this.db);
48-
await removeOldLogs(this.db);
49-
runGarbageCollection();
50-
};
51-
52-
private async runOnceJob(compressEnabled: boolean) {
28+
public async runOnce() {
5329
// Stage 1: Rotate log
5430
await this.rotate();
5531
// Stage 2: Compress files - optional
56-
if (compressEnabled) {
32+
if (!COMPRESS_SKIP) {
5733
await this.compress();
5834
}
5935
// Stage 3: Backup
@@ -62,23 +38,46 @@ export class JobService {
6238
await this.janitor();
6339
}
6440

65-
private async cronJobs(compressEnabled: boolean) {
41+
public async runCron() {
6642
const rotateCronJob = Cron(CRON_ROTATE, this.rotate);
67-
68-
if (compressEnabled) {
69-
const compressCronJob = Cron(CRON_COMPRESS, this.compress);
70-
console.log(`Compress job next run: ${compressCronJob.nextRun()}`);
71-
}
43+
const compressCronJob = COMPRESS_SKIP
44+
? null
45+
: Cron(CRON_COMPRESS, this.compress);
7246
const backupCronJob = Cron(CRON_BACKUP, this.backup);
7347
const janitorCronJob = Cron(CRON_JANITOR, this.janitor);
7448

7549
console.log(`Backup job next run: ${backupCronJob.nextRun()}`);
50+
if (compressCronJob) {
51+
console.log(`Compress job next run: ${compressCronJob.nextRun()}`);
52+
} else {
53+
console.log(`Compress job next run: stage skipped`);
54+
}
7655
console.log(`Rotate job next run: ${rotateCronJob.nextRun()}`);
7756
console.log(`Janitor job next run: ${janitorCronJob.nextRun()}`);
7857
}
7958

80-
async run(runOnce: boolean, compressEnabled: boolean) {
81-
if (runOnce) this.runOnceJob(compressEnabled);
82-
else this.cronJobs(compressEnabled);
59+
private async rotate() {
60+
await rotateLogs(this.db);
61+
runGarbageCollection();
62+
}
63+
64+
private async compress() {
65+
console.log('start compress');
66+
await syncLogsDb(this.db);
67+
await compress(this.db);
68+
runGarbageCollection();
69+
}
70+
71+
private async backup() {
72+
console.log('start backup');
73+
await syncLogsDb(this.db);
74+
await backup(this.db);
75+
runGarbageCollection();
76+
}
77+
78+
private async janitor() {
79+
await syncLogsDb(this.db);
80+
await removeOldLogs(this.db);
81+
runGarbageCollection();
8382
}
8483
}

0 commit comments

Comments
 (0)