diff --git a/clean_failed_job_stacks.js b/clean_failed_job_stacks.js index cb69089..5c26054 100644 --- a/clean_failed_job_stacks.js +++ b/clean_failed_job_stacks.js @@ -5,7 +5,7 @@ const sendToSlack = require('./utils/slack')(process.env.SLACK_WEBHOOK_URL, proc const sumArray = require('./utils/sumArray') const getFailedQueues = require('./utils/getFailedQueues') -const getJobsBelowAttemptLimit = require('./utils/jobsNotExceedingAttemptsFilter') +const getJobsWithAttemptLimit = require('./utils/jobAttemptsFilter') console.log(`${new Date().toISOString()} - Checking for failed jobs in ${queueNames.join(', ')} queues`) @@ -17,15 +17,15 @@ getFailedQueues(queueNames) .then(async queues => { const retriedJobCountsForEachQueue = await Promise.all( queues.map(async queue => { - const failedJobsToRetry = getJobsBelowAttemptLimit(await queue.getFailed()) + const failedJobsAboveAttemptLimit = getJobsWithAttemptLimit(await queue.getFailed(), true) - if (!failedJobsToRetry.length) { + if (!failedJobsAboveAttemptLimit.length) { return queue.close().then(() => 0) } - const failedCount = failedJobsToRetry.length + const failedCount = failedJobsAboveAttemptLimit.length const retriedJobs = await pMap( - failedJobsToRetry, + failedJobsAboveAttemptLimit, async job => { const { id, data, opts } = job opts.jobId = id diff --git a/retry_failed_jobs.js b/retry_failed_jobs.js index 22cc67e..cc61cbc 100644 --- a/retry_failed_jobs.js +++ b/retry_failed_jobs.js @@ -5,7 +5,7 @@ const sendToSlack = require('./utils/slack')(process.env.SLACK_WEBHOOK_URL, proc const sumArray = require('./utils/sumArray') const getFailedQueues = require('./utils/getFailedQueues') -const getAttemptedJobs = require('./utils/jobsExceedingAttemptsFilter') +const getJobsWithAttemptLimit = require('./utils/jobAttemptsFilter') console.log(`${new Date().toISOString()} - Checking for failed jobs in ${queueNames.join(', ')} queues`) @@ -17,15 +17,15 @@ getFailedQueues(queueNames) .then(async queues => { const retriedJobCountsForEachQueue = await Promise.all( queues.map(async queue => { - const failedJobs = getAttemptedJobs(await queue.getFailed()) + const failedJobsBelowAttemptLimit = getJobsWithAttemptLimit(await queue.getFailed(), false) - if (!failedJobs.length) { + if (!failedJobsBelowAttemptLimit.length) { return queue.close().then(() => 0) } - const failedCount = failedJobs.length + const failedCount = failedJobsBelowAttemptLimit.length const retriedJobs = await pMap( - failedJobs, + failedJobsBelowAttemptLimit, async job => { try { await job.retry() diff --git a/utils/jobsNotExceedingAttemptsFilter.js b/utils/jobAttemptsFilter.js similarity index 57% rename from utils/jobsNotExceedingAttemptsFilter.js rename to utils/jobAttemptsFilter.js index cdf4059..536fb72 100644 --- a/utils/jobsNotExceedingAttemptsFilter.js +++ b/utils/jobAttemptsFilter.js @@ -1,9 +1,11 @@ const MAX_FAILED_COUNT = Number.parseInt(process.env.MAX_FAILED_COUNT) -module.exports = jobs => jobs.filter(job => { +module.exports = (jobs, exceeded = true) => jobs.filter(job => { if (job && typeof job === 'object') { const max = (MAX_FAILED_COUNT || Number.parseInt(job.opts.attempts) || 100) - return job.attemptsMade < max + return exceeded + ? job.attemptsMade > max + : job.attemptsMade < max } return false })