Skip to content

Commit

Permalink
Merge 254a8d3 into 3fa4791
Browse files Browse the repository at this point in the history
  • Loading branch information
Albert-IV committed Feb 11, 2015
2 parents 3fa4791 + 254a8d3 commit 1be9deb
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions lib/agenda.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var Agenda = module.exports = function(config) {
if (config.db) {
this.database(config.db.address, config.db.collection, config.db.options);
} else if (config.mongo) {
this._db = config.mongo;
this._db = config.mongo;
}
};

Expand Down Expand Up @@ -183,16 +183,15 @@ Agenda.prototype.saveJob = function(job, cb) {
delete props.unique;

props.lastModifiedBy = this._name;

var now = new Date(),
protect = {},
update = { $set: props };


if (id) {
this._db.findAndModify({_id: id}, {}, update, {new: true}, processDbResult);
}
else if (props.type == 'single') {
} else if (props.type == 'single') {
if (props.nextRunAt && props.nextRunAt <= now) {
protect.nextRunAt = props.nextRunAt;
delete props.nextRunAt;
Expand Down Expand Up @@ -314,7 +313,7 @@ function unlockJobs(done) {
function getJobId(j) {
return j.attrs._id;
}

var jobIds = this._jobQueue.map(getJobId)
.concat(this._runningJobs.map(getJobId));
this._db.update({_id: { $in: jobIds } }, { $set: { lockedAt: null } }, {multi: true}, done);
Expand All @@ -324,7 +323,7 @@ function processJobs(extraJob) {
if (!this._processInterval) {
return;
}

var definitions = this._definitions,
jobName,
jobQueue = this._jobQueue,
Expand Down Expand Up @@ -354,7 +353,12 @@ function processJobs(extraJob) {
}

if (job) {
jobQueue.unshift(job);
if( Array.isArray(job) ) {
jobQueue = job.concat(jobQueue);
} else {
jobQueue.unshift(job);
}

jobQueueFilling(name);
jobProcessing();
}
Expand All @@ -374,8 +378,7 @@ function processJobs(extraJob) {

if (job.attrs.nextRunAt < now) {
runOrRetry();
}
else {
} else {
setTimeout(runOrRetry, job.attrs.nextRunAt - now);
}

Expand Down

0 comments on commit 1be9deb

Please sign in to comment.