Skip to content

Commit

Permalink
fix agenda.every overwriting nextRunAt [closes #70]
Browse files Browse the repository at this point in the history
  • Loading branch information
rschmukler committed Jun 11, 2014
1 parent 0b4e9ea commit 99d30dd
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
25 changes: 25 additions & 0 deletions agenda-test.js
@@ -0,0 +1,25 @@
var connStr = 'mongodb://localhost/agenda-test';
var Agenda = require('agenda');

var agenda = new Agenda({db: { address: connStr } });

agenda.define('5 minutes job', function(job, done) {
console.log("Running 5 minutes job");
done();
});

agenda.define('10 minutes job', function(job, done) {
console.log("Running 10 minutes job");
done();
});

agenda.define('once a day job', function(job, done) {
console.log('ONCE A DAY RUNNING');
done();
});

agenda.every('5 minutes', '5 minutes job');
agenda.every('10 minutes', '10 minutes job');
agenda.every('0 5 * * 1-5', 'once a day job');

agenda.start();
22 changes: 17 additions & 5 deletions lib/agenda.js
Expand Up @@ -170,16 +170,28 @@ Agenda.prototype.saveJob = function(job, cb) {
delete props._id;

props.lastModifiedBy = this._name;

var now = new Date(),
protect = {},
update;

if(props.nextRunAt && props.nextRunAt <= now) {
protect.nextRunAt = props.nextRunAt;
delete props.nextRunAt;
}

update = { $set: props };

if (Object.keys(protect).length > 0) {
update.$setOnInsert = protect;
}

if(id) {
if(props.type == 'single' && props.nextRunAt && props.nextRunAt <= new Date()) {
delete props.nextRunAt;
}
this._db.findAndModify({_id: id}, {}, {$set: props}, {new: true}, processDbResult);
this._db.findAndModify({_id: id}, {}, update, {new: true}, processDbResult);
}
else if(props.type == 'single') {
// Try an upsert.
this._db.findAndModify({name: props.name, type: 'single'}, {}, {$set: props}, {upsert: true, new: true}, processDbResult);
this._db.findAndModify({name: props.name, type: 'single'}, {}, update, {upsert: true, new: true}, processDbResult);
} else {
this._db.insert(props, processDbResult);
}
Expand Down

0 comments on commit 99d30dd

Please sign in to comment.