Skip to content

Commit

Permalink
[Task Manager] Changed alerts schedule logic to use Task Manager inte…
Browse files Browse the repository at this point in the history
…rnals (#80149)

* spiked intervals in alerts

* ensure scheduled tasks dont get wiped

* Fixed type checks and unit tests

* Added simple test, which only covers successful case when edit happened right after task was complete previous execution

* fixed jest

* fallback to existing task schedule when possible

* added missing test

* Added support for day and hour schedule interval values

* added docs for new schedule run result

* fixed doc

* added UnrecoverableError support for task runners nad pluged it into alerting where needed

* typo

Co-authored-by: Yuliia Naumenko <yuliia.naumenko@elastic.com>
  • Loading branch information
gmmorris and YulNaumenko authored Nov 2, 2020
1 parent 53978e3 commit 51acfb9
Show file tree
Hide file tree
Showing 26 changed files with 520 additions and 194 deletions.
15 changes: 12 additions & 3 deletions x-pack/plugins/alerts/server/alerts_client/alerts_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,11 @@ export class AlertsClient {
if (data.enabled) {
let scheduledTask;
try {
scheduledTask = await this.scheduleAlert(createdAlert.id, rawAlert.alertTypeId);
scheduledTask = await this.scheduleAlert(
createdAlert.id,
rawAlert.alertTypeId,
data.schedule
);
} catch (e) {
// Cleanup data, something went wrong scheduling the task
try {
Expand Down Expand Up @@ -697,7 +701,11 @@ export class AlertsClient {
this.invalidateApiKey({ apiKey: updateAttributes.apiKey });
throw e;
}
const scheduledTask = await this.scheduleAlert(id, attributes.alertTypeId);
const scheduledTask = await this.scheduleAlert(
id,
attributes.alertTypeId,
attributes.schedule as IntervalSchedule
);
await this.unsecuredSavedObjectsClient.update('alert', id, {
scheduledTaskId: scheduledTask.id,
});
Expand Down Expand Up @@ -933,9 +941,10 @@ export class AlertsClient {
]);
}

private async scheduleAlert(id: string, alertTypeId: string) {
private async scheduleAlert(id: string, alertTypeId: string, schedule: IntervalSchedule) {
return await this.taskManager.schedule({
taskType: `alerting:${alertTypeId}`,
schedule,
params: {
alertId: id,
spaceId: this.spaceId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,9 @@ describe('create()', () => {
"alertId": "1",
"spaceId": "default",
},
"schedule": Object {
"interval": "10s",
},
"scope": Array [
"alerting",
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ describe('enable()', () => {
alertId: '1',
spaceId: 'default',
},
schedule: {
interval: '10s',
},
state: {
alertInstances: {},
alertTypeState: {},
Expand Down
27 changes: 0 additions & 27 deletions x-pack/plugins/alerts/server/task_runner/get_next_run_at.test.ts

This file was deleted.

17 changes: 0 additions & 17 deletions x-pack/plugins/alerts/server/task_runner/get_next_run_at.ts

This file was deleted.

Loading

0 comments on commit 51acfb9

Please sign in to comment.