Workers
Workers are the actual instances that perform some job based on the jobs that are added in the queue. A worker is equivalent to a "message" receiver in a traditional message queue. The worker's duty is to complete the job. If it succeeds, the job will be moved to the "completed" status. If the worker throws an exception during its processing, the job will automatically be moved to the "failed" status.
Failed jobs can be automatically retried, see Retrying failing jobs
A worker is instantiated with the Worker
class, and the work itself will be performed in the process function. Process functions are meant to be asynchronous, using either the async
keyword or returning a promise.
When a worker instance is created, it launches the processor immediately
In order to decide when your processor should start its execution, pass autorun: false
as part of worker options:
Note that a processor can optionally return a value. This value can be retrieved either by getting the job and accessing the returnvalue
property or by listening to the completed
event:
Progress
Inside the worker process function it is also possible to emit progress events. Calling job.progress
you can specify a number or an object if you have more complex needs. The progress
event can be listened for in the same way as the completed
event:
Finally, when the process fails with an exception it is possible to listen for the failed
event too:
It is also possible to listen to global events in order to get notifications of job completions, progress and failures:
Finally, you should attach an error listener to your worker to avoid NodeJS raising an unhandled exception when an error occurs. For example:
If the error handler is missing, your worker may stop processing jobs when an error is emitted! Find more info here.
Typescript typings
It is also possible to specify the data types for the Job data and return value using generics:
Read more:
Last updated