Last active
January 4, 2023 10:33
-
-
Save benjaminudoh10/9965aad9f8a5fa2563b77809b7a9cbfe to your computer and use it in GitHub Desktop.
Bullqueue blog - Queue Service
This file contains hidden or 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 { Queue } from 'bullmq'; | |
enum Queues { | |
DEFAULT = 'default', | |
} | |
export default class QueueService { | |
private queues: Record<string, Queue>; | |
private defaultQueue: Queue; | |
private static instance: QueueService; | |
private static QUEUE_OPTIONS = { | |
defaultJobOptions: { | |
removeOnComplete: false, // this indicates if the job should be removed from the queue once it's complete | |
removeOnFail: false, // this indicates if the job should be removed from the queue if it fails | |
}, | |
connection: { | |
// redis server connection options | |
host: process.env.REDIS_HOST, | |
port: process.env.REDIS_PORT, | |
}, | |
}; | |
constructor() { | |
if (QueueService.instance instanceof QueueService) { | |
return QueueService.instance; | |
} | |
this.queues = {}; | |
QueueService.instance = this; | |
this.instantiateQueues(); | |
} | |
async instantiateQueues() { | |
this.defaultQueue = new Queue(Queues.DEFAULT, QueueService.QUEUE_OPTIONS); | |
this.queues[Queues.DEFAULT] = this.defaultQueue; | |
} | |
getQueue(name: Queues) { | |
return this.queues[name]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment