Skip to content

Commit

Permalink
[Reporting/CSV] Do not fail the job if scroll ID can not be cleared (#…
Browse files Browse the repository at this point in the history
…76014)

Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
tsullivan and elasticmachine authored Sep 1, 2020
1 parent 9a27beb commit ba9ad8d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,13 @@
import expect from '@kbn/expect';
import sinon from 'sinon';
import { CancellationToken } from '../../../../common';
import { LevelLogger } from '../../../lib';
import { createMockLevelLogger } from '../../../test_helpers/create_mock_levellogger';
import { ScrollConfig } from '../../../types';
import { createHitIterator } from './hit_iterator';

const mockLogger = {
error: new Function(),
debug: new Function(),
warning: new Function(),
} as LevelLogger;
const mockLogger = createMockLevelLogger();
const debugLogStub = sinon.stub(mockLogger, 'debug');
const warnLogStub = sinon.stub(mockLogger, 'warning');
const warnLogStub = sinon.stub(mockLogger, 'warn');
const errorLogStub = sinon.stub(mockLogger, 'error');
const mockCallEndpoint = sinon.stub();
const mockSearchRequest = {};
Expand Down Expand Up @@ -134,4 +130,30 @@ describe('hitIterator', function () {
expect(errorLogStub.callCount).to.be(1);
expect(errorThrown).to.be(true);
});

it('handles scroll id could not be cleared', async () => {
// Setup
mockCallEndpoint.withArgs('clearScroll').rejects({ status: 404 });

// Begin
const hitIterator = createHitIterator(mockLogger);
const iterator = hitIterator(
mockConfig,
mockCallEndpoint,
mockSearchRequest,
realCancellationToken
);

while (true) {
const { done: iterationDone, value: hit } = await iterator.next();
if (iterationDone) {
break;
}
expect(hit).to.be('you found me');
}

expect(mockCallEndpoint.callCount).to.be(13);
expect(warnLogStub.callCount).to.be(1);
expect(errorLogStub.callCount).to.be(1);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,17 @@ export function createHitIterator(logger: LevelLogger) {
);
}

function clearScroll(scrollId: string | undefined) {
async function clearScroll(scrollId: string | undefined) {
logger.debug('executing clearScroll request');
return callEndpoint('clearScroll', {
scrollId: [scrollId],
});
try {
await callEndpoint('clearScroll', {
scrollId: [scrollId],
});
} catch (err) {
// Do not throw the error, as the job can still be completed successfully
logger.warn('Scroll context can not be cleared!');
logger.error(err);
}
}

try {
Expand All @@ -86,7 +92,7 @@ export function createHitIterator(logger: LevelLogger) {
({ scrollId, hits } = await scroll(scrollId));

if (cancellationToken.isCancelled()) {
logger.warning(
logger.warn(
'Any remaining scrolling searches have been cancelled by the cancellation token.'
);
}
Expand Down

0 comments on commit ba9ad8d

Please sign in to comment.