kue-scheduler does not Save unique data until first run of job.
Opened this issue · 2 comments
I was wrapping a pre-check for an existing job with the same unique name when I noticed a behavior I think I understand the reason for:
If you create a job:
var kue = require('kue-scheduler');
var Queue = kue.createQueue();
var client = Queue.client;
//create a job instance
var job = Queue
.createJob('myjob', {'foo':'bar'})
.attempts(3)
.priority('normal')
.unique('testUnique');
//schedule it to run every 5 seconds
Queue.every('five seconds', job,function(err,result){
client.exists('q:unique:jobs', function(err, reply) {
if (reply === 1) {
console.log('Immediately exists');
} else {
console.log('doesn\'t exist');
}
//wait until the job has been scheduled for the first time
setTimeout(function(){
client.exists('q:unique:jobs', function(err, reply) {
if (reply === 1) {
console.log('exists after scheduling');
} else {
console.log('doesn\'t exist');
}
});
},6000);
});
}
);
So I noticed this, and I'm not sure how to fix it, but it does to at least be documented - as I was struggling with why if I looked in q:unique:jobs, the job would not exist until it is scheduled for the first time, but I see now that save does not get called until the first scheduled run of the job.
This creates an opportunity where you could create two jobs with the same unique constraint and it would depend on which job was scheduled last(?) which job would win in being the 'unique' job, correct?
@gmcnaught This is because kue-scheduler
rely much on redis working to achieve what it does in scheduling every
jobs. So if you say a job to run every five seconds
it will wait until five second for it to create a job.
In case of two job with the same unique constraint am planning to move on using redis data structure
that fit for the work than just a stringified objects.
Hope we can work on this to improve documentation and implementing the desired feature.