Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
state / status inconsistency
  • Loading branch information
tj committed Aug 8, 2011
1 parent ef62893 commit bbc4d82
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 63 deletions.
4 changes: 2 additions & 2 deletions lib/http/index.js
Expand Up @@ -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);
Expand Down
18 changes: 9 additions & 9 deletions lib/http/routes/json.js
Expand Up @@ -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;
Expand All @@ -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);
});
Expand Down Expand Up @@ -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' });
Expand Down
25 changes: 12 additions & 13 deletions lib/kue.js
Expand Up @@ -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;
};

Expand All @@ -231,31 +230,31 @@ Queue.prototype.card = function(status, fn){
*/

Queue.prototype.complete = function(fn){
return this.status('complete', fn);
return this.state('complete', fn);
};

/**
* Failed jobs.
*/

Queue.prototype.failed = function(fn){
return this.status('failed', fn);
return this.state('failed', fn);
};

/**
* Inactive jobs (queued).
*/

Queue.prototype.inactive = function(fn){
return this.status('inactive', fn);
return this.state('inactive', fn);
};

/**
* Active jobs (mid-process).
*/

Queue.prototype.active = function(fn){
return this.status('active', fn);
return this.state('active', fn);
};

/**
Expand Down
78 changes: 39 additions & 39 deletions lib/queue/job.js
Expand Up @@ -100,36 +100,36 @@ 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
* @param {Function} 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
* @param {Function} 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));
};

/**
Expand All @@ -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;
Expand All @@ -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);
});
};

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
};

Expand All @@ -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');
};

/**
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit bbc4d82

Please sign in to comment.