@opens/bullmq
v3.0.0
Published
A wrapper around bullmq
Downloads
466
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
After installing the package, you must first start the library before creating any tasks, as shown in the following example
import { bullmq } from '@opens/bullmq';
// if the BULLMQ_REDIS_HOST and BULLMQ_REDIS_PORT environment variables are set this will work out of the box.
bullmq.start()You may also use a custom connection or redis settings. Either way it works.
import { bullmq } from '@opens/bullmq';
bullmq.start({redis: {host:"localhost", port: 6459}})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,
publishTaskreturnsnull. - If a worker fails, you can define a
downfunction to handle failures.
License
This project is licensed under the MIT License.
