-
Notifications
You must be signed in to change notification settings - Fork 8.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Response Ops] Keep task document when enabling/disabling rules #139826
Changes from all commits
ff61e14
1a56865
b9c2d86
531ceb6
bdff214
feff3da
69397ff
f239fe9
2fa3a44
638852e
0869306
46bfc04
0833943
fa4a907
a2db534
4ee56b7
482d58d
4dd9ade
95b9e8c
8dc3c62
869be57
397495e
1123e3a
c9390eb
bb04cff
2e25e39
3bd7a3a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -384,7 +384,7 @@ export interface GetActionErrorLogByIdParams { | |
sort: estypes.Sort; | ||
} | ||
|
||
interface ScheduleRuleOptions { | ||
interface ScheduleTaskOptions { | ||
id: string; | ||
consumer: string; | ||
ruleTypeId: string; | ||
|
@@ -589,7 +589,7 @@ export class RulesClient { | |
if (data.enabled) { | ||
let scheduledTask; | ||
try { | ||
scheduledTask = await this.scheduleRule({ | ||
scheduledTask = await this.scheduleTask({ | ||
id: createdAlert.id, | ||
consumer: data.consumer, | ||
ruleTypeId: rawRule.alertTypeId, | ||
|
@@ -2138,7 +2138,24 @@ export class RulesClient { | |
} catch (e) { | ||
throw e; | ||
} | ||
const scheduledTask = await this.scheduleRule({ | ||
} | ||
|
||
let scheduledTaskIdToCreate: string | null = null; | ||
ymao1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (attributes.scheduledTaskId) { | ||
// If scheduledTaskId defined in rule SO, make sure it exists | ||
try { | ||
await this.taskManager.get(attributes.scheduledTaskId); | ||
ymao1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} catch (err) { | ||
scheduledTaskIdToCreate = id; | ||
} | ||
} else { | ||
// If scheduledTaskId doesn't exist in rule SO, set it to rule ID | ||
scheduledTaskIdToCreate = id; | ||
} | ||
|
||
if (scheduledTaskIdToCreate) { | ||
// Schedule the task if it doesn't exist | ||
const scheduledTask = await this.scheduleTask({ | ||
id, | ||
consumer: attributes.consumer, | ||
ruleTypeId: attributes.alertTypeId, | ||
|
@@ -2148,6 +2165,9 @@ export class RulesClient { | |
await this.unsecuredSavedObjectsClient.update('alert', id, { | ||
scheduledTaskId: scheduledTask.id, | ||
}); | ||
} else { | ||
// Task exists so set enabled to true | ||
await this.taskManager.bulkEnableDisable([attributes.scheduledTaskId!], true); | ||
} | ||
} | ||
|
||
|
@@ -2282,14 +2302,21 @@ export class RulesClient { | |
this.updateMeta({ | ||
...attributes, | ||
enabled: false, | ||
scheduledTaskId: null, | ||
scheduledTaskId: attributes.scheduledTaskId === id ? attributes.scheduledTaskId : null, | ||
updatedBy: await this.getUserName(), | ||
updatedAt: new Date().toISOString(), | ||
}), | ||
{ version } | ||
); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This update to the disable logic takes into account that there may be pre 8.0 rules running where the scheduled task ID does not equal the rule ID. In these cases, we still want to remove the task document so that a new one matching the rule id can be created on enable. If the scheduledTaskId already matches the rule ID, this will set the task to disabled. |
||
// If the scheduledTaskId does not match the rule id, we should | ||
// remove the task, otherwise mark the task as disabled | ||
if (attributes.scheduledTaskId) { | ||
await this.taskManager.removeIfExists(attributes.scheduledTaskId); | ||
if (attributes.scheduledTaskId !== id) { | ||
await this.taskManager.removeIfExists(attributes.scheduledTaskId); | ||
} else { | ||
await this.taskManager.bulkEnableDisable([attributes.scheduledTaskId], false); | ||
} | ||
} | ||
} | ||
} | ||
|
@@ -2767,7 +2794,7 @@ export class RulesClient { | |
return this.spaceId; | ||
} | ||
|
||
private async scheduleRule(opts: ScheduleRuleOptions) { | ||
private async scheduleTask(opts: ScheduleTaskOptions) { | ||
const { id, consumer, ruleTypeId, schedule, throwOnConflict } = opts; | ||
const taskInstance = { | ||
id, // use the same ID for task document as the rule | ||
|
@@ -2784,6 +2811,7 @@ export class RulesClient { | |
alertInstances: {}, | ||
}, | ||
scope: ['alerting'], | ||
enabled: true, | ||
}; | ||
try { | ||
return await this.taskManager.schedule(taskInstance); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This update to the enable logic takes into account the fact that there may be already disabled rules with no corresponding task.
If the rule is disabled with no corresponding task, it's scheduledTaskId will be undefined. In this case, we want to schedule a task on enable.
If a rule somehow has a scheduledTaskId defined but it doesn't actually exist, we want to schedule a task on enable.
Finally, if a rule has a scheduledTaskId defined and the task exists, we enable the task.