| 1 | 1 | var Job = require('./job.js'), |
| 2 | | humanInterval = require('human-interval'), |
| 3 | | mongo = require('mongoskin'); |
| 4 | | |
| 5 | 1 | var Agenda = module.exports = function(config) { |
| 6 | 3 | if(!(this instanceof Agenda)) return new Agenda(config); |
| 7 | 1 | if(!config) config = {}; |
| 8 | 1 | this._processEvery = humanInterval(config.processEvery) || humanInterval('5 seconds'); |
| 9 | 1 | this._defaultConcurrency = config.defaultConcurrency || 5; |
| 10 | 1 | this._maxConcurrency = config.maxConcurrency || 20; |
| 11 | 1 | this._definitions = {}; |
| 12 | 1 | this._runningJobs = 0; |
| 13 | 1 | if(config.db) |
| 14 | 1 | this.database(config.db.address, config.db.collection); |
| 15 | | }; |
| 16 | | |
| 17 | | // Configuration Methods |
| 18 | | |
| 19 | 1 | Agenda.prototype.database = function(url, collection) { |
| 20 | 4 | collection = collection || 'agendaJobs'; |
| 21 | 4 | this._db = mongo.db(url, {w: 0}).collection(collection); |
| 22 | 4 | return this; |
| 23 | | }; |
| 24 | | |
| 25 | 1 | Agenda.prototype.processEvery = function(time) { |
| 26 | 3 | this._processEvery = humanInterval(time); |
| 27 | 3 | return this; |
| 28 | | }; |
| 29 | | |
| 30 | 1 | Agenda.prototype.maxConcurrency = function(num) { |
| 31 | 2 | this._maxConcurrency = num; |
| 32 | 2 | return this; |
| 33 | | }; |
| 34 | | |
| 35 | 1 | Agenda.prototype.defaultConcurrency = function(num) { |
| 36 | 2 | this._defaultConcurrency = num; |
| 37 | 2 | return this; |
| 38 | | }; |
| 39 | | |
| 40 | | // Job Methods |
| 41 | 1 | Agenda.prototype.create = function(name, data) { |
| 42 | 14 | var priority = this._definitions[name] ? this._definitions[name].priority : 0; |
| 43 | 14 | var job = new Job({name: name, data: data, type: 'normal', priority: priority, agenda: this}); |
| 44 | 14 | return job; |
| 45 | | }; |
| 46 | | |
| 47 | 1 | Agenda.prototype.jobs = function() { |
| 48 | 2 | var args = Array.prototype.slice.call(arguments); |
| 49 | 2 | var self = this; |
| 50 | | |
| 51 | 2 | if(typeof args[args.length -1] == 'function') { |
| 52 | 2 | var fn = args.pop(); |
| 53 | 2 | var wrapJobs = function(err, jobs) { |
| 54 | 2 | if(err) fn(err, null); |
| 55 | | else { |
| 56 | 2 | jobs = jobs.map(function(j) { |
| 57 | 2 | j.agenda = self; |
| 58 | 2 | return new Job(j); |
| 59 | | }); |
| 60 | 2 | fn(err, jobs); |
| 61 | | } |
| 62 | | }; |
| 63 | 2 | args.push(wrapJobs); |
| 64 | | } |
| 65 | | |
| 66 | 2 | return this._db.findItems.apply(this._db, args); |
| 67 | | }; |
| 68 | | |
| 69 | 1 | Agenda.prototype.define = function(name, options, processor) { |
| 70 | 5 | if(!processor) { |
| 71 | 4 | processor = options; |
| 72 | 4 | options = {}; |
| 73 | | } |
| 74 | 5 | this._definitions[name] = { |
| 75 | | fn: processor, |
| 76 | | concurrency: options.concurrency || this._defaultConcurrency, |
| 77 | | priority: options.priority || 0, |
| 78 | | running: 0 |
| 79 | | }; |
| 80 | | }; |
| 81 | | |
| 82 | 1 | Agenda.prototype.every = function(interval, name, data) { |
| 83 | 4 | var job; |
| 84 | 4 | job = this.create(name, data); |
| 85 | 4 | job.attrs.type = 'single'; |
| 86 | 4 | job.repeatEvery(interval); |
| 87 | 4 | job.save(); |
| 88 | 4 | return job; |
| 89 | | }; |
| 90 | | |
| 91 | 1 | Agenda.prototype.schedule = function(when, name, data) { |
| 92 | 2 | var job = this.create(name, data); |
| 93 | 2 | job.schedule(when); |
| 94 | 2 | job.save(); |
| 95 | 2 | return job; |
| 96 | | }; |
| 97 | | |
| 98 | 1 | Agenda.prototype.saveJob = function(job, cb) { |
| 99 | 13 | var fn = cb; |
| 100 | 13 | var props = getJobProperties(job); |
| 101 | 13 | if(props.type == 'single') |
| 102 | 5 | this._db.findAndModify({name: props.name, type: 'single'}, {}, {$set: props}, {upsert: true, new: true}, processDbResult); |
| 103 | | else { |
| 104 | 8 | var query = {}; |
| 105 | 8 | if(job.attrs._id) query._id = job.attrs._id; |
| 106 | 8 | this._db.findAndModify(query, {$set: props}, {upsert: true, new: true}, processDbResult); |
| 107 | | } |
| 108 | | |
| 109 | 13 | function processDbResult(err, res) { |
| 110 | 12 | if(err) throw(err); |
| 111 | 12 | else if(res && typeof res == 'object') |
| 112 | 12 | job.attrs._id = res._id; |
| 113 | | |
| 114 | 12 | if(fn) |
| 115 | 5 | fn(err, job); |
| 116 | | } |
| 117 | | }; |
| 118 | | |
| 119 | | // Job Flow Methods |
| 120 | | |
| 121 | 1 | Agenda.prototype.start = function() { |
| 122 | 1 | var self = this; |
| 123 | 1 | this._processInterval = setInterval(function() { |
| 124 | 1 | processJobs.call(self); |
| 125 | | }, this._processEvery); |
| 126 | | }; |
| 127 | | |
| 128 | 1 | Agenda.prototype.stop = function() { |
| 129 | 1 | clearInterval(this._processInterval); |
| 130 | 1 | this._processInterval = undefined; |
| 131 | | }; |
| 132 | | |
| 133 | 1 | function processJobs() { |
| 134 | 1 | var definitions = this._definitions, |
| 135 | | self = this; |
| 136 | 1 | var now = new Date(); |
| 137 | 1 | this.jobs({nextRunAt: {$lte: now}}, {sort: {'priority': -1}}, function(err, jobs) { |
| 138 | 1 | if(err) throw(err); |
| 139 | 1 | else fillJobQueue(); |
| 140 | | |
| 141 | 1 | function fillJobQueue() { |
| 142 | 1 | if(!jobs.length) return; |
| 143 | 1 | while(self._runningJobs < self._maxConcurrency) { |
| 144 | 2 | var job = getNextProcessableJob(); |
| 145 | 2 | if(job) { |
| 146 | 1 | job.run(fillJobQueue); |
| 147 | | } else |
| 148 | 1 | break; |
| 149 | | } |
| 150 | | } |
| 151 | | |
| 152 | 1 | function getNextProcessableJob() { |
| 153 | 2 | var definition, job, index; |
| 154 | 2 | for(index = 0; index < jobs.length; ++index) { |
| 155 | 1 | definition = definitions[jobs[index].attrs.name]; |
| 156 | 1 | if(definition.concurrency > definition.running) { |
| 157 | 1 | job = jobs[index]; |
| 158 | 1 | break; |
| 159 | | } |
| 160 | | } |
| 161 | 3 | if(job) return jobs.splice(index, 1)[0]; |
| 162 | 1 | return undefined; |
| 163 | | } |
| 164 | | }); |
| 165 | | } |
| 166 | | |
| 167 | 1 | function getJobProperties(job) { |
| 168 | 13 | var props = {}; |
| 169 | 13 | for(var key in job.attrs) { |
| 170 | 119 | props[key] = job.attrs[key]; |
| 171 | | } |
| 172 | 13 | delete props._id; |
| 173 | 13 | return props; |
| 174 | | } |
| 175 | | |