@opens/bullmq
v1.0.11
Published
A wrapper around bullmq
Downloads
292
Keywords
Readme
BullMQWrapper
BullMQWrapper is a wrapper around BullMQ that simplifies creating and managing task queues using Redis. It provides an easy interface for queue creation, worker registration, and task publishing.
Usage
Install
On top of installing this package, the following variables must be defined in your environment or an exception will be thrown
BULLMQ_REDIS_HOST BULLMQ_REDIS_PORT
Creating a Task Queue
To create a queue, you can use createTask
. You can specify the queue name, job options, and an optional worker to process jobs.
import { bullmq } from '@opens/bullmq';
bullmq.createTask({
taskName: 'example-task',
concurrency: 5,
up: async (data, job) => {
console.log('Processing job:', job.id);
},
});
Publishing a Task
Once the queue is created, you can add tasks to it using publishTask
.
await bullmq.publishTask({
taskName: 'example-task',
taskData: { key: 'value' },
taskOptions: { delay: 1000 },
});
Creating a Worker
Workers process jobs in the queue. You can create a worker separately using createWorker
.
bullmq.createWorker({
taskName: 'example-task',
concurrency: 3,
up: async (data, job) => {
console.log('Job executed:', job.data);
},
down: async (data, job, error) => {
console.log('Job failed::', job.id);
console.error("With error::", error)
},
});
Getting Task Count
To check the number of active queues, use getTaskCount
.
console.log('Number of task queues:', bullmq.getTaskCount());
Error Handling
- If a task queue does not exist,
publishTask
returnsnull
. - If a worker fails, you can define a
down
function to handle failures.
License
This project is licensed under the MIT License.