From bbc4d824a4294409a61650a622d6720354312b32 Mon Sep 17 00:00:00 2001 From: Tj Holowaychuk Date: Mon, 8 Aug 2011 09:50:21 -0700 Subject: [PATCH] state / status inconsistency --- lib/http/index.js | 4 +-- lib/http/routes/json.js | 18 +++++----- lib/kue.js | 25 +++++++------ lib/queue/job.js | 78 ++++++++++++++++++++--------------------- 4 files changed, 62 insertions(+), 63 deletions(-) diff --git a/lib/http/index.js b/lib/http/index.js index 9ad3d759..d4d38f59 100644 --- a/lib/http/index.js +++ b/lib/http/index.js @@ -53,8 +53,8 @@ app.use(express.static(__dirname + '/public')); app.get('/stats', provides('json'), json.stats); app.get('/job/search', provides('json'), json.search); app.get('/jobs/:from..:to/:order?', provides('json'), json.jobRange); -app.get('/jobs/:type/:status/:from..:to/:order?', provides('json'), json.jobTypeRange); -app.get('/jobs/:status/:from..:to/:order?', provides('json'), json.jobStatusRange); +app.get('/jobs/:type/:state/:from..:to/:order?', provides('json'), json.jobTypeRange); +app.get('/jobs/:state/:from..:to/:order?', provides('json'), json.jobStateRange); app.get('/job/types', provides('json'), json.types); app.get('/job/:id', provides('json'), json.job); app.get('/job/:id/log', provides('json'), json.log); diff --git a/lib/http/routes/json.js b/lib/http/routes/json.js index f7eed88b..04852ee5 100644 --- a/lib/http/routes/json.js +++ b/lib/http/routes/json.js @@ -60,7 +60,7 @@ exports.types = function(req, res){ */ exports.jobRange = function(req, res){ - var status = req.params.status + var state = req.params.state , from = parseInt(req.params.from, 10) , to = parseInt(req.params.to, 10) , order = req.params.order; @@ -72,33 +72,33 @@ exports.jobRange = function(req, res){ }; /** - * Get jobs by :status, and range :from..:to. + * Get jobs by :state, and range :from..:to. */ -exports.jobStatusRange = function(req, res){ - var status = req.params.status +exports.jobStateRange = function(req, res){ + var state = req.params.state , from = parseInt(req.params.from, 10) , to = parseInt(req.params.to, 10) , order = req.params.order; - Job.rangeByStatus(status, from, to, order, function(err, jobs){ + Job.rangeByState(state, from, to, order, function(err, jobs){ if (err) return res.send({ error: err.message }); res.send(jobs); }); }; /** - * Get jobs by :type, :status, and range :from..:to. + * Get jobs by :type, :state, and range :from..:to. */ exports.jobTypeRange = function(req, res){ var type = req.params.type - , status = req.params.status + , state = req.params.state , from = parseInt(req.params.from, 10) , to = parseInt(req.params.to, 10) , order = req.params.order; - Job.rangeByType(type, status, from, to, order, function(err, jobs){ + Job.rangeByType(type, state, from, to, order, function(err, jobs){ if (err) return res.send({ error: err.message }); res.send(jobs); }); @@ -157,7 +157,7 @@ exports.updateState = function(req, res){ Job.get(id, function(err, job){ if (err) return res.send({ error: err.message }); - job.status(state); + job.state(state); job.save(function(err){ if (err) return res.send({ error: err.message }); res.send({ message: 'updated state' }); diff --git a/lib/kue.js b/lib/kue.js index f1c5533f..ce935991 100644 --- a/lib/kue.js +++ b/lib/kue.js @@ -198,31 +198,30 @@ Queue.prototype.types = function(fn){ }; /** - * Return job ids for the given `status`, and - * callback `fn(err, ids)`. + * Return job ids with the given `state`, and callback `fn(err, ids)`. * - * @param {String} status + * @param {String} state * @param {Function} fn * @return {Queue} for chaining * @api public */ -Queue.prototype.status = function(status, fn){ - this.client.zrange('q:jobs:' + status, 0, -1, fn); +Queue.prototype.state = function(state, fn){ + this.client.zrange('q:jobs:' + state, 0, -1, fn); return this; }; /** - * Get cardinality of `status` and callback `fn(err, n)`. + * Get cardinality of `state` and callback `fn(err, n)`. * - * @param {String} status + * @param {String} state * @param {Function} fn * @return {Queue} for chaining * @api public */ -Queue.prototype.card = function(status, fn){ - this.client.zcard('q:jobs:' + status, fn); +Queue.prototype.card = function(state, fn){ + this.client.zcard('q:jobs:' + state, fn); return this; }; @@ -231,7 +230,7 @@ Queue.prototype.card = function(status, fn){ */ Queue.prototype.complete = function(fn){ - return this.status('complete', fn); + return this.state('complete', fn); }; /** @@ -239,7 +238,7 @@ Queue.prototype.complete = function(fn){ */ Queue.prototype.failed = function(fn){ - return this.status('failed', fn); + return this.state('failed', fn); }; /** @@ -247,7 +246,7 @@ Queue.prototype.failed = function(fn){ */ Queue.prototype.inactive = function(fn){ - return this.status('inactive', fn); + return this.state('inactive', fn); }; /** @@ -255,7 +254,7 @@ Queue.prototype.inactive = function(fn){ */ Queue.prototype.active = function(fn){ - return this.status('active', fn); + return this.state('active', fn); }; /** diff --git a/lib/queue/job.js b/lib/queue/job.js index 77676b64..7ea29722 100644 --- a/lib/queue/job.js +++ b/lib/queue/job.js @@ -100,10 +100,10 @@ exports.range = function(from, to, order, fn){ }; /** - * Get jobs of `status`, with the range `from`..`to` + * Get jobs of `state`, with the range `from`..`to` * and invoke callback `fn(err, ids)`. * - * @param {String} status + * @param {String} state * @param {Number} from * @param {Number} to * @param {String} order @@ -111,16 +111,16 @@ exports.range = function(from, to, order, fn){ * @api public */ -exports.rangeByStatus = function(status, from, to, order, fn){ - pool.alloc().zrange('q:jobs:' + status, from, to, get(fn, order)); +exports.rangeByState = function(state, from, to, order, fn){ + pool.alloc().zrange('q:jobs:' + state, from, to, get(fn, order)); }; /** - * Get jobs of `type` and `status`, with the range `from`..`to` + * Get jobs of `type` and `state`, with the range `from`..`to` * and invoke callback `fn(err, ids)`. * * @param {String} type - * @param {String} status + * @param {String} state * @param {Number} from * @param {Number} to * @param {String} order @@ -128,8 +128,8 @@ exports.rangeByStatus = function(status, from, to, order, fn){ * @api public */ -exports.rangeByType = function(type, status, from, to, order, fn){ - pool.alloc().zrange('q:jobs:' + type + ':' + status, from, to, get(fn, order)); +exports.rangeByType = function(type, state, from, to, order, fn){ + pool.alloc().zrange('q:jobs:' + type + ':' + state, from, to, get(fn, order)); }; /** @@ -156,7 +156,7 @@ exports.get = function(id, fn){ job._progress = hash.progress; job._attempts = hash.attempts; job._max_attempts = hash.max_attempts; - job.state = hash.state; + job._state = hash.state; job._error = hash.error; job.created_at = hash.created_at; job.updated_at = hash.updated_at; @@ -183,7 +183,7 @@ exports.remove = function(id, fn){ exports.get(id, function(err, job){ if (err) return fn(err); if (!job) return fn(new Error('failed to find job ' + id)); - job.removeStatus(job.state, fn); + job.removeState(job._state, fn); }); }; @@ -236,7 +236,7 @@ Job.prototype.toJSON = function(){ , data: this.data , priority: this._priority , progress: this._progress || 0 - , state: this.state + , state: this._state , error: this._error , created_at: this.created_at , updated_at: this.updated_at @@ -391,18 +391,18 @@ Job.prototype.attempts = function(n){ }; /** - * Remove `status` with optional callback `fn`. + * Remove `state` with optional callback `fn`. * - * @param {String} status + * @param {String} state * @param {Function} fn * @return {Job} for chaining * @api public */ -Job.prototype.removeStatus = function(status, fn){ +Job.prototype.removeState = function(state, fn){ this.client.zrem('q:jobs', this.id); - this.client.zrem('q:jobs:' + status, this.id); - this.client.zrem('q:jobs:' + this.type + ':' + status, this.id, fn || noop); + this.client.zrem('q:jobs:' + state, this.id); + this.client.zrem('q:jobs:' + this.type + ':' + state, this.id, fn || noop); return this; }; @@ -425,57 +425,57 @@ Job.prototype.error = function(err){ }; /** - * Set state to `status`. + * Set state to `state`. * - * @param {String} status + * @param {String} state * @return {Job} for chaining * @api public */ -Job.prototype.status = function(status){ - this.state = status; - this.removeStatus('complete'); - this.removeStatus('failed'); - this.removeStatus('inactive'); - this.removeStatus('active'); - this.removeStatus('delayed'); - this.set('state', status); +Job.prototype.state = function(state){ + this._state = state; + this.removeState('complete'); + this.removeState('failed'); + this.removeState('inactive'); + this.removeState('active'); + this.removeState('delayed'); + this.set('state', state); this.client.zadd('q:jobs', this._priority, this.id); - this.client.zadd('q:jobs:' + status, this._priority, this.id); - this.client.zadd('q:jobs:' + this.type + ':' + status, this._priority, this.id); + this.client.zadd('q:jobs:' + state, this._priority, this.id); + this.client.zadd('q:jobs:' + this.type + ':' + state, this._priority, this.id); return this; }; /** - * Set status to "complete", and progress to 100%. + * Set state to "complete", and progress to 100%. */ Job.prototype.complete = function(){ - return this.set('progress', 100).status('complete'); + return this.set('progress', 100).state('complete'); }; /** - * Set status to "failed". + * Set state to "failed". */ Job.prototype.failed = function(){ - return this.status('failed'); + return this.state('failed'); }; /** - * Set status to "inactive". + * Set state to "inactive". */ Job.prototype.inactive = function(){ - return this.status('inactive'); + return this.state('inactive'); }; /** - * Set status to "active". + * Set state to "active". */ Job.prototype.active = function(){ - return this.status('active'); + return this.state('active'); }; /** @@ -500,7 +500,7 @@ Job.prototype.save = function(fn){ if (err) return fn(err); var key = 'q:job:' + id; self.id = id; - self.state = self.state || 'inactive'; + self._state = self._state || 'inactive'; if (max) client.hset(key, 'max_attempts', max); client.sadd('q:job:types', self.type); self.set('type', self.type); @@ -544,8 +544,8 @@ Job.prototype.update = function(fn){ // priority this.set('priority', this._priority); - // status - this.status(this.state); + // state + this.state(this._state); // data this.set('data', json, fn);