Skip to content

Commit 15e17a9

Browse files
committed
fix: refactor jobs
1 parent aa52fc7 commit 15e17a9

File tree

1 file changed

+36
-32
lines changed

1 file changed

+36
-32
lines changed

src/index.ts

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -31,54 +31,58 @@ async function main() {
3131
const runOnce = process.env.RUN_ONCE === 'true';
3232
const compressEnabled = process.env.COMPRESS_ENABLED === 'true';
3333

34-
const combinedJob = async () => {
35-
// Stage 1: Rotate log
34+
const rotateJob = async () => {
3635
await rotateLogs(db);
36+
runGarbageCollection();
37+
};
3738

38-
// Stage 2: Compress files - optional
39-
if (compressEnabled) {
40-
await syncLogsDb(db);
41-
await compress(db);
42-
}
39+
const compressEnabledJob = async () => {
40+
await syncLogsDb(db);
41+
await compress(db);
42+
runGarbageCollection();
43+
};
4344

44-
// Stage 3: Backup
45+
const backupJob = async () => {
4546
await syncLogsDb(db);
4647
await backup(db);
48+
runGarbageCollection();
49+
};
4750

48-
// Stage 4: Janitor
51+
const janitorJob = async () => {
4952
await syncLogsDb(db);
5053
await removeOldLogs(db);
5154
runGarbageCollection();
5255
};
5356

57+
const combinedJob = async () => {
58+
// Stage 1: Rotate log
59+
rotateJob();
60+
61+
// Stage 2: Compress files - optional
62+
// Stage 3: Backup - if compress enabled
63+
if (compressEnabled) {
64+
compressEnabledJob();
65+
backupJob();
66+
}
67+
68+
// Stage 4: Janitor
69+
janitorJob();
70+
};
71+
5472
if (runOnce) combinedJob();
5573
else {
56-
const rotateJob = Cron(CRON_ROTATE, async () => {
57-
await rotateLogs(db);
58-
runGarbageCollection();
59-
});
74+
const rotateCronJob = Cron(CRON_ROTATE, rotateJob);
6075

6176
if (compressEnabled) {
62-
const compressJob = Cron(CRON_COMPRESS, async () => {
63-
await syncLogsDb(db);
64-
await compress(db);
65-
runGarbageCollection();
66-
});
67-
console.log(`Compress job next run: ${compressJob.nextRun()}`);
77+
const compressCronJob = Cron(CRON_COMPRESS, compressEnabledJob);
78+
const backupCronJob = Cron(CRON_BACKUP, backupJob);
79+
console.log(`Compress job next run: ${compressCronJob.nextRun()}`);
80+
console.log(`Backup job next run: ${backupCronJob.nextRun()}`);
6881
}
69-
const backupJob = Cron(CRON_BACKUP, async () => {
70-
await syncLogsDb(db);
71-
await backup(db);
72-
runGarbageCollection();
73-
});
74-
const janitorJob = Cron(CRON_JANITOR, async () => {
75-
await syncLogsDb(db);
76-
await removeOldLogs(db);
77-
runGarbageCollection();
78-
});
79-
console.log(`Rotate job next run: ${rotateJob.nextRun()}`);
80-
console.log(`Backup job next run: ${backupJob.nextRun()}`);
81-
console.log(`Janitor job next run: ${janitorJob.nextRun()}`);
82+
83+
const janitorCronJob = Cron(CRON_JANITOR, janitorJob);
84+
console.log(`Rotate job next run: ${rotateCronJob.nextRun()}`);
85+
console.log(`Janitor job next run: ${janitorCronJob.nextRun()}`);
8286
}
8387
}
8488

0 commit comments

Comments
 (0)