lykmapipo / kue-scheduler

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

.remove not working for scheduled unqiue jobs #38

Open arashkay opened 8 years ago

arashkay commented 8 years ago

Is there a way to do reschedule a unique job?

I have job which is scheduled to send notifications 1 hour before an activity starts. On updating the activity time I need to update the scheduled job or at least remove it and create a new one.

.remove( { unique: 'activityID1' } ) seems not working as I get this:

{ removedJobInstance: null,
  removedExpiryKey: 0,
  removedJobData: 0 }
lykmapipo commented 7 years ago

@tectual Please share a piece of codes that remove the schedule. But as per #L1231 it have to work.

You may also update kue-scheduler spec to fail on your scenario and i can fix it.

sonnyt commented 6 years ago

@lykmapipo I am experiencing the same issue, is there a work around for this?

syedshahzebhasnain commented 6 years ago

+1 same case with me as well

sonnyt commented 6 years ago

I ended up just using the .delay() option of Kue. Storing the jobId and then when I need to update it:

kue.Job.get(JOBID, (err, job) => {
  job.set('created_at', new Date().getTime());
  job.delay(new Date()).save();
});

I hope this helps.

respinha commented 6 years ago

@lykmapipo This happens to me whenever I attempt to remove jobs that haven't been processed yet, which is a perfectly valid use case: I could want to delete a job before its first-ever execution. In that case, it should be possible to perform such operation through the 'unique' / 'jobDataKey' keyword. And the line you quoted (#L1231) does not reflect the use case @tectual is exposing. #L1256 is the criteria used in this case. And the problem here is that if unique or jobDataKey are provided, the next() function is called with job as null, so the job instance is never deleted in the finish() function. Again, this problem only occurs when the job has not been processed for the first time.

Example code:

const redisConfig = {
  "host": "localhost",
  "port": 6379
};
const kue = require('kue');
const queue = require('kue-scheduler').createQueue({
  redis: redisConfig
});

// schedule time is 20 seconds from now
let d = new Date();
d.setSeconds(d.getSeconds() + 20);

// creating a job
var job = queue
  .createJob('example')
  .attempts(3)
  .priority('normal')
  .unique('unique_every');

queue.schedule(d.toISOString(), job);

queue.process('example', function (job, done) {
  console.log('Job processed');
  done();
});

// attempting to remove the job before its first execution
queue.remove({
  unique: 'unique_every'
}, (err, result) => {
  console.log('Error', err);
  console.log('Result', result);
})

Output:

Error null
Result { removedJobInstance: null,
  removedExpiryKey: 0,
  removedJobData: 0 }
Job processed

There should be a way of removing the job by its unique key before it got processed, right?