queue-worker
v2.1.0
Published
RabbitMQ queue worker
Downloads
4
Readme
queue-worker
RabbitMQ queue worker.
How to use?
- To install package run the command
npm install rabbit-queue queue-worker --save
Rabbit
should be initialized with flagscheduledPublish: true
:
const rabbit = new Rabbit(URL, { scheduledPublish: true });
type Message = { message: string };
type Exception = { message: string } | string;
type Result = { done: boolean };
const worker = new QueueWorker<Message, Exception, Result>(rabbit, 'test', {
jobsCount: 5,
attemptsCount: 10, // set attempts count here
attemptDelays: ['0', '2 min', '5 min'],
// 1st attempt's trying to resolve now
// on fail 1st attempt, 2nd attempt will try to resolve after 2 minutes
// on fail 2nd attempt, all next attempts will try to resolve every 5 minutes
});
worker.handle(async ({ message }) => {
// handle data
console.log(`Hello ${message}`);
return { done: true };
});
// on success
worker.on('success', (data: Message, result: Result) => {
// you can watch success result here
});
// on attempt fail
worker.on('fail', (data: Message, error: Exception) => {
// watch me die
});
// on attempts end
worker.on('error', (data: Message, errors: Exception[]) => {
// at the end of all attempts
});
worker.addItem({ message: 'world' });
// or you can set non-default options for this message
worker.addItem(
{ message: 'world' },
{
attemptsCount: 0,
attemptDelays: ['10 min'],
// this item will be handled after 10 minutes without attempts
},
);
Main features
- Simple queue setup.
- Jobs (
jobsCount
). - Attempts (
attemptsCount
). - Function of repeat on fail after some time (
attemptDelays[attemptNumber]
. If length ofattemptDelays
is <= current number of attempt then it uses last element ofattemptDelays
).
License
Licensed under MIT license