Automattic/kue

Waiting for promises to resolve before running next job?

Popesites opened this issue · 0 comments

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.