diff --git a/src/worker.js b/src/worker.js index c5e8851..7383b62 100644 --- a/src/worker.js +++ b/src/worker.js @@ -102,8 +102,10 @@ export default class Job extends events.EventEmitter { return Bluebird.fromCallback((cb) => this.workerFn(job._source.payload, cb)) .then((output) => { + const completedTime = moment().toISOString(); const unknownMime = false; const docOutput = {}; + if (typeof output === 'object' && output.content) { docOutput.content = output.content; docOutput.content_type = output.content_type || unknownMime; @@ -114,6 +116,7 @@ export default class Job extends events.EventEmitter { const doc = { status: jobStatuses.JOB_STATUS_COMPLETED, + completed_at: completedTime, output: docOutput }; diff --git a/test/src/worker.js b/test/src/worker.js index a67cb77..447857e 100644 --- a/test/src/worker.js +++ b/test/src/worker.js @@ -4,7 +4,7 @@ import moment from 'moment'; import { noop, random } from 'lodash'; import Worker from '../../lib/worker'; import elasticsearchMock from '../fixtures/elasticsearch'; -import { JOB_STATUS_PROCESSING, JOB_STATUS_FAILED } from '../../lib/helpers/constants'; +import { JOB_STATUS_PROCESSING, JOB_STATUS_COMPLETED, JOB_STATUS_FAILED } from '../../lib/helpers/constants'; const anchor = '2016-04-02T01:02:03.456'; // saturday const defaults = { @@ -273,6 +273,30 @@ describe('Worker class', function () { }); }); + it('should update the job status and completed time', function (done) { + const startTime = moment().valueOf(); + const workerFn = function (jobPayload, cb) { + expect(jobPayload).to.eql(payload); + cb(null, payload); + }; + const worker = new Worker(mockQueue, 'test', workerFn); + + worker._performJob(job) + .then(() => { + try { + sinon.assert.calledOnce(updateSpy); + const doc = updateSpy.firstCall.args[0].body.doc; + expect(doc).to.have.property('status', JOB_STATUS_COMPLETED); + expect(doc).to.have.property('completed_at'); + const completedTimestamp = moment(doc.completed_at).valueOf(); + expect(completedTimestamp).to.be.greaterThan(startTime); + done(); + } catch (err) { + done(err); + } + }); + }); + it('should reject on job errors', function (done) { const workerFn = function (jobPayload, cb) { cb(new Error('test error'));