Skip to content

Commit

Permalink
Fix previousStartedAt by not changing when execution fails (#81388)
Browse files Browse the repository at this point in the history
Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
mikecote and kibanamachine authored Oct 26, 2020
1 parent d784840 commit 141426d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 24 deletions.
61 changes: 43 additions & 18 deletions x-pack/plugins/alerts/server/task_runner/task_runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -621,9 +621,7 @@ describe('Task Runner', () => {
expect(await taskRunner.run()).toMatchInlineSnapshot(`
Object {
"runAt": 1970-01-01T00:00:10.000Z,
"state": Object {
"previousStartedAt": 1970-01-01T00:00:00.000Z,
},
"state": Object {},
}
`);
expect(taskRunnerFactoryInitializerParams.logger.error).toHaveBeenCalledWith(
Expand Down Expand Up @@ -727,9 +725,7 @@ describe('Task Runner', () => {
expect(runnerResult).toMatchInlineSnapshot(`
Object {
"runAt": 1970-01-01T00:00:10.000Z,
"state": Object {
"previousStartedAt": 1970-01-01T00:00:00.000Z,
},
"state": Object {},
}
`);

Expand Down Expand Up @@ -781,9 +777,7 @@ describe('Task Runner', () => {
expect(runnerResult).toMatchInlineSnapshot(`
Object {
"runAt": 1970-01-01T00:05:00.000Z,
"state": Object {
"previousStartedAt": 1970-01-01T00:00:00.000Z,
},
"state": Object {},
}
`);
});
Expand Down Expand Up @@ -814,9 +808,7 @@ describe('Task Runner', () => {
expect(runnerResult).toMatchInlineSnapshot(`
Object {
"runAt": 1970-01-01T00:05:00.000Z,
"state": Object {
"previousStartedAt": 1970-01-01T00:00:00.000Z,
},
"state": Object {},
}
`);
});
Expand Down Expand Up @@ -846,13 +838,48 @@ describe('Task Runner', () => {
expect(runnerResult).toMatchInlineSnapshot(`
Object {
"runAt": 1970-01-01T00:05:00.000Z,
"state": Object {
"previousStartedAt": 1970-01-01T00:00:00.000Z,
},
"state": Object {},
}
`);
});

test(`doesn't change previousStartedAt when it fails to run`, async () => {
const originalAlertSate = {
previousStartedAt: '1970-01-05T00:00:00.000Z',
};

alertType.executor.mockImplementation(
({ services: executorServices }: AlertExecutorOptions) => {
throw new Error('OMG');
}
);

const taskRunner = new TaskRunner(
alertType,
{
...mockedTaskInstance,
state: originalAlertSate,
},
taskRunnerFactoryInitializerParams
);

alertsClient.get.mockResolvedValueOnce(mockedAlertTypeSavedObject);
encryptedSavedObjectsClient.getDecryptedAsInternalUser.mockResolvedValueOnce({
id: '1',
type: 'alert',
attributes: {
apiKey: Buffer.from('123:abc').toString('base64'),
},
references: [],
});

const runnerResult = await taskRunner.run();

expect(runnerResult.state.previousStartedAt).toEqual(
new Date(originalAlertSate.previousStartedAt)
);
});

test('avoids rescheduling a failed Alert Task Runner when it throws due to failing to fetch the alert', async () => {
alertsClient.get.mockImplementation(() => {
throw SavedObjectsErrorHelpers.createGenericNotFoundError('task', '1');
Expand All @@ -878,9 +905,7 @@ describe('Task Runner', () => {
expect(runnerResult).toMatchInlineSnapshot(`
Object {
"runAt": undefined,
"state": Object {
"previousStartedAt": 1970-01-01T00:00:00.000Z,
},
"state": Object {},
}
`);
});
Expand Down
9 changes: 3 additions & 6 deletions x-pack/plugins/alerts/server/task_runner/task_runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ export class TaskRunner {
async run(): Promise<AlertTaskRunResult> {
const {
params: { alertId, spaceId },
startedAt: previousStartedAt,
startedAt,
state: originalState,
} = this.taskInstance;

Expand Down Expand Up @@ -360,7 +360,7 @@ export class TaskRunner {
(stateUpdates: AlertTaskState) => {
return {
...stateUpdates,
previousStartedAt,
previousStartedAt: startedAt,
};
},
(err: Error) => {
Expand All @@ -370,10 +370,7 @@ export class TaskRunner {
} else {
this.logger.error(message);
}
return {
...originalState,
previousStartedAt,
};
return originalState;
}
),
runAt: resolveErr<Date | undefined, Error>(runAt, (err) => {
Expand Down

0 comments on commit 141426d

Please sign in to comment.