Skip to content

Commit

Permalink
fix: remove the old expired jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Yankov committed Mar 15, 2024
1 parent f7be702 commit 5f91e22
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
7 changes: 4 additions & 3 deletions cfg/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"logger": {
"console": {
"handleExceptions": false,
"level": "error",
"level": "silly",
"colorize": true,
"prettyPrint": true
},
Expand Down Expand Up @@ -147,7 +147,7 @@
"configUpdateResponse": {
"messageObject": "io.restorecommerce.commandinterface.CommandResponse"
},
"setApiKeyCommand": {
"setApiKeyCommand": {
"messageObject": "io.restorecommerce.commandinterface.CommandRequest"
},
"setApiKeyResponse": {
Expand Down Expand Up @@ -308,6 +308,7 @@
},
"queueCleanup": {
"ttlAfterFinished": 86400000,
"cleanInterval": 86400000
"cleanInterval": 86400000,
"maxJobsToCleanLimit": 10000
}
}
14 changes: 7 additions & 7 deletions src/schedulingService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1096,11 +1096,11 @@ export class SchedulingService implements SchedulingServiceServiceImplementation
* Clean up queues - removes complted and failed jobs from queue
* @param {any} job clean up job
*/
async cleanupJobs(ttlAfterFinished) {
async cleanupJobs(ttlAfterFinished: number, maxJobsToCleanLimit: number) {
for (let queue of this.queuesList) {
try {
await queue.clean(ttlAfterFinished, 0, COMPLETED_JOB_STATE);
await queue.clean(ttlAfterFinished, 0, FAILED_JOB_STATE);
await queue.clean(ttlAfterFinished, maxJobsToCleanLimit, COMPLETED_JOB_STATE);
await queue.clean(ttlAfterFinished, maxJobsToCleanLimit, FAILED_JOB_STATE);
} catch (err) {
this.logger.error('Error cleaning up jobs', err);
}
Expand All @@ -1110,7 +1110,7 @@ export class SchedulingService implements SchedulingServiceServiceImplementation
await this.repeatJobIdRedisClient.set(QUEUE_CLEANUP, JSON.stringify(lastExecutedInterval));
}

async setupCleanInterval(cleanInterval: number, ttlAfterFinished: number) {
async setupCleanInterval(cleanInterval: number, ttlAfterFinished: number, maxJobsToCleanLimit: number) {
if (!ttlAfterFinished) {
ttlAfterFinished = DEFAULT_CLEANUP_COMPLETED_JOBS;
}
Expand All @@ -1128,11 +1128,11 @@ export class SchedulingService implements SchedulingServiceServiceImplementation
// use setTimeout and then create interval on setTimeout
this.logger.info('Restoring previous execution interval with set timeout', { time: cleanInterval - delta });
setTimeout(async () => {
await this.cleanupJobs(ttlAfterFinished);
setInterval(this.cleanupJobs.bind(this), cleanInterval, ttlAfterFinished);
await this.cleanupJobs(ttlAfterFinished, maxJobsToCleanLimit);
setInterval(this.cleanupJobs.bind(this), cleanInterval, ttlAfterFinished, maxJobsToCleanLimit);
}, cleanInterval - delta);
} else {
setInterval(this.cleanupJobs.bind(this), cleanInterval, ttlAfterFinished);
setInterval(this.cleanupJobs.bind(this), cleanInterval, ttlAfterFinished, maxJobsToCleanLimit);
this.logger.info('Clean up job interval set successfully');
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ export class Worker {
// cleanup job
const queueCleanup = cfg.get('queueCleanup');
if (queueCleanup?.cleanInterval && typeof queueCleanup.cleanInterval === 'number') {
await schedulingService.setupCleanInterval(queueCleanup.cleanInterval, queueCleanup.ttlAfterFinished);
await schedulingService.setupCleanInterval(queueCleanup.cleanInterval, queueCleanup.ttlAfterFinished, queueCleanup.maxJobsToCleanLimit);
}

const cis = new JobsCommandInterface(server, cfg,
Expand Down Expand Up @@ -354,7 +354,7 @@ export class Worker {
}
// check for double default
let fileImport = await import(require_dir + externalFile);
if(fileImport?.default?.default) {
if (fileImport?.default?.default) {
(async () => (await import(require_dir + externalFile)).default.default(cfg, logger, events, runWorker))().catch(err => {
this.logger.error(`Error scheduling external job ${externalFile}`, { err: err.message });
});
Expand Down

0 comments on commit 5f91e22

Please sign in to comment.