Fix unenroll inactive agent tasks if first set of agents returned is equal to UNENROLLMENT_BATCH_SIZE (#216283)

## Summary

Fix the unenrollment inactive agent task to be able to unenroll some
agents when the number of agents returned by the first
`getAgentsByKuery` trigger is equal to the limit of
`UNENROLLMENT_BATCH_SIZE`.


### 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
- [x] [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)

### 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)
- [ ] ...

---------

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
This commit is contained in:
Mario Rodriguez Molins 2025-03-31 15:55:14 +02:00 committed by GitHub
parent f270bd5956
commit 8728a23282
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 9 additions and 3 deletions

View file

@ -57,6 +57,7 @@ describe('UnenrollInactiveAgentsTask', () => {
let mockTaskManagerSetup: jest.Mocked<TaskManagerSetupContract>;
const mockedUnenrollBatch = jest.mocked(unenrollBatch);
const unenrollBatchSize = 3;
const agents = [
{
id: 'agent-1',
@ -91,6 +92,7 @@ describe('UnenrollInactiveAgentsTask', () => {
core: mockCore,
taskManager: mockTaskManagerSetup,
logFactory: loggingSystemMock.create(),
unenrollBatchSize,
});
});

View file

@ -40,6 +40,7 @@ interface UnenrollInactiveAgentsTaskSetupContract {
core: CoreSetup;
taskManager: TaskManagerSetupContract;
logFactory: LoggerFactory;
unenrollBatchSize?: number;
}
interface UnenrollInactiveAgentsTaskStartContract {
@ -50,10 +51,13 @@ export class UnenrollInactiveAgentsTask {
private logger: Logger;
private wasStarted: boolean = false;
private abortController = new AbortController();
private unenrollBatchSize: number;
constructor(setupContract: UnenrollInactiveAgentsTaskSetupContract) {
const { core, taskManager, logFactory } = setupContract;
const { core, taskManager, logFactory, unenrollBatchSize } = setupContract;
this.logger = logFactory.get(this.taskId);
this.unenrollBatchSize =
unenrollBatchSize !== undefined ? unenrollBatchSize : UNENROLLMENT_BATCHSIZE;
taskManager.registerTaskDefinitions({
[TYPE]: {
@ -140,7 +144,7 @@ export class UnenrollInactiveAgentsTask {
kuery,
showInactive: true,
page: 1,
perPage: UNENROLLMENT_BATCHSIZE,
perPage: this.unenrollBatchSize,
});
if (!res.agents.length) {
this.logger.debug(
@ -149,7 +153,7 @@ export class UnenrollInactiveAgentsTask {
continue;
}
agentCounter += res.agents.length;
if (agentCounter >= UNENROLLMENT_BATCHSIZE) {
if (agentCounter > this.unenrollBatchSize) {
this.endRun('Reached the maximum amount of agents to unenroll, exiting.');
return;
}