Skip to content

Commit

Permalink
Fix enable API to schedule task after alert is updated (#55095) (#55846)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikecote committed Jan 24, 2020
1 parent 9c0bb0f commit ab58a71
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
8 changes: 6 additions & 2 deletions x-pack/legacy/plugins/alerting/server/alerts_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -883,7 +883,6 @@ describe('enable()', () => {
schedule: { interval: '10s' },
alertTypeId: '2',
enabled: true,
scheduledTaskId: 'task-123',
updatedBy: 'elastic',
apiKey: null,
apiKeyOwner: null,
Expand All @@ -892,6 +891,9 @@ describe('enable()', () => {
version: '123',
}
);
expect(savedObjectsClient.update).toHaveBeenCalledWith('alert', '1', {
scheduledTaskId: 'task-123',
});
expect(taskManager.schedule).toHaveBeenCalledWith({
taskType: `alerting:2`,
params: {
Expand Down Expand Up @@ -964,7 +966,6 @@ describe('enable()', () => {
schedule: { interval: '10s' },
alertTypeId: '2',
enabled: true,
scheduledTaskId: 'task-123',
apiKey: Buffer.from('123:abc').toString('base64'),
apiKeyOwner: 'elastic',
updatedBy: 'elastic',
Expand All @@ -973,6 +974,9 @@ describe('enable()', () => {
version: '123',
}
);
expect(savedObjectsClient.update).toHaveBeenCalledWith('alert', '1', {
scheduledTaskId: 'task-123',
});
expect(taskManager.schedule).toHaveBeenCalledWith({
taskType: `alerting:2`,
params: {
Expand Down
5 changes: 2 additions & 3 deletions x-pack/legacy/plugins/alerting/server/alerts_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,6 @@ export class AlertsClient {
});

if (attributes.enabled === false) {
const scheduledTask = await this.scheduleAlert(id, attributes.alertTypeId);
const username = await this.getUserName();
await this.savedObjectsClient.update(
'alert',
Expand All @@ -372,11 +371,11 @@ export class AlertsClient {
enabled: true,
...this.apiKeyAsAlertAttributes(await this.createAPIKey(), username),
updatedBy: username,

scheduledTaskId: scheduledTask.id,
},
{ version }
);
const scheduledTask = await this.scheduleAlert(id, attributes.alertTypeId);
await this.savedObjectsClient.update('alert', id, { scheduledTaskId: scheduledTask.id });
await this.invalidateApiKey({ apiKey: attributes.apiKey });
}
}
Expand Down

0 comments on commit ab58a71

Please sign in to comment.