Skip to content

Commit b665bd5

Browse files
authored
[Fleet] Refactor abortController usage in Fleet task (#232814)
## Summary Closes #216804 Most of Fleet tasks were fixed in #230017 Tested locally by calling `abortController.abort()` to the first task run, and then verifying that the task still runs next time. ``` [2025-08-26T09:56:08.158+02:00][INFO ][plugins.fleet.fleet:agent-status-change-task:1.0.3] [runTask()] started [2025-08-26T09:56:08.158+02:00][INFO ][plugins.fleet.fleet:agent-status-change-task:1.0.3] [runTask()] abort task [2025-08-26T09:56:08.361+02:00][ERROR][plugins.fleet.fleet:agent-status-change-task:1.0.3] [AgentStatusChangeTask] error: Error: Task was aborted [2025-08-26T09:56:08.361+02:00][INFO ][plugins.fleet.fleet:agent-status-change-task:1.0.3] [AgentStatusChangeTask] runTask ended: error [2025-08-26T09:56:21.114+02:00][INFO ][plugins.fleet.fleet:agent-status-change-task:1.0.3] [runTask()] started [2025-08-26T09:56:21.296+02:00][DEBUG][plugins.fleet.fleet:agent-status-change-task:1.0.3] [AgentStatusChangeTask] Recording 10000 status changes [2025-08-26T09:56:24.215+02:00][DEBUG][plugins.fleet.fleet:agent-status-change-task:1.0.3] [AgentStatusChangeTask] Recording 10000 status changes [2025-08-26T09:56:26.294+02:00][DEBUG][plugins.fleet.fleet:agent-status-change-task:1.0.3] [AgentStatusChangeTask] Recording 10000 status changes [2025-08-26T09:56:28.357+02:00][DEBUG][plugins.fleet.fleet:agent-status-change-task:1.0.3] [AgentStatusChangeTask] Recording 10000 status changes [2025-08-26T09:56:30.367+02:00][DEBUG][plugins.fleet.fleet:agent-status-change-task:1.0.3] [AgentStatusChangeTask] Recording 10000 status changes [2025-08-26T09:56:32.404+02:00][INFO ][plugins.fleet.fleet:agent-status-change-task:1.0.3] [AgentStatusChangeTask] runTask ended: success [2025-08-26T09:56:33.109+02:00][INFO ][plugins.fleet.fleet:agent-status-change-task:1.0.3] [runTask()] started [2025-08-26T09:56:33.481+02:00][INFO ][plugins.fleet.fleet:agent-status-change-task:1.0.3] [AgentStatusChangeTask] runTask ended: success [2025-08-26T09:56:45.117+02:00][INFO ][plugins.fleet.fleet:agent-status-change-task:1.0.3] [runTask()] started [2025-08-26T09:56:45.496+02:00][INFO ][plugins.fleet.fleet:agent-status-change-task:1.0.3] [AgentStatusChangeTask] runTask ended: success [2025-08-26T09:56:54.120+02:00][INFO ][plugins.fleet.fleet:agent-status-change-task:1.0.3] [runTask()] started [2025-08-26T09:56:54.508+02:00][INFO ][plugins.fleet.fleet:agent-status-change-task:1.0.3] [AgentStatusChangeTask] runTask ended: success ``` ### Checklist Check the PR satisfies following conditions. Reviewers should verify this PR satisfies this list as well. - [ ] Any text added follows [EUI's writing guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses sentence case text and includes [i18n support](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md) - [ ] [Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html) was added for features that require explanation or tutorials - [ ] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios - [ ] If a plugin configuration key changed, check if it needs to be allowlisted in the cloud and added to the [docker list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker) - [ ] This was checked for breaking HTTP API changes, and any breaking changes have been approved by the breaking-change committee. The `release_note:breaking` label should be applied in these situations. - [ ] [Flaky Test Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was used on any tests changed - [ ] The PR description includes the appropriate Release Notes section, and the correct `release_note:*` label is applied per the [guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process) - [ ] Review the [backport guidelines](https://docs.google.com/document/d/1VyN5k91e5OVumlc0Gb9RPa3h1ewuPE705nRtioPiTvY/edit?usp=sharing) and apply applicable `backport:*` labels. ### Identify risks Does this PR introduce any risks? For example, consider risks like hard to test bugs, performance regression, potential of data loss. Describe the risk, its severity, and mitigation for each identified risk. Invite stakeholders and evaluate how to proceed before merging. - [ ] [See some risk examples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx) - [ ] ...
1 parent 4574388 commit b665bd5

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

x-pack/platform/plugins/shared/fleet/server/tasks/agent_status_change_task.ts

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import { SO_SEARCH_LIMIT } from '../constants';
2727
import { getAgentPolicySavedObjectType } from '../services/agent_policy';
2828

2929
export const TYPE = 'fleet:agent-status-change-task';
30-
export const VERSION = '1.0.0';
30+
export const VERSION = '1.0.1';
3131
const TITLE = 'Fleet Agent Status Change Task';
3232
const SCOPE = ['fleet'];
3333
const DEFAULT_INTERVAL = '1m';
@@ -58,7 +58,6 @@ interface AgentStatusChangeTaskStartContract {
5858
export class AgentStatusChangeTask {
5959
private logger: Logger;
6060
private wasStarted: boolean = false;
61-
private abortController?: AbortController;
6261
private taskInterval: string;
6362

6463
constructor(setupContract: AgentStatusChangeTaskSetupContract) {
@@ -70,15 +69,18 @@ export class AgentStatusChangeTask {
7069
[TYPE]: {
7170
title: TITLE,
7271
timeout: TIMEOUT,
73-
createTaskRunner: ({ taskInstance }: { taskInstance: ConcreteTaskInstance }) => {
72+
createTaskRunner: ({
73+
taskInstance,
74+
abortController,
75+
}: {
76+
taskInstance: ConcreteTaskInstance;
77+
abortController: AbortController;
78+
}) => {
7479
return {
7580
run: async () => {
76-
this.abortController = new AbortController();
77-
return this.runTask(taskInstance, core);
78-
},
79-
cancel: async () => {
80-
this.abortController?.abort('Task timed out');
81+
return this.runTask(taskInstance, core, abortController);
8182
},
83+
cancel: async () => {},
8284
};
8385
},
8486
},
@@ -118,7 +120,11 @@ export class AgentStatusChangeTask {
118120
this.logger.info(`[AgentStatusChangeTask] runTask ended${msg ? ': ' + msg : ''}`);
119121
}
120122

121-
public runTask = async (taskInstance: ConcreteTaskInstance, core: CoreSetup) => {
123+
public runTask = async (
124+
taskInstance: ConcreteTaskInstance,
125+
core: CoreSetup,
126+
abortController: AbortController
127+
) => {
122128
if (!appContextService.getExperimentalFeatures().enableAgentStatusAlerting) {
123129
this.logger.debug(
124130
'[AgentStatusChangeTask] Aborting runTask: agent status alerting feature is disabled'
@@ -144,7 +150,7 @@ export class AgentStatusChangeTask {
144150
const soClient = new SavedObjectsClient(coreStart.savedObjects.createInternalRepository());
145151

146152
try {
147-
await this.persistAgentStatusChanges(esClient, soClient);
153+
await this.persistAgentStatusChanges(esClient, soClient, abortController);
148154

149155
this.endRun('success');
150156
} catch (err) {
@@ -160,7 +166,8 @@ export class AgentStatusChangeTask {
160166

161167
private persistAgentStatusChanges = async (
162168
esClient: ElasticsearchClient,
163-
soClient: SavedObjectsClientContract
169+
soClient: SavedObjectsClientContract,
170+
abortController: AbortController
164171
) => {
165172
let agentlessPolicies: string[] | undefined;
166173
const agentsFetcher = await fetchAllAgentsByKuery(esClient, soClient, {
@@ -176,7 +183,7 @@ export class AgentStatusChangeTask {
176183
const agentsToUpdate = [];
177184

178185
for (const agent of agentPageResults) {
179-
this.throwIfAborted();
186+
this.throwIfAborted(abortController);
180187

181188
if (agent.status !== agent.last_known_status) {
182189
agentsToUpdate.push(agent);
@@ -262,8 +269,8 @@ export class AgentStatusChangeTask {
262269
});
263270
};
264271

265-
private throwIfAborted() {
266-
if (this.abortController?.signal.aborted) {
272+
private throwIfAborted(abortController: AbortController) {
273+
if (abortController.signal.aborted) {
267274
throw new Error('Task was aborted');
268275
}
269276
}

0 commit comments

Comments
 (0)