Version: v0.0.7

Creating Jobs

Now, that the queue is configured. We need to create jobs. You can think of jobs as piece of code which will run on the other end of the queue. Technically, we dispatch jobs that will be pushed into the queue and be processed by the queue worker.

Now, to create job, this package packs a @Job decorator inside any @Injectable provider.

note

This package uses DiscoveryService internally to track all jobs. So, it is a must that you define the job inside an Injectable provider only. Else the jobs will not be registered.

Create Job

import { Job } from '@squareboat/nest-queue';
import { Injectable } from '@nestjs/common';
@Injectable()
export class NotificationService {
@Job('SEND_MAIL')
sampleMethod(data: Record<string, any>) {
console.log('data coming ===> ', data);
// ...add your logic here
}
}

Notice the @Job('SEND_MAIL'). SEND_MAIL is the job name which we will be using when dispatching jobs.

Options

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:

@Job('SEND_MAIL', {connection: "transactional-emails"})

queue

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

@Job('SEND_MAIL', {queue: 'payment-emails'})

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.

@Job('SEND_MAIL', {tries: 3})

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.

@Job('SEND_MAIL', {delay: 60})
info

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