Skip to content

Instantly share code, notes, and snippets.

@crrobinson14
Created February 16, 2018 19:34
Show Gist options
  • Save crrobinson14/b6792cd9b8aa839d6fc0a8c072150d92 to your computer and use it in GitHub Desktop.
Save crrobinson14/b6792cd9b8aa839d6fc0a8c072150d92 to your computer and use it in GitHub Desktop.
Clean old workers and tasks
module.exports = {
start: function(_api, next) {
var maxWorkerAge = 10000;
api.log('Removing stuck workers older than ' + maxWorkerAge + 'ms', 'info');
api.resque.queue.cleanOldWorkers(maxWorkerAge, function(err, result) {
if (err) {
api.log(err, 'error');
}
if (Object.keys(result).length > 0) {
api.log('Removed stuck workers with errors: ', 'info', result);
}
});
// Every 60 seconds, remove failed tasks and re-enqueue stuck/hung jobs.
if (api.config.tasks.scheduler) {
setInterval(function() {
api.tasks.failed(0, -1, (failedTasksErr, failedTasks) => {
failedTasks.map(task => {
api.tasks.removeFailed(task, (removeError, countRemoved) => {
if (removeError) {
api.log('Error removing failed task', 'error', removeError, task);
} else {
api.log('Removed failed task', 'info', task);
}
});
});
});
// Find recurring jobs that have been hanging around too long without actually executing
Object.keys(api.tasks.tasks).map(taskName => {
let task = api.tasks.tasks[taskName];
if (task.frequency > 0) {
api.tasks.scheduledAt(task.queue, taskName, {}, (scheduledAtError, timestamps) => {
(timestamps || []).map(entry => {
let timestamp = +entry,
humanReadable = (new Date(timestamp * 1000));
if (timestamp > 0 && timestamp < (Date.now() / 1000) - 300) {
api.log('Found stuck task', 'warning', timestamp, task);
api.tasks.delDelayed(task.queue, taskName, {}, delDelayedError => {
if (delDelayedError) {
api.log('Error clearing stuck task ' + taskName, 'error', delDelayedError);
} else {
api.log('Cleared stuck task ' + taskName + ', stuck since ' + humanReadable, 'info');
api.tasks.enqueueRecurrentJob(taskName, function() {
// Nothing to do here.
});
}
});
}
});
});
}
});
}, 60000).unref();
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment