Skip to content

feat: add options for compress and run once #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ export const CRON_COMPRESS = process.env.CRON_COMPRESS ?? '*/10 * * * *';
export const CRON_BACKUP = process.env.CRON_BACKUP ?? '*/20 * * * *';
export const CRON_JANITOR = process.env.CRON_JANITOR ?? '*/10 * * * *';

export const RUN_ONCE = process.env.RUN_ONCE ?? 'false';

export const COMPRESS_ENABLED = process.env.COMPRESS_ENABLED ?? 'true';

export const LOGROTATE_DIRECTORY = process.env.LOGROTATE_DIRECTORY ?? 'logs';
export const LOGROTATE_STATUSFILE =
process.env.LOGROTATE_STATUSFILE ?? 'cron.db';
Expand Down
56 changes: 44 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,62 @@ async function main() {
'when launching node to enable forced garbage collection.',
);
}
const runOnce = process.env.RUN_ONCE === 'true';
const compressEnabled = process.env.COMPRESS_ENABLED === 'true';

const rotateJob = Cron(CRON_ROTATE, async () => {
const rotateJob = async () => {
await rotateLogs(db);
runGarbageCollection();
});
const compressJob = Cron(CRON_COMPRESS, async () => {
};

const compressEnabledJob = async () => {
await syncLogsDb(db);
await compress(db);
runGarbageCollection();
});
const backupJob = Cron(CRON_BACKUP, async () => {
};

const backupJob = async () => {
await syncLogsDb(db);
await backup(db);
runGarbageCollection();
});
const janitorJob = Cron(CRON_JANITOR, async () => {
};

const janitorJob = async () => {
await syncLogsDb(db);
await removeOldLogs(db);
runGarbageCollection();
});
console.log(`Rotate job next run: ${rotateJob.nextRun()}`);
console.log(`Compress job next run: ${compressJob.nextRun()}`);
console.log(`Backup job next run: ${backupJob.nextRun()}`);
console.log(`Janitor job next run: ${janitorJob.nextRun()}`);
};

const combinedJob = async () => {
// Stage 1: Rotate log
rotateJob();

// Stage 2: Compress files - optional
// Stage 3: Backup - if compress enabled
if (compressEnabled) {
compressEnabledJob();
backupJob();
}

// Stage 4: Janitor
janitorJob();
};

if (runOnce) combinedJob();
else {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is one a function call and the other not?

const rotateCronJob = Cron(CRON_ROTATE, rotateJob);

if (compressEnabled) {
const compressCronJob = Cron(CRON_COMPRESS, compressEnabledJob);
const backupCronJob = Cron(CRON_BACKUP, backupJob);
console.log(`Compress job next run: ${compressCronJob.nextRun()}`);
console.log(`Backup job next run: ${backupCronJob.nextRun()}`);
}

const janitorCronJob = Cron(CRON_JANITOR, janitorJob);
console.log(`Rotate job next run: ${rotateCronJob.nextRun()}`);
console.log(`Janitor job next run: ${janitorCronJob.nextRun()}`);
}
}

main();