diamondio/better-queue

Precondition delay and retry failure.

Closed this issue · 2 comments

The way i've read the documentation is that the precondition function will be called until it passes and there are objects in the queue. Also, precondition retry will be delayed by whatever the timeout is.

It looks as if every time an object is added to the queue, it calls the precondition function, ignoring the delay.

In addition to the delay being ignored, my code breaks the queue from calling the precondition infinitely and i'm having a hard time figuring out why.

I've setup a queue with the following code:

receiptQueue = new Queue(function (data, cb) {
        printService.print(data)
            .then(function () {
                cb(null, "ok");
            }).catch(function (err) {
            cb(err);
        });
    }, {
        afterProcessDelay: 500,
        maxRetries: 12, retryDelay: 5000,
        precondition: function (cb) {
            printService.isOnline()
                .then(function (result) {
                    if (result) {
                        console.log("Online");
                        cb(null, true);
                    } else {
                        console.log("Offline");
                        cb(null, false);
                    }
                })

        }, preconditionRetryTimeout: 1000
    }
);

I'm using the latest better-queue and node v8.

Yes, precondition doesn't wait for a delay. The way the afterProcessDelay works is that it waits the 500ms after processing a task. There's batchDelay that waits before, but if there are more tasks in the queue it will not wait.

You can experiment by running this code

const bq = require('better-queue');
let cond = false;
const q = new bq((data, cb) => {
  console.log(data);
  cb();
}, {
  afterProcessDelay: 500,
  maxRetries: 12,
  retryDelay: 5000,
  precondition: (cb) => {
    console.log('check precond');
    cb(null, cond);
  },
  preconditionRetryTimeout: 1000,
});

q.push(1);
q.push(2);
q.push(3);

setTimeout(() => (cond = true), 5000);

It seems to be working properly.

This does work for me and it makes sense.