diff --git a/x-pack/legacy/plugins/reporting/server/lib/esqueue/__tests__/worker.js b/x-pack/legacy/plugins/reporting/server/lib/esqueue/__tests__/worker.js index 1b604ebe50aa0..2c516ff77e748 100644 --- a/x-pack/legacy/plugins/reporting/server/lib/esqueue/__tests__/worker.js +++ b/x-pack/legacy/plugins/reporting/server/lib/esqueue/__tests__/worker.js @@ -10,7 +10,7 @@ import moment from 'moment'; import { noop, random, get, find, identity } from 'lodash'; import { ClientMock } from './fixtures/elasticsearch'; import { QueueMock } from './fixtures/queue'; -import { Worker } from '../worker'; +import { formatJobObject, getUpdatedDocPath, Worker } from '../worker'; import { constants } from '../constants'; const anchor = '2016-04-02T01:02:03.456'; // saturday @@ -1061,3 +1061,34 @@ describe('Worker class', function () { }); }); }); + +describe('Format Job Object', () => { + it('pulls index and ID', function () { + const jobMock = { + _index: 'foo', + _id: 'booId', + }; + expect(formatJobObject(jobMock)).eql({ + index: 'foo', + id: 'booId', + }); + }); +}); + +describe('Get Doc Path from ES Response', () => { + it('returns a formatted string after response of an update', function () { + const responseMock = { + _index: 'foo', + _type: '_doc', + _id: 'booId', + }; + expect(getUpdatedDocPath(responseMock)).equal('/foo/_doc/booId'); + }); + it('returns the same formatted string even if there is no _doc provided', function () { + const responseMock = { + _index: 'foo', + _id: 'booId', + }; + expect(getUpdatedDocPath(responseMock)).equal('/foo/_doc/booId'); + }); +}); diff --git a/x-pack/legacy/plugins/reporting/server/lib/esqueue/worker.js b/x-pack/legacy/plugins/reporting/server/lib/esqueue/worker.js index 826704aee989d..0e2f681f9b418 100644 --- a/x-pack/legacy/plugins/reporting/server/lib/esqueue/worker.js +++ b/x-pack/legacy/plugins/reporting/server/lib/esqueue/worker.js @@ -14,13 +14,18 @@ import { Poller } from '../../../../../common/poller'; const puid = new Puid(); -function formatJobObject(job) { +export function formatJobObject(job) { return { index: job._index, id: job._id, }; } +export function getUpdatedDocPath(response) { + const { _index: ind, _type: type = '_doc', _id: id } = response; + return `/${ind}/${type}/${id}`; +} + const MAX_PARTIAL_ERROR_LENGTH = 1000; // 1000 of beginning, 1000 of end const ERROR_PARTIAL_SEPARATOR = '...'; const MAX_ERROR_LENGTH = (MAX_PARTIAL_ERROR_LENGTH * 2) + ERROR_PARTIAL_SEPARATOR.length; @@ -158,6 +163,7 @@ export class Worker extends events.EventEmitter { body: { doc } }) .then((response) => { + this.info(`Job marked as claimed: ${getUpdatedDocPath(response)}`); const updatedJob = { ...job, ...response @@ -194,7 +200,9 @@ export class Worker extends events.EventEmitter { if_primary_term: job._primary_term, body: { doc } }) - .then(() => true) + .then((response) => { + this.info(`Job marked as failed: ${getUpdatedDocPath(response)}`); + }) .catch((err) => { if (err.statusCode === 409) return true; this.warn(`_failJob failed to update job ${job._id}`, err); @@ -285,8 +293,7 @@ export class Worker extends events.EventEmitter { }; this.emit(constants.EVENT_WORKER_COMPLETE, eventOutput); - const formattedDocPath = `/${response._index}/${response._type}/${response._id}`; - this.info(`Job data saved successfully: ${formattedDocPath}`); + this.info(`Job data saved successfully: ${getUpdatedDocPath(response)}`); }) .catch((err) => { if (err.statusCode === 409) return false; @@ -372,7 +379,6 @@ export class Worker extends events.EventEmitter { this.debug(`Found no claimable jobs out of ${jobs.length} total`); return; } - this.info(`Claimed job ${claimedJob._id}`); return this._performJob(claimedJob); }) .catch((err) => {