Skip to content

Commit ab58a71

Browse files
authored
Fix enable API to schedule task after alert is updated (#55095) (#55846)
1 parent 9c0bb0f commit ab58a71

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

x-pack/legacy/plugins/alerting/server/alerts_client.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -883,7 +883,6 @@ describe('enable()', () => {
883883
schedule: { interval: '10s' },
884884
alertTypeId: '2',
885885
enabled: true,
886-
scheduledTaskId: 'task-123',
887886
updatedBy: 'elastic',
888887
apiKey: null,
889888
apiKeyOwner: null,
@@ -892,6 +891,9 @@ describe('enable()', () => {
892891
version: '123',
893892
}
894893
);
894+
expect(savedObjectsClient.update).toHaveBeenCalledWith('alert', '1', {
895+
scheduledTaskId: 'task-123',
896+
});
895897
expect(taskManager.schedule).toHaveBeenCalledWith({
896898
taskType: `alerting:2`,
897899
params: {
@@ -964,7 +966,6 @@ describe('enable()', () => {
964966
schedule: { interval: '10s' },
965967
alertTypeId: '2',
966968
enabled: true,
967-
scheduledTaskId: 'task-123',
968969
apiKey: Buffer.from('123:abc').toString('base64'),
969970
apiKeyOwner: 'elastic',
970971
updatedBy: 'elastic',
@@ -973,6 +974,9 @@ describe('enable()', () => {
973974
version: '123',
974975
}
975976
);
977+
expect(savedObjectsClient.update).toHaveBeenCalledWith('alert', '1', {
978+
scheduledTaskId: 'task-123',
979+
});
976980
expect(taskManager.schedule).toHaveBeenCalledWith({
977981
taskType: `alerting:2`,
978982
params: {

x-pack/legacy/plugins/alerting/server/alerts_client.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,6 @@ export class AlertsClient {
362362
});
363363

364364
if (attributes.enabled === false) {
365-
const scheduledTask = await this.scheduleAlert(id, attributes.alertTypeId);
366365
const username = await this.getUserName();
367366
await this.savedObjectsClient.update(
368367
'alert',
@@ -372,11 +371,11 @@ export class AlertsClient {
372371
enabled: true,
373372
...this.apiKeyAsAlertAttributes(await this.createAPIKey(), username),
374373
updatedBy: username,
375-
376-
scheduledTaskId: scheduledTask.id,
377374
},
378375
{ version }
379376
);
377+
const scheduledTask = await this.scheduleAlert(id, attributes.alertTypeId);
378+
await this.savedObjectsClient.update('alert', id, { scheduledTaskId: scheduledTask.id });
380379
await this.invalidateApiKey({ apiKey: attributes.apiKey });
381380
}
382381
}

0 commit comments

Comments
 (0)