Permalink
Browse files

Fixed `Job#remove(fn)`

removes:

  - hash
  - search index
  - from state set
  - from jobs set
  • Loading branch information...
1 parent bbc4d82 commit 5ef0b812227df45f0be4cf3f02ebda309e3a5daa @tj tj committed Aug 8, 2011
Showing with 40 additions and 27 deletions.
  1. +2 −2 examples/events.js
  2. +38 −25 lib/queue/job.js
View
@@ -21,9 +21,9 @@ function create() {
});
job.on('complete', function(){
- console.log("Job complete");
+ console.log(" Job complete");
}).on('failed', function(){
- console.log("Job failed");
+ console.log(" Job failed");
}).on('progress', function(progress){
process.stdout.write('\r job #' + job.id + ' ' + progress + '% complete');
});
View
@@ -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.removeState(job._state, fn);
+ job.remove(fn);
});
};
@@ -330,7 +330,7 @@ Job.prototype.progress = function(complete, total){
Job.prototype.delay = function(ms){
if (0 == arguments.length) return this._delay;
this._delay = ms;
- this.state = 'delayed';
+ this._state = 'delayed';
return this;
};
@@ -391,36 +391,34 @@ Job.prototype.attempts = function(n){
};
/**
- * Remove `state` with optional callback `fn`.
+ * Remove the job and callback `fn(err)`.
*
- * @param {String} state
* @param {Function} fn
* @return {Job} for chaining
* @api public
*/
-Job.prototype.removeState = function(state, fn){
- this.client.zrem('q:jobs', this.id);
- this.client.zrem('q:jobs:' + state, this.id);
- this.client.zrem('q:jobs:' + this.type + ':' + state, this.id, fn || noop);
+Job.prototype.remove = function(fn){
+ this.removeState();
+ search.remove(this.id);
+ this.client.del('q:job:' + this.id, fn || noop);
return this;
};
/**
- * Set the job's failure `err`.
+ * Remove state and callback `fn(err)`.
*
- * @param {Error} err
+ * @param {Function} fn
* @return {Job} for chaining
* @api public
*/
-Job.prototype.error = function(err){
- if (0 == arguments.length) return this._error;
- var str = err.stack || err.message
- , summary = str.split('\n')[0];
- this.set('failed_at', Date.now());
- this.set('error', str);
- this.log('%s', summary);
+Job.prototype.removeState = function(fn){
+ var client = this.client
+ , state = this._state;
+ client.zrem('q:jobs', this.id);
+ client.zrem('q:jobs:' + state, this.id);
+ client.zrem('q:jobs:' + this.type + ':' + state, this.id);
return this;
};
@@ -433,16 +431,31 @@ Job.prototype.error = function(err){
*/
Job.prototype.state = function(state){
+ var client = this.client;
+ this.removeState();
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:' + state, this._priority, this.id);
- this.client.zadd('q:jobs:' + this.type + ':' + state, this._priority, this.id);
+ client.zadd('q:jobs', this._priority, this.id);
+ client.zadd('q:jobs:' + state, this._priority, this.id);
+ client.zadd('q:jobs:' + this.type + ':' + state, this._priority, this.id);
+ return this;
+};
+
+/**
+ * Set the job's failure `err`.
+ *
+ * @param {Error} err
+ * @return {Job} for chaining
+ * @api public
+ */
+
+Job.prototype.error = function(err){
+ if (0 == arguments.length) return this._error;
+ var str = err.stack || err.message
+ , summary = str.split('\n')[0];
+ this.set('failed_at', Date.now());
+ this.set('error', str);
+ this.log('%s', summary);
return this;
};

0 comments on commit 5ef0b81

Please sign in to comment.