Skip to content
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

Make stop method of the taskManager plugin async #191218

Merged
merged 2 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -193,19 +193,5 @@ describe('KibanaDiscoveryService', () => {
'Removed this node from the Kibana Discovery Service'
);
});

it('logs an error when failed', async () => {
savedObjectsRepository.delete.mockRejectedValue(new Error('bar'));

const kibanaDiscoveryService = new KibanaDiscoveryService({
savedObjectsRepository,
logger,
currentNode,
});

await kibanaDiscoveryService.deleteCurrentNode();

expect(logger.error).toHaveBeenCalledWith('Deleting current node has failed. error: bar');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,7 @@ export class KibanaDiscoveryService {
}

public async deleteCurrentNode() {
try {
await this.savedObjectsRepository.delete(BACKGROUND_TASK_NODE_SO_NAME, this.currentNode);
this.logger.info('Removed this node from the Kibana Discovery Service');
} catch (e) {
this.logger.error(`Deleting current node has failed. error: ${e.message}`);
}
await this.savedObjectsRepository.delete(BACKGROUND_TASK_NODE_SO_NAME, this.currentNode);
this.logger.info('Removed this node from the Kibana Discovery Service');
}
}
24 changes: 24 additions & 0 deletions x-pack/plugins/task_manager/server/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/

import { TaskManagerPlugin, getElasticsearchAndSOAvailability } from './plugin';
import { KibanaDiscoveryService } from './kibana_discovery_service';

import { coreMock } from '@kbn/core/server/mocks';
import { TaskManagerConfig } from './config';
import { Subject } from 'rxjs';
Expand Down Expand Up @@ -37,6 +39,9 @@ jest.mock('./ephemeral_task_lifecycle', () => {
};
});

const deleteCurrentNodeSpy = jest.spyOn(KibanaDiscoveryService.prototype, 'deleteCurrentNode');
const discoveryIsStarted = jest.spyOn(KibanaDiscoveryService.prototype, 'isStarted');

const coreStart = coreMock.createStart();
const pluginInitializerContextParams = {
max_attempts: 9,
Expand Down Expand Up @@ -176,6 +181,25 @@ describe('TaskManagerPlugin', () => {
});
});

describe('stop', () => {
test('should remove the current from discovery service', async () => {
const pluginInitializerContext = coreMock.createPluginInitializerContext<TaskManagerConfig>(
pluginInitializerContextParams
);
pluginInitializerContext.node.roles.backgroundTasks = true;
const taskManagerPlugin = new TaskManagerPlugin(pluginInitializerContext);
taskManagerPlugin.setup(coreMock.createSetup(), { usageCollection: undefined });
taskManagerPlugin.start(coreStart, {
cloud: cloudMock.createStart(),
});

discoveryIsStarted.mockReturnValueOnce(true);
await taskManagerPlugin.stop();

expect(deleteCurrentNodeSpy).toHaveBeenCalledTimes(1);
});
});

describe('getElasticsearchAndSOAvailability', () => {
test('returns true when both services are available', async () => {
const core$ = new Subject<CoreStatus>();
Expand Down
8 changes: 6 additions & 2 deletions x-pack/plugins/task_manager/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,9 +395,13 @@ export class TaskManagerPlugin
};
}

public stop() {
public async stop() {
if (this.kibanaDiscoveryService?.isStarted()) {
this.kibanaDiscoveryService.deleteCurrentNode().catch(() => {});
try {
await this.kibanaDiscoveryService.deleteCurrentNode();
} catch (e) {
this.logger.error(`Deleting current node has failed. error: ${e.message}`);
}
}
}
}
Expand Down