Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions lib/queue/job.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};


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

Expand All @@ -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;
};
Expand Down
28 changes: 27 additions & 1 deletion test/tdd/kue.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -819,4 +845,4 @@ describe('Kue', function () {
});
});

});
});