Version: v0.0.7

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 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.