Aller au contenu principal

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

  1. 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.
  2. Add the Job to the Queue

    • In your controller, instantiate the queue and add the job using the names defined above.
  3. Register the Queue in the Dashboard

    • In dashboard.controller.ts, register your new queue so it appears in the monitoring dashboard.
  4. Create a Worker Class

    • Create a new class that encapsulates a BullMQ's Worker.
    • 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.

🔁 Reusing an Existing Queue

  1. Define the Job Name

    • Choose a clear and unique job name that accurately describes the new job.
  2. Add the Job to the Queue

    • In your controller, reuse the existing queue and add your new job.
  3. Update the Worker

    • Modify the existing worker class for that queue so it can handle your new job.