Version: Next

Dispatch Jobs

After creating jobs, we will now need to dispatch it. Dispatching a job basically means pushing it to the queue(sqs, redis or sync etc.) so that it can be processed by the queue worker.

To dispatch a job, you can simply do:

Using helper function

import { Injectable } from '@nestjs/common';
import { Dispatch } from '@squareboat/nest-queue';
@Injectable()
export class PaymentService {
async verify(inputs: Record<string, any>): Promise<Record<string, any>> {
// ...your custom code here
Dispatch({
job: 'SEND_MAIL',
data: {
email: 'abcdefgh@gmail.com',
subject: 'Yay! Your payment is succesful!',
},
});
}
}

Notice the Dispatch function call, we are passing two attributes:

  • job: Name of the job that we want to run when this payload is received by the queue worker. In our case, 'SEND_MAIL'
  • data: Payload that we want to pass to the job. Any data that you pass here will be received by the job as its argument.

Since, the payload is serialized while pushing it to the queue, whatever type of object that you are passing will be serialized and pushed. Jobs will receive POJO/string/number as their argument.

For example, if you are passing a class instance, that will be converted into a POJO and pushed to the queue. In the job also, POJO will be received.

Using Queue class

You can use class, to dispatch jobs in a more declarative way

import { Injectable } from '@nestjs/common';
import { Queue } from '@squareboat/nest-queue';
@Injectable()
export class PaymentService {
async verify(inputs: Record<string, any>): Promise<Record<string, any>> {
// ...your custom code here
Queue.dispatch({
job: 'SEND_MAIL',
data: {
email: 'abcdefgh@gmail.com',
subject: 'Yay! Your payment is succesful!',
},
});
}
}

Attributes are same as above.

Options

caution

All the options passed while dispatching the job will override all the default options and the options defined in @Job

connection

We understand that you may have multiple queue connections to handle in your application. While configuring the module, we set default connection. Incase, you want to dispatch the job on a different connection, you can do:

Dispatch({
job: 'SEND_MAIL',
connection: 'transactional-emails',
data: {
email: 'abcdefgh@gmail.com',
subject: 'Yay! Your payment is succesful!',
},
});

queue

If you want to dispatch job to a different queue but on default connection, you can pass the queue attribute.

Dispatch({
job: 'SEND_MAIL',
queue: 'payment-emails',
data: {
email: 'abcdefgh@gmail.com',
subject: 'Yay! Your payment is succesful!',
},
});

tries

This package provides out-of-the-box retrial logic, so that incase if any of the job throws any error, they will be retried a specific number of times. Default being 0.

Dispatch({
job: 'SEND_MAIL',
tries: 3,
data: {
email: 'abcdefgh@gmail.com',
subject: 'Yay! Your payment is succesful!',
},
});

If the SEND_MAIL job throws any error, worker will again push the job to the queue and re-run it again. Once the maximum number of retries are exhausted, the job will be discarded as it is.

delay

There can be some situations where you may want to delay the job by sometime. For example, you may want to delay the job by 60 seconds, ie the job will become available to the queue worker once the delay period has been elapsed.

Dispatch({
job: 'SEND_MAIL',
delay: 60, // in seconds
data: {
email: 'abcdefgh@gmail.com',
subject: 'Yay! Your payment is succesful!',
},
});
info

If you are using sqs as the driver, the maxium allowed delay is 15 mins.