Skip to content

Commit

Permalink
implement job.removeOnComplete, closes #383
Browse files Browse the repository at this point in the history
  • Loading branch information
behrad committed Aug 8, 2014
1 parent 1fb1b98 commit b9b7e9d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/queue/job.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ exports.get = function (id, fn) {
job.updated_at = hash.updated_at;
job.failed_at = hash.failed_at;
job.duration = hash.duration;
job._removeOnComplete = hash._removeOnComplete;
try {
if (hash.data) job.data = JSON.parse(hash.data);
if (hash.result) job.result = JSON.parse(hash.result);
Expand Down Expand Up @@ -389,6 +390,11 @@ Job.prototype.delay = function (ms) {
return this;
};

Job.prototype.removeOnComplete = function (param) {
if (0 == arguments.length) return this._removeOnComplete;
this._removeOnComplete = param;
return this;
};

Job.prototype.backoff = function (param) {
if (0 == arguments.length) return this._backoff;
Expand Down Expand Up @@ -681,6 +687,7 @@ Job.prototype.update = function (fn) {

// delay
if (this._delay) this.set('delay', this._delay);
if (this._removeOnComplete) this.set('removeOnComplete', this._removeOnComplete);

if (this._backoff) {
if (_.isPlainObject(this._backoff)) this.set('backoff', JSON.stringify(this._backoff) );
Expand Down
3 changes: 3 additions & 0 deletions lib/queue/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ Worker.prototype.process = function (job, fn) {
job.attempt( function(){
self.emit('job complete', job);
events.emit(job.id, 'complete', result);
if( job.removeOnComplete() ) {
job.remove();
}
});
}.bind(this));
self.start(fn);
Expand Down

0 comments on commit b9b7e9d

Please sign in to comment.