Version: v0.0.13

Run Queue Workers

Nest Queue provides a clean and maintaible API to manage message queues over several popular queue brokers like aws sqs, redis(coming soon), rabbitmq(coming soon).

note

You don't need to run QueueWorker if you are using sync driver.

To perform action on any queue, you can make use of QueueWorker class. To initialize,

const worker = QueueWorker.init({connection: "default", queue:"default-queue", sleep: 10});

Note that if any of the value is not passed, then default setting for the missing property will be used as fallback.

info

If you are using multiple queues/connections in your application, then you will have to run different queue worker instances for each queue/connection.

Functions

Run Listener

Now that we have created jobs and dispatched them, it is now necessary that we run the queue worker which will start pulling jobs from queues and process them.

await worker.listen()

Purge Queue

You may want to clear(purge) the queue, you can do so by calling clear method.

await worker.purge()

Running

There are several ways through which you may want to run queue workers,

  • via npm script
  • [COMING SOON] custom cli commands (uses @squareboat/nest-console) internally.

via @squareboat/nest-console

The package already contains predefined commands to help you easily run the queue workers and other actions.

Following commands are supported out of the box

CommandDescription
queue:workCommand to run the queue worker, starts processing the jobs
queue:lengthCommand to get the length of the specified queue
queue:purgeCommand to purge the queue

All the commands expect sleep, queue, connection attributes. If none of the attributes are passed, default configurations options will be assumed.

To use these commands, you need to install @squareboat/nest-console package and simply import the ConsoleModule inside AppModule.

info

For more information on various usage of @squareboat/nest-console, you can read about it here.

Now, simply do

node cli queue:work --queue=MyQueue

via npm script

You can simply create your own typescript script like below

src/queue/listenDefault.ts
import { QueueListener } from '@lib/queue';
async function listenQueue({
connection,
queue,
sleep,
}: {
connection?: string;
sleep?: Number;
queue?: string;
}) {
const options = {};
if (sleep) options['sleep'] = sleep;
if (connection) options['connection'] = connection;
if (queue) options['queue'] = queue;
await QueueListener.init(options).run();
return;
}
listenQueue({ connection: 'default', sleep: 10 });

Now, do the following change in your package.json

{
"scripts": {
"queue:default": "node dist/src/queue/listenDefault.js"
}
}

Similarly for the purge function as well, you can create different commands.