Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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');
});
});
16 changes: 11 additions & 5 deletions x-pack/legacy/plugins/reporting/server/lib/esqueue/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) => {
Expand Down