Skip to content

Commit

Permalink
run failure handling in dashboard plugin start
Browse files Browse the repository at this point in the history
  • Loading branch information
ersin-erdal committed Jun 27, 2022
1 parent 9951dcf commit 44dadf9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
18 changes: 18 additions & 0 deletions src/plugins/dashboard/server/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { DashboardPlugin } from './plugin';
import { coreMock } from '@kbn/core/server/mocks';
import { taskManagerMock } from '@kbn/task-manager-plugin/server/mocks';
import { scheduleDashboardTelemetry, TASK_ID } from './usage/dashboard_telemetry_collection_task';
import { Logger } from '@kbn/core/server';

jest.mock('./usage/dashboard_telemetry_collection_task', () => ({
scheduleDashboardTelemetry: jest.fn().mockResolvedValue('ok'),
Expand All @@ -21,11 +22,17 @@ describe('DashboardPlugin', () => {
let mockCoreStart: ReturnType<typeof coreMock.createStart>;
let initContext: ReturnType<typeof coreMock.createPluginInitializerContext>;
let mockTaskManager: ReturnType<typeof taskManagerMock.createStart>;
let mockLogger: Logger;

beforeEach(() => {
mockCoreStart = coreMock.createStart();
mockTaskManager = taskManagerMock.createStart();
initContext = coreMock.createPluginInitializerContext();
mockLogger = initContext.logger.get();
});

afterEach(() => {
jest.clearAllMocks();
});

test('should call mockTaskManager.runSoon', async () => {
Expand All @@ -37,5 +44,16 @@ describe('DashboardPlugin', () => {
expect(await mockTaskManager.runSoon).toHaveBeenCalledTimes(1);
expect(await mockTaskManager.runSoon).toHaveBeenCalledWith(TASK_ID);
});

test('error from runSoon is handled gracefully', async () => {
const dashboardPlugin = new DashboardPlugin(initContext);
mockTaskManager.runSoon.mockRejectedValueOnce(500);
const response = dashboardPlugin.start(mockCoreStart, {
taskManager: mockTaskManager,
});
expect(scheduleDashboardTelemetry).toHaveBeenCalledTimes(1);
expect(await mockTaskManager.runSoon).toHaveBeenCalledTimes(1);
expect(response).toEqual({});
});
});
});
4 changes: 2 additions & 2 deletions src/plugins/dashboard/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ export class DashboardPlugin

if (plugins.taskManager) {
scheduleDashboardTelemetry(this.logger, plugins.taskManager)
.then(() => {
plugins.taskManager.runSoon(TASK_ID);
.then(async () => {
await plugins.taskManager.runSoon(TASK_ID);
})
.catch((e) => {
this.logger.debug(`Error scheduling task, received ${e.message}`);
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/task_manager/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ The danger is that in such a situation, a Task with that same `id` might already
To achieve this you should use the `ensureScheduling` api which has the exact same behavior as `schedule`, except it allows the scheduling of a Task with an `id` that's already in assigned to another Task and it will assume that the existing Task is the one you wished to `schedule`, treating this as a successful operation.

#### runSoon
Using `runSoon` you can instruct TaskManger to run an existing task on-demand, without waiting for its scheduled time to be reached.
Using `runSoon` you can instruct TaskManager to run an existing task as soon as possible by updating the next scheduled run date to be now

```js
export class Plugin {
Expand Down

0 comments on commit 44dadf9

Please sign in to comment.