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
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:
queue
If you want to dispatch job to a different queue but on default
connection, you can pass the queue attribute.
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.
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.
info
If you are using sqs as the driver, the maxium allowed delay is 15 mins.