Skip to content

Commit

Permalink
Add better logging for BullMQ jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
valtterikantanen committed Jul 4, 2024
1 parent 6000ac9 commit 032c946
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions services/backend/src/worker/worker.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { Worker } = require('bullmq')
const moment = require('moment')

const { redis } = require('../conf-backend')
const logger = require('../util/logger')
Expand All @@ -18,7 +19,12 @@ const worker = new Worker('refresh-redis-data', `${__dirname}/processor.js`, {
})

worker.on('completed', job => {
logger.info(`Completed job: ${job.id}`)
const timeUsed = moment.duration(moment(job.finishedOn).diff(job.processedOn, undefined, true))
if (timeUsed.seconds() > 60) {
logger.info(`Completed job: ${job.id} (took ${timeUsed.asMinutes().toFixed(3)} minutes)`)
} else {
logger.info(`Completed job: ${job.id} (took ${timeUsed.asSeconds().toFixed(3)} seconds)`)
}
})

// If there is no error event listener, the worker stops taking jobs after any error.
Expand All @@ -28,9 +34,7 @@ worker.on('error', error => {
})

worker.on('failed', job => {
const reason = job?.failedReason ?? ''
const id = job?.id ?? ''
logger.error(`Job ${id} failed.${reason}`)
logger.error(`Job ${job?.id ?? ''} failed. ${job?.stacktrace ? `${job.stacktrace.join('')}` : ''}`)
})

worker.on('active', job => {
Expand Down

0 comments on commit 032c946

Please sign in to comment.