lykmapipo / kue-scheduler

A job scheduler utility for kue, backed by redis and built for node.js
246 stars 47 forks source link

Unique recurring jobs are duplicated when they are restored #90

Closed schmod closed 7 years ago

schmod commented 7 years ago

When queues are restored, we sometimes end up with more instances of a unique job than we started with.

For example, a job that is scheduled to run every 2 seconds ends up running every second when we restore its queue:

const kue = require('./index');

const Queue1 = kue.createQueue();
Queue1.clear();

var backoff = {
  delay: 60000,
  type: 'fixed'
};

var job = Queue1
  .createJob('unique_every', {})
  .attempts(3)
  .backoff(backoff)
  .priority('normal')
  .unique('every_mail');

Queue1.every('2 seconds', job);

Queue1.process('unique_every', function (job, finalize) {
  console.log('Queue 1: ' + new Date());
  finalize();
});

setTimeout(function() {
  const Queue2 = kue.createQueue();
  Queue2.restore();
  Queue2.process('unique_every', function (job, finalize) {
    console.log('Queue 2: ' + new Date());
    finalize();
  });
}, 3000);

Output:

Queue 1: Mon Mar 27 2017 12:14:15 GMT-0400 (EDT)
Queue 2: Mon Mar 27 2017 12:14:16 GMT-0400 (EDT)
Queue 1: Mon Mar 27 2017 12:14:17 GMT-0400 (EDT)
Queue 2: Mon Mar 27 2017 12:14:18 GMT-0400 (EDT)
Queue 1: Mon Mar 27 2017 12:14:19 GMT-0400 (EDT)
Queue 2: Mon Mar 27 2017 12:14:20 GMT-0400 (EDT)

Note that the job is clearly running every second instead of every 2 seconds.

lykmapipo commented 7 years ago

@schmod To test this.

  1. Use https://github.com/lykmapipo/kue-scheduler/blob/master/test/restore.spec.js

  2. Create a new describe block.

Lets start with that then we will improve for other scenarios.

Hope it helps.