Skip to content

Commit

Permalink
Improved job attempts filter, fixed retry and clean stack scripts.
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Michalski committed May 19, 2021
1 parent cb7a89a commit a5728b3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
10 changes: 5 additions & 5 deletions clean_failed_job_stacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`)

Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions retry_failed_jobs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`)

Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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
})
Expand Down

0 comments on commit a5728b3

Please sign in to comment.