redis-tasks v0.1.3
Redis Tasks
This module will allow you to create producers and consumers for tasks.
It requires tasks to be managed by task-distributor
.
Tasks are BullMQ jobs.
TaskProducer
The TaskProducer has the ability to produce tasks to a queue that will be consumed by any started consumer on the queue. Example:
const producer = new TaskProducer('https://task-distributor');
await producer.produce({
queue: 'queueName',
target: 'targetId',
data: { key: 'value' },
})
TaskConsumer
The task consumer is a wrapper to BullMQ worker. it requires a processor function which is invoked each time we have a new task. as an argument The callback will be run when a task is found, if the promise is resolved as true the task is completed otherwise if resolved as false it will not be finished, and if there is a monitor it will be returned to the todo queue.
const consumer = new TaskConsumer({
taskDistributorUrl: 'https://task-distributor',
queue: 'queueName',
processor: (job: Job) => new Promise.resolve(true),
workerOptions: { connection: redisOptions },
});
redisOptions
is required otherwise the worker will connect to localhost redis.
You can pass along any other WorkerOptions
that Bull's worker has.
Processor Function
The processor function is an async function that gets the Job
as an argument.
Any data returned by the processor function will be sent to a resultQueue to be logged as a completed task.
Errors thrown by the processor function will be logged (as well as stacktrace), as a failed task.
If a processor function is not provided (processor=undefined
) you'll have to consume jobs manually, for more details refer to BullMQ docs.
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago