Commit 1eb893ef authored by Caleb Weeks's avatar Caleb Weeks

adding clean up jobs

parent 8a6fffb7
const _ = require("lodash");
const oneDayInMillis = 24 * 60 * 60 * 1000;
const statusMap = {
completed: {
configKey: 'removeOnComplete',
gracePeriod: oneDayInMillis,
cron: '0 0 0 ? * *' //run every day at midnight
},
failed: {
configKey: 'removeOnFailed',
gracePeriod: oneDayInMillis * 7,
cron: '0 0 0 ? * SUN' //run every Sunday at midnight
}
}
module.exports = queueStatus => {
const log = global.bl.logger.get(`blacklight.edit.queues.${queueStatus}-jobs-cleanup`);
const {configKey, gracePeriod, cron} = statusMap[queueStatus];
return {
process: {
processor: async job => {
const results = {};
//get all queues
const queueIds = global.bl.queues.getAll();
for(const queueId of queueIds){
//get the actuall BullQueue Object and the initial definition
const {getBullQueue, getDefinition} = global.bl.queues.get(queueId);
const definition = getDefinition();
const queue = getBullQueue();
//if this queue doesn't define a custom removeOnComplete, it's a candidate
const configValue = _.get(definition, ["options", "defaultJobOptions", configKey]);
if(!_.isBoolean(configValue) && !_.isNumber(configValue)){
//this queue is a candidate
const cleanResult = await queue.clean(job.data.grace ||gracePeriod, queueStatus);
results[queueId] = (`Cleaned ${cleanResult.length} jobs`);
}
}
return results;
}
},
events: {
completed: (job) => {
log.info(`${queueStatus}-jobs-cleanup job complete. JOB ID: ${job.id}`);
},
failed: (job, err) => {
log.error(`${queueStatus}-jobs-cleanup job failed. JOB ID: ${job.id}`, err);
}
},
options: {
defaultJobOptions: {
attemps: 10,
delay: 100,
removeOnComplete: 50,
removeOnFailed: 100
}
},
description: `Queue for cleaning up ${queueStatus} jobs from all queues that don't provide a custom ${configKey} in their definition`,
singleRepeatedJob: {
repeat: {
cron
}
}
};
}
\ No newline at end of file
module.exports = require('../lib/cleanup-jobs-generator')('completed');
\ No newline at end of file
module.exports = require('../lib/cleanup-jobs-generator')('failed');
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment