Skip to content

Instantly share code, notes, and snippets.

@benjaminudoh10
Last active January 4, 2023 10:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benjaminudoh10/9965aad9f8a5fa2563b77809b7a9cbfe to your computer and use it in GitHub Desktop.
Save benjaminudoh10/9965aad9f8a5fa2563b77809b7a9cbfe to your computer and use it in GitHub Desktop.
Bullqueue blog - Queue Service
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