Automattic / kue

Kue is a priority job queue backed by redis, built for node.js.
http://automattic.github.io/kue
MIT License
9.45k stars 862 forks source link

Waiting for promises to resolve before running next job? #1236

Open Popesites opened 4 years ago

Popesites commented 4 years ago

My application is structured somewhat like this:

`

router.get('/', (req, res) => {
  const node = req.body.node;
  const operation = req.body.operation;
  const value = req.body.value;
  const ids = req.body.ids;
  queue
    .create(node, {
        title: "Pausing",
        data: {
            node,
            operation,
            value,
            ids
        }
    })
    .priority("high")
    .save();
  queue.process(node, (job, done) => {
    axios.get(`https://backend-service.com/test`, job.data)
        .then(res => {
            console.log(res.status);
            done();
        })
  });
res.send("Hello World!");
});

`

Here's what the backend-service test endpoint is:

`

router.get('/test', async (req, res, next) => {
  const p = new Promise(resolve => {
    setTimeout(() => {
      console.log(req.body);
      resolve();
    }, 10000);
  });
  await p;
  return res.sendStatus(200);
});

`

The issue is that when I call this endpoint twice, with the same node parameter, what happens is it'll wait 10 seconds, then it'll console.log the response status twice right after one another.

What I want to happen is once the API is called twice, the first job should run, wait 10 seconds, then complete, then the second job should begin. Right now, both jobs are beginning at the same time essentially.