Skip to content

Commit

Permalink
Merge 80e536a into 59bad81
Browse files Browse the repository at this point in the history
  • Loading branch information
raisch committed Dec 11, 2013
2 parents 59bad81 + 80e536a commit 9061497
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
17 changes: 7 additions & 10 deletions lib/agenda.js
Expand Up @@ -101,7 +101,11 @@ Agenda.prototype.schedule = function(when, name, data) {

Agenda.prototype.saveJob = function(job, cb) {
var fn = cb;
var props = getJobProperties(job);

var props = job.toJSON();

delete props._id;

if(props.type == 'single')
this._db.findAndModify({name: props.name, type: 'single'}, {}, {$set: props}, {upsert: true, new: true}, processDbResult);
else {
Expand Down Expand Up @@ -147,7 +151,9 @@ Agenda.prototype.stop = function() {
function processJobs() {
var definitions = this._definitions,
self = this;

var now = new Date();

this.jobs({nextRunAt: {$lte: now}}, {sort: {'priority': -1}}, function(err, jobs) {
if(err) throw(err);
else fillJobQueue();
Expand Down Expand Up @@ -178,12 +184,3 @@ function processJobs() {
}
});
}

function getJobProperties(job) {
var props = {};
for(var key in job.attrs) {
props[key] = job.attrs[key];
}
delete props._id;
return props;
}
15 changes: 15 additions & 0 deletions lib/job.js
Expand Up @@ -23,6 +23,21 @@ var Job = module.exports = function Job(args) {
this.attrs = attrs;
};

Job.prototype.toJSON=function(){ // create a persistable Mongo object -RR
var self=this,
attrs=self.attrs||{};
return {
_id: attrs._id,
name: attrs.name,
data: attrs.data,
priority: attrs.priority,
lastRunAt: attrs.lastRunAt ? new Date(attrs.lastRunAt) : null,
lastFinishedAt:attrs.lastFinishedAt ? new Date(attrs.lastFinishedAt) : null,
nextRunAt: attrs.nextRunAt ? new Date(attrs.nextRunAt) : null,
repeatInterval:attrs.repeatInterval,
type: attrs.type
};
};

Job.prototype.computeNextRunAt = function() {
var interval = this.attrs.repeatInterval;
Expand Down

0 comments on commit 9061497

Please sign in to comment.