Skip to content

Commit 27cee8c

Browse files
Fix previousStartedAt by not changing when execution fails (elastic#81388)
Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
1 parent cb65d36 commit 27cee8c

File tree

2 files changed

+46
-24
lines changed

2 files changed

+46
-24
lines changed

x-pack/plugins/alerts/server/task_runner/task_runner.test.ts

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -621,9 +621,7 @@ describe('Task Runner', () => {
621621
expect(await taskRunner.run()).toMatchInlineSnapshot(`
622622
Object {
623623
"runAt": 1970-01-01T00:00:10.000Z,
624-
"state": Object {
625-
"previousStartedAt": 1970-01-01T00:00:00.000Z,
626-
},
624+
"state": Object {},
627625
}
628626
`);
629627
expect(taskRunnerFactoryInitializerParams.logger.error).toHaveBeenCalledWith(
@@ -727,9 +725,7 @@ describe('Task Runner', () => {
727725
expect(runnerResult).toMatchInlineSnapshot(`
728726
Object {
729727
"runAt": 1970-01-01T00:00:10.000Z,
730-
"state": Object {
731-
"previousStartedAt": 1970-01-01T00:00:00.000Z,
732-
},
728+
"state": Object {},
733729
}
734730
`);
735731

@@ -781,9 +777,7 @@ describe('Task Runner', () => {
781777
expect(runnerResult).toMatchInlineSnapshot(`
782778
Object {
783779
"runAt": 1970-01-01T00:05:00.000Z,
784-
"state": Object {
785-
"previousStartedAt": 1970-01-01T00:00:00.000Z,
786-
},
780+
"state": Object {},
787781
}
788782
`);
789783
});
@@ -814,9 +808,7 @@ describe('Task Runner', () => {
814808
expect(runnerResult).toMatchInlineSnapshot(`
815809
Object {
816810
"runAt": 1970-01-01T00:05:00.000Z,
817-
"state": Object {
818-
"previousStartedAt": 1970-01-01T00:00:00.000Z,
819-
},
811+
"state": Object {},
820812
}
821813
`);
822814
});
@@ -846,13 +838,48 @@ describe('Task Runner', () => {
846838
expect(runnerResult).toMatchInlineSnapshot(`
847839
Object {
848840
"runAt": 1970-01-01T00:05:00.000Z,
849-
"state": Object {
850-
"previousStartedAt": 1970-01-01T00:00:00.000Z,
851-
},
841+
"state": Object {},
852842
}
853843
`);
854844
});
855845

846+
test(`doesn't change previousStartedAt when it fails to run`, async () => {
847+
const originalAlertSate = {
848+
previousStartedAt: '1970-01-05T00:00:00.000Z',
849+
};
850+
851+
alertType.executor.mockImplementation(
852+
({ services: executorServices }: AlertExecutorOptions) => {
853+
throw new Error('OMG');
854+
}
855+
);
856+
857+
const taskRunner = new TaskRunner(
858+
alertType,
859+
{
860+
...mockedTaskInstance,
861+
state: originalAlertSate,
862+
},
863+
taskRunnerFactoryInitializerParams
864+
);
865+
866+
alertsClient.get.mockResolvedValueOnce(mockedAlertTypeSavedObject);
867+
encryptedSavedObjectsClient.getDecryptedAsInternalUser.mockResolvedValueOnce({
868+
id: '1',
869+
type: 'alert',
870+
attributes: {
871+
apiKey: Buffer.from('123:abc').toString('base64'),
872+
},
873+
references: [],
874+
});
875+
876+
const runnerResult = await taskRunner.run();
877+
878+
expect(runnerResult.state.previousStartedAt).toEqual(
879+
new Date(originalAlertSate.previousStartedAt)
880+
);
881+
});
882+
856883
test('avoids rescheduling a failed Alert Task Runner when it throws due to failing to fetch the alert', async () => {
857884
alertsClient.get.mockImplementation(() => {
858885
throw SavedObjectsErrorHelpers.createGenericNotFoundError('task', '1');
@@ -878,9 +905,7 @@ describe('Task Runner', () => {
878905
expect(runnerResult).toMatchInlineSnapshot(`
879906
Object {
880907
"runAt": undefined,
881-
"state": Object {
882-
"previousStartedAt": 1970-01-01T00:00:00.000Z,
883-
},
908+
"state": Object {},
884909
}
885910
`);
886911
});

x-pack/plugins/alerts/server/task_runner/task_runner.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ export class TaskRunner {
322322
async run(): Promise<AlertTaskRunResult> {
323323
const {
324324
params: { alertId, spaceId },
325-
startedAt: previousStartedAt,
325+
startedAt,
326326
state: originalState,
327327
} = this.taskInstance;
328328

@@ -360,7 +360,7 @@ export class TaskRunner {
360360
(stateUpdates: AlertTaskState) => {
361361
return {
362362
...stateUpdates,
363-
previousStartedAt,
363+
previousStartedAt: startedAt,
364364
};
365365
},
366366
(err: Error) => {
@@ -370,10 +370,7 @@ export class TaskRunner {
370370
} else {
371371
this.logger.error(message);
372372
}
373-
return {
374-
...originalState,
375-
previousStartedAt,
376-
};
373+
return originalState;
377374
}
378375
),
379376
runAt: resolveErr<Date | undefined, Error>(runAt, (err) => {

0 commit comments

Comments
 (0)