Skip to content

Commit

Permalink
wait for index refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
gmmorris committed Aug 2, 2020
1 parent ce2ee00 commit 9be0b3e
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 10 deletions.
17 changes: 12 additions & 5 deletions x-pack/plugins/alerts/server/alerts_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,11 +386,18 @@ export class AlertsClient {
updateResult.scheduledTaskId &&
!isEqual(alertSavedObject.attributes.schedule, updateResult.schedule)
) {
this.taskManager.runNow(updateResult.scheduledTaskId).catch((err: Error) => {
this.logger.error(
`Alert update failed to run its underlying task. TaskManager runNow failed with Error: ${err.message}`
);
});
this.taskManager
.runNow(updateResult.scheduledTaskId)
.then(() => {
this.logger.debug(
`Alert update has rescheduled the underlying task: ${updateResult.scheduledTaskId}`
);
})
.catch((err: Error) => {
this.logger.error(
`Alert update failed to run its underlying task. TaskManager runNow failed with Error: ${err.message}`
);
});
}
})(),
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ export class SampleTaskManagerFixturePlugin
.toPromise();

public setup(core: CoreSetup) {
core.http.createRouter().get(
const router = core.http.createRouter();
router.get(
{
path: '/api/alerting_tasks/{taskId}',
validate: {
Expand All @@ -77,6 +78,23 @@ export class SampleTaskManagerFixturePlugin
}
}
);

router.get(
{
path: `/api/ensure_tasks_index_refreshed`,
validate: {},
},
async function (
context: RequestHandlerContext,
req: KibanaRequest<any, any, any, any>,
res: KibanaResponseFactory
): Promise<IKibanaResponse<any>> {
await core.elasticsearch.legacy.client.callAsInternalUser('indices.refresh', {
index: '.kibana_task_manager',
});
return res.ok({ body: {} });
}
);
}

public start(core: CoreStart, { taskManager }: SampleTaskManagerFixtureStartDeps) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,15 @@ export default function createUpdateTests({ getService }: FtrProviderContext) {
.then((response: SupertestResponse) => response.body);
}

// FLAKY: https://github.com/elastic/kibana/issues/72803
describe.skip('update', () => {
function ensureTasksIndexRefreshed() {
return supertest
.get(`/api/ensure_tasks_index_refreshed`)
.send({})
.expect(200)
.then((response) => response.body);
}

describe('update', () => {
const objectRemover = new ObjectRemover(supertest);

after(() => objectRemover.removeAll());
Expand Down Expand Up @@ -732,6 +739,8 @@ export default function createUpdateTests({ getService }: FtrProviderContext) {
ensureDatetimeIsWithinRange(Date.parse(alertTask.runAt), 30 * 60 * 1000);
});

await ensureTasksIndexRefreshed();

const updatedData = {
name: 'bcd',
tags: ['bar'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,21 @@ export function initRoutes(
}
);

router.get(
{
path: `/api/ensure_tasks_index_refreshed`,
validate: {},
},
async function (
context: RequestHandlerContext,
req: KibanaRequest<any, any, any, any>,
res: KibanaResponseFactory
): Promise<IKibanaResponse<any>> {
await ensureIndexIsRefreshed();
return res.ok({ body: {} });
}
);

router.delete(
{
path: `/api/sample_tasks`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ export default function ({ getService }) {
.then((response) => response.body);
}

function ensureTasksIndexRefreshed() {
return supertest
.get(`/api/ensure_tasks_index_refreshed`)
.send({})
.expect(200)
.then((response) => response.body);
}

function historyDocs(taskId) {
return es
.search({
Expand Down Expand Up @@ -420,7 +428,7 @@ export default function ({ getService }) {
expectReschedule(Date.parse(originalTask.runAt), task, 30 * 60000);
});

await delay(1000);
await ensureTasksIndexRefreshed();

// second run should still be successful
const successfulRunNowResult = await runTaskNow({
Expand All @@ -434,7 +442,7 @@ export default function ({ getService }) {
expect(task.status).to.eql('idle');
});

await delay(1000);
await ensureTasksIndexRefreshed();

// third run should fail
const failedRunNowResult = await runTaskNow({
Expand Down

0 comments on commit 9be0b3e

Please sign in to comment.