Skip to content

Commit 7abc6c6

Browse files
committed
[Reporting/CSV] Do not fail the job if scroll ID can not be cleared
1 parent 1ca7651 commit 7abc6c6

File tree

2 files changed

+40
-12
lines changed

2 files changed

+40
-12
lines changed

x-pack/plugins/reporting/server/export_types/csv/generate_csv/hit_iterator.test.ts

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,13 @@
77
import expect from '@kbn/expect';
88
import sinon from 'sinon';
99
import { CancellationToken } from '../../../../common';
10-
import { LevelLogger } from '../../../lib';
10+
import { createMockLevelLogger } from '../../../test_helpers/create_mock_levellogger';
1111
import { ScrollConfig } from '../../../types';
1212
import { createHitIterator } from './hit_iterator';
1313

14-
const mockLogger = {
15-
error: new Function(),
16-
debug: new Function(),
17-
warning: new Function(),
18-
} as LevelLogger;
14+
const mockLogger = createMockLevelLogger();
1915
const debugLogStub = sinon.stub(mockLogger, 'debug');
20-
const warnLogStub = sinon.stub(mockLogger, 'warning');
16+
const warnLogStub = sinon.stub(mockLogger, 'warn');
2117
const errorLogStub = sinon.stub(mockLogger, 'error');
2218
const mockCallEndpoint = sinon.stub();
2319
const mockSearchRequest = {};
@@ -134,4 +130,30 @@ describe('hitIterator', function () {
134130
expect(errorLogStub.callCount).to.be(1);
135131
expect(errorThrown).to.be(true);
136132
});
133+
134+
it('handles scroll id could not be cleared', async () => {
135+
// Setup
136+
mockCallEndpoint.withArgs('clearScroll').rejects({ status: 404 });
137+
138+
// Begin
139+
const hitIterator = createHitIterator(mockLogger);
140+
const iterator = hitIterator(
141+
mockConfig,
142+
mockCallEndpoint,
143+
mockSearchRequest,
144+
realCancellationToken
145+
);
146+
147+
while (true) {
148+
const { done: iterationDone, value: hit } = await iterator.next();
149+
if (iterationDone) {
150+
break;
151+
}
152+
expect(hit).to.be('you found me');
153+
}
154+
155+
expect(mockCallEndpoint.callCount).to.be(13);
156+
expect(warnLogStub.callCount).to.be(1);
157+
expect(errorLogStub.callCount).to.be(1);
158+
});
137159
});

x-pack/plugins/reporting/server/export_types/csv/generate_csv/hit_iterator.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,17 @@ export function createHitIterator(logger: LevelLogger) {
6868
);
6969
}
7070

71-
function clearScroll(scrollId: string | undefined) {
71+
async function clearScroll(scrollId: string | undefined) {
7272
logger.debug('executing clearScroll request');
73-
return callEndpoint('clearScroll', {
74-
scrollId: [scrollId],
75-
});
73+
try {
74+
await callEndpoint('clearScroll', {
75+
scrollId: [scrollId],
76+
});
77+
} catch (err) {
78+
// Do not throw the error, as the job can still be completed successfully
79+
logger.warn('Scroll context can not be cleared!');
80+
logger.error(err);
81+
}
7682
}
7783

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

8894
if (cancellationToken.isCancelled()) {
89-
logger.warning(
95+
logger.warn(
9096
'Any remaining scrolling searches have been cancelled by the cancellation token.'
9197
);
9298
}

0 commit comments

Comments
 (0)