add completed_at time, add update tests
This commit is contained in:
@@ -102,8 +102,10 @@ export default class Job extends events.EventEmitter {
|
|||||||
|
|
||||||
return Bluebird.fromCallback((cb) => this.workerFn(job._source.payload, cb))
|
return Bluebird.fromCallback((cb) => this.workerFn(job._source.payload, cb))
|
||||||
.then((output) => {
|
.then((output) => {
|
||||||
|
const completedTime = moment().toISOString();
|
||||||
const unknownMime = false;
|
const unknownMime = false;
|
||||||
const docOutput = {};
|
const docOutput = {};
|
||||||
|
|
||||||
if (typeof output === 'object' && output.content) {
|
if (typeof output === 'object' && output.content) {
|
||||||
docOutput.content = output.content;
|
docOutput.content = output.content;
|
||||||
docOutput.content_type = output.content_type || unknownMime;
|
docOutput.content_type = output.content_type || unknownMime;
|
||||||
@@ -114,6 +116,7 @@ export default class Job extends events.EventEmitter {
|
|||||||
|
|
||||||
const doc = {
|
const doc = {
|
||||||
status: jobStatuses.JOB_STATUS_COMPLETED,
|
status: jobStatuses.JOB_STATUS_COMPLETED,
|
||||||
|
completed_at: completedTime,
|
||||||
output: docOutput
|
output: docOutput
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import moment from 'moment';
|
|||||||
import { noop, random } from 'lodash';
|
import { noop, random } from 'lodash';
|
||||||
import Worker from '../../lib/worker';
|
import Worker from '../../lib/worker';
|
||||||
import elasticsearchMock from '../fixtures/elasticsearch';
|
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 anchor = '2016-04-02T01:02:03.456'; // saturday
|
||||||
const defaults = {
|
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) {
|
it('should reject on job errors', function (done) {
|
||||||
const workerFn = function (jobPayload, cb) {
|
const workerFn = function (jobPayload, cb) {
|
||||||
cb(new Error('test error'));
|
cb(new Error('test error'));
|
||||||
|
|||||||
Reference in New Issue
Block a user