diff --git a/lib/queue/job.js b/lib/queue/job.js index 204aff27..145632d3 100644 --- a/lib/queue/job.js +++ b/lib/queue/job.js @@ -340,12 +340,19 @@ Job.prototype.toJSON = function() { }; -Job.prototype.refreshTtl = function() { - ('active' === this.state() && this._ttl > 0) +/** + * Refreshes Time to live of the job in jobs:`state` zset + * + * @param {Function} [clbk] + */ + +Job.prototype.refreshTtl = function( clbk ) { + clbk = clbk || noop; + return ('active' === this.state() && this._ttl > 0) ? - this.client.zadd(this.client.getKey('jobs:' + this.state()), Date.now() + parseInt(this._ttl), this.zid, noop) + this.client.zadd(this.client.getKey('jobs:' + this.state()), Date.now() + parseInt(this._ttl), this.zid, clbk) : - noop(); + clbk(); }; @@ -417,13 +424,15 @@ Job.prototype.get = function( key, fn ) { * * @param {Number} complete * @param {Number} total - * @param {Object} data + * @param {Object} [data] + * @param {Function} [clbk] * @return {Job} for chaining * @api public */ -Job.prototype.progress = function( complete, total, data ) { +Job.prototype.progress = function( complete, total, data, clbk ) { if( 0 == arguments.length ) return this._progress; + if ( undefined === clbk && 'function' === typeof data ) clbk = data; var n = Math.min(100, complete * 100 / total | 0); this.set('progress', n); @@ -432,7 +441,7 @@ Job.prototype.progress = function( complete, total, data ) { if( data ) this.set('progress_data', JSON.stringify(data)); this.set('updated_at', Date.now()); - this.refreshTtl(); + this.refreshTtl(clbk); events.emit(this.id, 'progress', n, data); return this; }; diff --git a/test/tdd/kue.spec.js b/test/tdd/kue.spec.js index 2a3cb054..1ca27b78 100644 --- a/test/tdd/kue.spec.js +++ b/test/tdd/kue.spec.js @@ -790,6 +790,32 @@ describe('Kue', function () { }); }); + describe('Function: progress', function () { + var queue; + var client; + + beforeEach(function() { + queue = kue.createQueue(); + client = { + hset: sinon.spy(), + zadd: sinon.spy(), + getKey: sinon.spy(), + publish: sinon.spy() + } + events.emit = sinon.spy() + }); + + it('accepts callback', function() { + var job = queue.create('type', {}); + job.client = client; + var clbk = sinon.spy(); + job._state = 'active'; + job._ttl = 1000; + job.progress(1, 10, 'data', clbk); + client.zadd.getCall(0).args[3].should.be.eql(clbk); + }); + }); + describe('Function: delayedCount', function() { var queue; @@ -819,4 +845,4 @@ describe('Kue', function () { }); }); -}); \ No newline at end of file +});