add completed_at time, add update tests

This commit is contained in:
2016-05-02 12:57:11 -07:00
parent 7191a2bed0
commit b12aa87ad3
2 changed files with 28 additions and 1 deletions

View File

@@ -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
};

View File

@@ -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'));