diff --git a/lib/http/public/javascripts/job.js b/lib/http/public/javascripts/job.js index 2fc08e1c..068ab869 100755 --- a/lib/http/public/javascripts/job.js +++ b/lib/http/public/javascripts/job.js @@ -220,8 +220,8 @@ Job.prototype.renderUpdate = function () { // delayed if ('delayed' == this.state) { - var delay = parseInt(this.delay, 10) - , creation = parseInt(this.created_at, 10) + var delay = this.delay + , creation = this.created_at , remaining = relative(creation + delay - Date.now()); view.title((this.data.title || '') + ' ( ' + remaining + ' )'); } diff --git a/lib/queue/job.js b/lib/queue/job.js index 204aff27..38b65e2c 100644 --- a/lib/queue/job.js +++ b/lib/queue/job.js @@ -185,18 +185,18 @@ exports.get = function( id, jobType, fn ) { // we can just merge these job.type = hash.type; job._ttl = hash.ttl; - job._delay = hash.delay; + job._delay = parseInt(hash.delay, 10); job.priority(Number(hash.priority)); job._progress = hash.progress; job._attempts = Number(hash.attempts); job._max_attempts = Number(hash.max_attempts); job._state = hash.state; job._error = hash.error; - job.created_at = hash.created_at; - job.promote_at = hash.promote_at; - job.updated_at = hash.updated_at; - job.failed_at = hash.failed_at; - job.started_at = hash.started_at; + job.created_at = parseInt(hash.created_at, 10); + job.promote_at = parseInt(hash.promote_at, 10); + job.updated_at = parseInt(hash.updated_at, 10); + job.failed_at = parseInt(hash.failed_at, 10); + job.started_at = parseInt(hash.started_at, 10); job.duration = hash.duration; job.workerId = hash.workerId; job._removeOnComplete = hash.removeOnComplete; @@ -692,7 +692,7 @@ Job.prototype.state = function( state, fn ) { .zadd(client.getKey('jobs:' + this.type + ':' + state), this._priority, this.zid); // use promote_at as score when job moves to delayed - ('delayed' === state) ? multi.zadd(client.getKey('jobs:' + state), parseInt(this.promote_at), this.zid) : noop(); + ('delayed' === state) ? multi.zadd(client.getKey('jobs:' + state), this.promote_at, this.zid) : noop(); ('active' === state && this._ttl > 0) ? multi.zadd(client.getKey('jobs:' + state), Date.now() + parseInt(this._ttl), this.zid) : noop(); ('active' === state && !this._ttl) ? multi.zadd(client.getKey('jobs:' + state), this._priority<0?this._priority:-this._priority, this.zid) : noop(); ('inactive' === state) ? multi.lpush(client.getKey(this.type + ':jobs'), 1) : noop(); @@ -839,8 +839,8 @@ Job.prototype.update = function( fn ) { if( this._delay ) { this.set('delay', this._delay); if( this.created_at ) { - var timestamp = parseInt(this.failed_at || this.created_at, 10) - , delay = parseInt(this._delay); + var timestamp = this.failed_at || this.created_at + , delay = this._delay; this.promote_at = timestamp + delay; this.set('promote_at', this.promote_at); } diff --git a/test/tdd/kue.spec.js b/test/tdd/kue.spec.js index 2a3cb054..f3176a20 100644 --- a/test/tdd/kue.spec.js +++ b/test/tdd/kue.spec.js @@ -65,6 +65,75 @@ describe('Kue', function () { }); }); + describe('Function: save', function () { + var queue; + var client; + + beforeEach(function () { + client = { + incr: sinon.stub().callsArgWith(1, null, 1), + getKey: sinon.stub().returns([]), + createFIFO: sinon.stub().returns(1), + sadd: sinon.stub() + }; + queue = kue.createQueue(); + Job.client = client; + }); + + it('should set created_at when job saved', function (done) { + var now = Date.now(); + var job = queue.create('type', {}); + job.subscribe = sinon.stub().callsArg(0); + job.set = sinon.stub(); + job.update = sinon.stub().callsArg(0); + + job.save(function () { + job.created_at.should.be.approximately(now, 200); + done(); + }); + }); + }); + + describe('Function: get', function () { + var client; + var createClient; + + beforeEach(function () { + client = { + getKey: sinon.stub().returns([]), + createFIFO: sinon.stub().returns(1), + hgetall: sinon.stub().callsArgWith(1, null, { + created_at: '12345', + failed_at: '12345', + promote_at: '12345', + delay: '12345', + updated_at: '12345', + started_at: '12345', + type: 'type' + }) + }; + createClient = redis.createClient; + redis.createClient = sinon.stub().returns(client); + }); + + afterEach(function () { + redis.createClient = createClient; + redis._client = null; + }); + + it('should convert date fields to numbers', function (done) { + Job.get(1, 'type', function (err, job) { + job.created_at.should.be.an.instanceOf(Number); + job.failed_at.should.be.an.instanceOf(Number); + job.promote_at.should.be.an.instanceOf(Number); + job._delay.should.be.an.instanceOf(Number); + job.updated_at.should.be.an.instanceOf(Number); + job.started_at.should.be.an.instanceOf(Number); + done(); + }); + }); + }); + describe('Function: on', function() { var queue, noop; beforeEach(function(){