Skip to content

Commit 855eb1f

Browse files
committed
feat(progress): adding clbk to set progress - fix inconsisentcies
fixes Automattic#1204
1 parent 87d6150 commit 855eb1f

File tree

2 files changed

+43
-8
lines changed

2 files changed

+43
-8
lines changed

lib/queue/job.js

+16-7
Original file line numberDiff line numberDiff line change
@@ -340,12 +340,19 @@ Job.prototype.toJSON = function() {
340340
};
341341

342342

343-
Job.prototype.refreshTtl = function() {
344-
('active' === this.state() && this._ttl > 0)
343+
/**
344+
* Refreshes Time to live of the job in jobs:`state` zset
345+
*
346+
* @param {Function} [clbk]
347+
*/
348+
349+
Job.prototype.refreshTtl = function( clbk ) {
350+
clbk = clbk || noop;
351+
return ('active' === this.state() && this._ttl > 0)
345352
?
346-
this.client.zadd(this.client.getKey('jobs:' + this.state()), Date.now() + parseInt(this._ttl), this.zid, noop)
353+
this.client.zadd(this.client.getKey('jobs:' + this.state()), Date.now() + parseInt(this._ttl), this.zid, clbk)
347354
:
348-
noop();
355+
clbk();
349356
};
350357

351358

@@ -417,13 +424,15 @@ Job.prototype.get = function( key, fn ) {
417424
*
418425
* @param {Number} complete
419426
* @param {Number} total
420-
* @param {Object} data
427+
* @param {Object} [data]
428+
* @param {Function} [clbk]
421429
* @return {Job} for chaining
422430
* @api public
423431
*/
424432

425-
Job.prototype.progress = function( complete, total, data ) {
433+
Job.prototype.progress = function( complete, total, data, clbk ) {
426434
if( 0 == arguments.length ) return this._progress;
435+
if ( undefined === clbk && 'function' === typeof data ) clbk = data;
427436
var n = Math.min(100, complete * 100 / total | 0);
428437
this.set('progress', n);
429438

@@ -432,7 +441,7 @@ Job.prototype.progress = function( complete, total, data ) {
432441
if( data ) this.set('progress_data', JSON.stringify(data));
433442

434443
this.set('updated_at', Date.now());
435-
this.refreshTtl();
444+
this.refreshTtl(clbk);
436445
events.emit(this.id, 'progress', n, data);
437446
return this;
438447
};

test/tdd/kue.spec.js

+27-1
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,32 @@ describe('Kue', function () {
790790
});
791791
});
792792

793+
describe('Function: progress', function () {
794+
var queue;
795+
var client;
796+
797+
beforeEach(function() {
798+
queue = kue.createQueue();
799+
client = {
800+
hset: sinon.spy(),
801+
zadd: sinon.spy(),
802+
getKey: sinon.spy(),
803+
publish: sinon.spy()
804+
}
805+
events.emit = sinon.spy()
806+
});
807+
808+
it('accepts callback', function() {
809+
var job = queue.create('type', {});
810+
job.client = client;
811+
var clbk = sinon.spy();
812+
job._state = 'active';
813+
job._ttl = 1000;
814+
job.progress(1, 10, 'data', clbk);
815+
client.zadd.getCall(0).args[3].should.be.eql(clbk);
816+
});
817+
});
818+
793819
describe('Function: delayedCount', function() {
794820
var queue;
795821

@@ -819,4 +845,4 @@ describe('Kue', function () {
819845
});
820846
});
821847

822-
});
848+
});

0 commit comments

Comments
 (0)