Skip to content
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

fix: compress skip flipped logic #22

Merged
merged 1 commit into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Rotated files are appended with the file's change date and the current UTC times

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

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

### Stage 3 - Backup log file

Expand Down
5 changes: 2 additions & 3 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ 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 RUN_ONCE = process.env.RUN_ONCE === 'true';
export const COMPRESS_SKIP = process.env.COMPRESS_SKIP === 'true';

export const LOGROTATE_DIRECTORY = process.env.LOGROTATE_DIRECTORY ?? 'logs';
export const LOGROTATE_STATUSFILE =
Expand Down
9 changes: 4 additions & 5 deletions src/cron/backup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
BROKER_SERVICE,
BROKER_USER,
DB_FILE_STATUS,
COMPRESS_ENABLED,
COMPRESS_SKIP,
OBJECT_STORAGE_ACCESS_KEY,
OBJECT_STORAGE_BUCKET,
OBJECT_STORAGE_ENABLED,
Expand Down Expand Up @@ -52,10 +52,9 @@ const objectstorageMetadata: ItemBucketMetadata =
: undefined;

export async function backup(db: DatabaseService) {
const dbFileStatus =
COMPRESS_ENABLED === 'true'
? DB_FILE_STATUS.Compressed
: DB_FILE_STATUS.Rotated;
const dbFileStatus = COMPRESS_SKIP
? DB_FILE_STATUS.Rotated
: DB_FILE_STATUS.Compressed;
console.log('backup: start');
const result = await db.all<{
id: number;
Expand Down
Empty file removed src/index.broker.ts
Empty file.
9 changes: 6 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { RUN_ONCE } from './constants';
import { DatabaseService } from './services/database.service';
import { JobService } from './services/job.service';

Expand All @@ -13,10 +14,12 @@ 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';

jobService.run(runOnce, compressEnabled);
if (RUN_ONCE) {
await jobService.runOnce();
} else {
await jobService.runCron();
}
}

main();
71 changes: 35 additions & 36 deletions src/services/job.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Cron } from 'croner';
import {
COMPRESS_SKIP,
CRON_BACKUP,
CRON_COMPRESS,
CRON_JANITOR,
Expand All @@ -24,36 +25,11 @@ export class JobService {
this.db = db;
}

rotate = async () => {
await rotateLogs(this.db);
runGarbageCollection();
};

compress = async () => {
console.log('start compress');
await syncLogsDb(this.db);
await compress(this.db);
runGarbageCollection();
};

backup = async () => {
console.log('start backup');
await syncLogsDb(this.db);
await backup(this.db);
runGarbageCollection();
};

janitor = async () => {
await syncLogsDb(this.db);
await removeOldLogs(this.db);
runGarbageCollection();
};

private async runOnceJob(compressEnabled: boolean) {
public async runOnce() {
// Stage 1: Rotate log
await this.rotate();
// Stage 2: Compress files - optional
if (compressEnabled) {
if (!COMPRESS_SKIP) {
await this.compress();
}
// Stage 3: Backup
Expand All @@ -62,23 +38,46 @@ export class JobService {
await this.janitor();
}

private async cronJobs(compressEnabled: boolean) {
public async runCron() {
const rotateCronJob = Cron(CRON_ROTATE, this.rotate);

if (compressEnabled) {
const compressCronJob = Cron(CRON_COMPRESS, this.compress);
console.log(`Compress job next run: ${compressCronJob.nextRun()}`);
}
const compressCronJob = COMPRESS_SKIP
? null
: Cron(CRON_COMPRESS, this.compress);
const backupCronJob = Cron(CRON_BACKUP, this.backup);
const janitorCronJob = Cron(CRON_JANITOR, this.janitor);

console.log(`Backup job next run: ${backupCronJob.nextRun()}`);
if (compressCronJob) {
console.log(`Compress job next run: ${compressCronJob.nextRun()}`);
} else {
console.log(`Compress job next run: stage skipped`);
}
console.log(`Rotate job next run: ${rotateCronJob.nextRun()}`);
console.log(`Janitor job next run: ${janitorCronJob.nextRun()}`);
}

async run(runOnce: boolean, compressEnabled: boolean) {
if (runOnce) this.runOnceJob(compressEnabled);
else this.cronJobs(compressEnabled);
private async rotate() {
await rotateLogs(this.db);
runGarbageCollection();
}

private async compress() {
console.log('start compress');
await syncLogsDb(this.db);
await compress(this.db);
runGarbageCollection();
}

private async backup() {
console.log('start backup');
await syncLogsDb(this.db);
await backup(this.db);
runGarbageCollection();
}

private async janitor() {
await syncLogsDb(this.db);
await removeOldLogs(this.db);
runGarbageCollection();
}
}
Loading