How to Offload a Process to a Worker
To offload a process to a background worker, follow these steps. Start by identifying the category your process belongs to. If a queue for that category already exists, reuse it. Otherwise, create a new queue and its associated worker.
๐ง Creating a New Queue and Workerโ
-
Name Your Queue and Job
- Choose a descriptive and unique name for your queue. Keep in mind it might handle similar jobs.
- Define a descriptive and unique name for the job that represents your process.
-
Add the Job to the Queue
- In your controller, instantiate the queue and add the job using the names defined above.
-
Register the Queue in the Dashboard
- In
dashboard.controller.ts
, register your new queue so it appears in the monitoring dashboard.
- In
-
Create a Worker Class
- Create a new class that encapsulates a
BullMQ
'sWorker
. - Ensure the worker starts immediately when the class is instantiated, and is configured to listen only to the specific job name defined earlier.
- The worker should contain the logic to handle the job and any error handling or logging as needed.
- Instantiate the worker class in
workers/entrypoint.ts
.
- Create a new class that encapsulates a
๐ Reusing an Existing Queueโ
-
Define the Job Name
- Choose a clear and unique job name that accurately describes the new job.
-
Add the Job to the Queue
- In your controller, reuse the existing queue and add your new job.
-
Update the Worker
- Modify the existing worker class for that queue so it can handle your new job.