Last active
January 4, 2023 11:06
-
-
Save benjaminudoh10/8971edfb36756dff63a095a731340a00 to your computer and use it in GitHub Desktop.
Bullqueue blog - Queue Service (add workers)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
import DefaultProcessor from './processors.default'; | |
enum JobType { | |
PROCESS_PAYMENT = 'process-payment', | |
} | |
export default class QueueService { | |
... | |
constructor() { | |
... | |
this.instantiateWorkers(); | |
} | |
... | |
async instantiateWorkers() { | |
this.defaultQueueWorker = new Worker( | |
Queues.DEFAULT, | |
async (job: Job) => { | |
switch (job.name) { | |
case JobType.PROCESS_PAYMENT: | |
await DefaultProcessor.processPayment(job); | |
break; | |
} | |
console.log('[DEFAULT QUEUE] Worker for default queue'); | |
}, | |
{ connection: QueueService.QUEUE_OPTIONS.connection }, | |
); | |
this.defaultQueueWorker.on('completed', (job: Job, value) => { | |
console.log( | |
`[DEFAULT QUEUE] Completed job with data\n | |
Data: ${job.asJSON().data}\n | |
ID: ${job.id}\n | |
Value: ${value} | |
`, | |
); | |
}); | |
this.defaultQueueWorker.on('failed', (job: Job, value) => { | |
console.log( | |
`[DEFAULT QUEUE] Failed job with data\n | |
Data: ${job.asJSON().data}\n | |
ID: ${job.id}\n | |
Value: ${value} | |
`, | |
); | |
}); | |
} | |
... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment