queue-worker
TypeScript icon, indicating that this package has built-in type declarations

2.1.0 • Public • Published

queue-worker

RabbitMQ queue worker.

How to use?

  1. To install package run the command npm install rabbit-queue queue-worker --save
  2. Rabbit should be initialized with flag scheduledPublish: 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

  1. Simple queue setup.
  2. Jobs (jobsCount).
  3. Attempts (attemptsCount).
  4. Function of repeat on fail after some time (attemptDelays[attemptNumber]. If length of attemptDelays is <= current number of attempt then it uses last element of attemptDelays).

License

Licensed under MIT license

Dependents (0)

Package Sidebar

Install

npm i queue-worker

Weekly Downloads

1

Version

2.1.0

License

MIT

Unpacked Size

27.7 kB

Total Files

6

Last publish

Collaborators

  • aristocr2t