bullbullmqbull-queue

Bull - Triggering multiple functions on a single queue


i would like to create one Bull Queue, and put function in queue? is it possible.

for example: I have

http request /func1 + params -> start func1(params)

http request /func2 + params -> start func2(params)

http request /func3 + params -> start func3(params)

Queue name: MyQueue

How can i process func1(params), func2(params), func3(params) in MyQueue.

i do not know how to queue.add function.

import Queue from "bull";
const queue = new Queue("myQueue");
const main = async () => {
  await queue.add({ name: "John", age: 30 });
};
queue.process((job, done) => {
  console.log(job.data);
  done();
});
main().catch(console.error);

tnx a lot


Solution

  • When publishing jobs to the queue you can add parameter
    and based on that handle them differently

    import express from "express";
    import Queue from "bull";
    
    const router = express.Router();
    const queue = new Queue("myQueue");
    
    router.get("/func1", function (req, res) {
      queue.add({ func: "func1", ...req.query.params})
    });
    
    router.get("/func2", function (req, res) {
      queue.add({ func: "func2", ...req.query.params})
    });
    
    router.get("/func3", function (req, res) {
      queue.add({ func: "func3", ...req.query.params})
    });
    
    queue.process((job, done) => {
      switch (job.data.func) {
        case "func1":
          // do something
          break;
        case "func2":
          // do something else
          break;
        case "func3":
          // do something else
          break;
        default:
          throw new Error(`unexpected func=${job.data.func}`);
      done();
    });