From f4fb9cf7bcb0a0b4dfe24d7bfc897352b4e57caa Mon Sep 17 00:00:00 2001 From: Mike Cote Date: Thu, 8 Aug 2019 13:47:58 -0400 Subject: [PATCH 1/3] Expose task manager as plugin instead of server attribute --- x-pack/legacy/plugins/actions/server/init.ts | 2 +- x-pack/legacy/plugins/alerting/server/init.ts | 2 +- .../maps_telemetry/maps_usage_collector.js | 5 ++- .../server/maps_telemetry/telemetry_task.js | 2 +- .../plugins/maps/server/test_utils/index.js | 10 ++--- .../legacy/plugins/oss_telemetry/index.d.ts | 18 ++++---- .../visualizations/get_usage_collector.ts | 2 +- .../oss_telemetry/server/lib/tasks/index.ts | 4 +- .../plugins/oss_telemetry/test_utils/index.ts | 10 ++--- x-pack/legacy/plugins/task_manager/README.md | 8 ++-- .../task_manager/{index.js => index.ts} | 43 ++++++++++++++----- .../plugins/task_manager/task_manager.mock.ts | 6 +-- .../plugins/task_manager/index.js | 2 +- .../plugins/task_manager/init_routes.js | 2 +- x-pack/test/typings/hapi.d.ts | 4 +- x-pack/typings/hapi.d.ts | 4 +- 16 files changed, 70 insertions(+), 54 deletions(-) rename x-pack/legacy/plugins/task_manager/{index.js => index.ts} (60%) diff --git a/x-pack/legacy/plugins/actions/server/init.ts b/x-pack/legacy/plugins/actions/server/init.ts index 27cff53bb97d1..3316b69923e77 100644 --- a/x-pack/legacy/plugins/actions/server/init.ts +++ b/x-pack/legacy/plugins/actions/server/init.ts @@ -62,7 +62,7 @@ export function init(server: Legacy.Server) { }; } - const { taskManager } = server; + const taskManager = server.plugins.task_manager!; const actionTypeRegistry = new ActionTypeRegistry({ getServices, taskManager: taskManager!, diff --git a/x-pack/legacy/plugins/alerting/server/init.ts b/x-pack/legacy/plugins/alerting/server/init.ts index bf7b4b8009b97..947e98d0d9361 100644 --- a/x-pack/legacy/plugins/alerting/server/init.ts +++ b/x-pack/legacy/plugins/alerting/server/init.ts @@ -47,7 +47,7 @@ export function init(server: Legacy.Server) { }; } - const { taskManager } = server; + const taskManager = server.plugins.task_manager!; const alertTypeRegistry = new AlertTypeRegistry({ getServices, taskManager: taskManager!, diff --git a/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_usage_collector.js b/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_usage_collector.js index 58996a1afe41f..5f6361a16aa00 100644 --- a/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_usage_collector.js +++ b/x-pack/legacy/plugins/maps/server/maps_telemetry/maps_usage_collector.js @@ -9,7 +9,7 @@ import { TASK_ID, scheduleTask, registerMapsTelemetryTask } from './telemetry_ta export function initTelemetryCollection(server) { registerMapsTelemetryTask(server); - scheduleTask(server, server.taskManager); + scheduleTask(server, server.plugins.task_manager); registerMapsUsageCollector(server); } @@ -20,8 +20,9 @@ async function isTaskManagerReady(server) { async function fetch(server) { let docs; + const taskManager = server.plugins.task_manager; try { - ({ docs } = await server.taskManager.fetch({ + ({ docs } = await taskManager.fetch({ query: { bool: { filter: { diff --git a/x-pack/legacy/plugins/maps/server/maps_telemetry/telemetry_task.js b/x-pack/legacy/plugins/maps/server/maps_telemetry/telemetry_task.js index e3f54335eeeed..6e134a483018f 100644 --- a/x-pack/legacy/plugins/maps/server/maps_telemetry/telemetry_task.js +++ b/x-pack/legacy/plugins/maps/server/maps_telemetry/telemetry_task.js @@ -35,7 +35,7 @@ export function scheduleTask(server, taskManager) { } export function registerMapsTelemetryTask(server) { - const taskManager = server.taskManager; + const taskManager = server.plugins.task_manager; taskManager.registerTaskDefinitions({ [TELEMETRY_TASK_TYPE]: { title: 'Maps telemetry fetch task', diff --git a/x-pack/legacy/plugins/maps/server/test_utils/index.js b/x-pack/legacy/plugins/maps/server/test_utils/index.js index 3de792e79c9e3..13b7c56d6fc8b 100644 --- a/x-pack/legacy/plugins/maps/server/test_utils/index.js +++ b/x-pack/legacy/plugins/maps/server/test_utils/index.js @@ -27,11 +27,6 @@ export const getMockTaskFetch = (docs = defaultMockTaskDocs) => { export const getMockKbnServer = ( mockCallWithInternal = getMockCallWithInternal(), mockTaskFetch = getMockTaskFetch()) => ({ - taskManager: { - registerTaskDefinitions: () => undefined, - schedule: () => Promise.resolve(), - fetch: mockTaskFetch, - }, plugins: { elasticsearch: { getCluster: () => ({ @@ -39,6 +34,11 @@ export const getMockKbnServer = ( }), }, xpack_main: {}, + task_manager: { + registerTaskDefinitions: () => undefined, + schedule: () => Promise.resolve(), + fetch: mockTaskFetch, + }, }, usage: { collectorSet: { diff --git a/x-pack/legacy/plugins/oss_telemetry/index.d.ts b/x-pack/legacy/plugins/oss_telemetry/index.d.ts index 8805292c7dc4c..9f735c676fe6d 100644 --- a/x-pack/legacy/plugins/oss_telemetry/index.d.ts +++ b/x-pack/legacy/plugins/oss_telemetry/index.d.ts @@ -35,15 +35,6 @@ export interface TaskInstance { } export interface HapiServer { - taskManager: { - registerTaskDefinitions: (opts: any) => void; - schedule: (opts: any) => Promise; - fetch: ( - opts: any - ) => Promise<{ - docs: TaskInstance[]; - }>; - }; plugins: { xpack_main: any; elasticsearch: { @@ -53,6 +44,15 @@ export interface HapiServer { callWithInternalUser: () => Promise; }; }; + task_manager: { + registerTaskDefinitions: (opts: any) => void; + schedule: (opts: any) => Promise; + fetch: ( + opts: any + ) => Promise<{ + docs: TaskInstance[]; + }>; + }; }; usage: { collectorSet: { diff --git a/x-pack/legacy/plugins/oss_telemetry/server/lib/collectors/visualizations/get_usage_collector.ts b/x-pack/legacy/plugins/oss_telemetry/server/lib/collectors/visualizations/get_usage_collector.ts index 2db431af7a4a2..2bbecf99b97c3 100644 --- a/x-pack/legacy/plugins/oss_telemetry/server/lib/collectors/visualizations/get_usage_collector.ts +++ b/x-pack/legacy/plugins/oss_telemetry/server/lib/collectors/visualizations/get_usage_collector.ts @@ -14,7 +14,7 @@ async function isTaskManagerReady(server: HapiServer) { } async function fetch(server: HapiServer) { - const { taskManager } = server; + const taskManager = server.plugins.task_manager!; let docs; try { diff --git a/x-pack/legacy/plugins/oss_telemetry/server/lib/tasks/index.ts b/x-pack/legacy/plugins/oss_telemetry/server/lib/tasks/index.ts index 93e6830e5f56e..1e7bff8564a8a 100644 --- a/x-pack/legacy/plugins/oss_telemetry/server/lib/tasks/index.ts +++ b/x-pack/legacy/plugins/oss_telemetry/server/lib/tasks/index.ts @@ -9,7 +9,7 @@ import { PLUGIN_ID, VIS_TELEMETRY_TASK, VIS_TELEMETRY_TASK_NUM_WORKERS } from '. import { visualizationsTaskRunner } from './visualizations/task_runner'; export function registerTasks(server: HapiServer) { - const { taskManager } = server; + const taskManager = server.plugins.task_manager; taskManager.registerTaskDefinitions({ [VIS_TELEMETRY_TASK]: { @@ -26,7 +26,7 @@ export function registerTasks(server: HapiServer) { } export function scheduleTasks(server: HapiServer) { - const { taskManager } = server; + const taskManager = server.plugins.task_manager; const { kbnServer } = server.plugins.xpack_main.status.plugin; kbnServer.afterPluginsInit(() => { diff --git a/x-pack/legacy/plugins/oss_telemetry/test_utils/index.ts b/x-pack/legacy/plugins/oss_telemetry/test_utils/index.ts index aee84fddb322d..7659f0d3516f9 100644 --- a/x-pack/legacy/plugins/oss_telemetry/test_utils/index.ts +++ b/x-pack/legacy/plugins/oss_telemetry/test_utils/index.ts @@ -34,11 +34,6 @@ export const getMockKbnServer = ( mockCallWithInternal = getMockCallWithInternal(), mockTaskFetch = getMockTaskFetch() ): HapiServer => ({ - taskManager: { - registerTaskDefinitions: (opts: any) => undefined, - schedule: (opts: any) => Promise.resolve(), - fetch: mockTaskFetch, - }, plugins: { elasticsearch: { getCluster: (cluster: string) => ({ @@ -46,6 +41,11 @@ export const getMockKbnServer = ( }), }, xpack_main: {}, + task_manager: { + registerTaskDefinitions: (opts: any) => undefined, + schedule: (opts: any) => Promise.resolve(), + fetch: mockTaskFetch, + }, }, usage: { collectorSet: { diff --git a/x-pack/legacy/plugins/task_manager/README.md b/x-pack/legacy/plugins/task_manager/README.md index 217037c1b8b83..adf7706443695 100644 --- a/x-pack/legacy/plugins/task_manager/README.md +++ b/x-pack/legacy/plugins/task_manager/README.md @@ -50,12 +50,12 @@ The task_manager can be configured via `taskManager` config options (e.g. `taskM ## Task definitions -Plugins define tasks by calling the `registerTaskDefinitions` method on the `server.taskManager` object. +Plugins define tasks by calling the `registerTaskDefinitions` method on the `server.plugins.task_manager` object. A sample task can be found in the [x-pack/test/plugin_api_integration/plugins/task_manager](../../test/plugin_api_integration/plugins/task_manager/index.js) folder. ```js -const { taskManager } = server; +const taskManager = server.plugins.task_manager; taskManager.registerTaskDefinitions({ // clusterMonitoring is the task type, and must be unique across the entire system clusterMonitoring: { @@ -215,7 +215,7 @@ The data stored for a task instance looks something like this: The task manager mixin exposes a taskManager object on the Kibana server which plugins can use to manage scheduled tasks. Each method takes an optional `scope` argument and ensures that only tasks with the specified scope(s) will be affected. ```js -const { taskManager } = server; +const taskManager = server.plugins.task_manager; // Schedules a task. All properties are as documented in the previous // storage section, except that here, params is an object, not a JSON // string. @@ -258,7 +258,7 @@ For example: ```js // In your plugin's init -server.taskManager.addMiddleware({ +server.plugins.task_manager.addMiddleware({ async beforeSave({ taskInstance, ...opts }) { console.log(`About to save a task of type ${taskInstance.taskType}`); diff --git a/x-pack/legacy/plugins/task_manager/index.js b/x-pack/legacy/plugins/task_manager/index.ts similarity index 60% rename from x-pack/legacy/plugins/task_manager/index.js rename to x-pack/legacy/plugins/task_manager/index.ts index 2ab880938c792..0701f77539cde 100644 --- a/x-pack/legacy/plugins/task_manager/index.js +++ b/x-pack/legacy/plugins/task_manager/index.ts @@ -4,21 +4,29 @@ * you may not use this file except in compliance with the Elastic License. */ +import { Root } from 'joi'; +import { Legacy } from 'kibana'; import { SavedObjectsSerializer, SavedObjectsSchema } from '../../../../src/core/server'; -import { TaskManager } from './task_manager'; +import { TaskManager as TaskMaangerClass } from './task_manager'; import mappings from './mappings.json'; import { migrations } from './migrations'; +import { TaskManager } from './types'; -export function taskManager(kibana) { +export { TaskManager }; +export { TaskInstance, ConcreteTaskInstance, TaskRunCreatorFunction } from './task'; + +export function taskManager(kibana: any) { return new kibana.Plugin({ id: 'task_manager', require: ['kibana', 'elasticsearch', 'xpack_main'], configPrefix: 'xpack.task_manager', - config(Joi) { + config(Joi: Root) { return Joi.object({ enabled: Joi.boolean().default(true), max_attempts: Joi.number() - .description('The maximum number of times a task will be attempted before being abandoned as failed') + .description( + 'The maximum number of times a task will be attempted before being abandoned as failed' + ) .min(1) .default(3), poll_interval: Joi.number() @@ -29,16 +37,20 @@ export function taskManager(kibana) { .description('The name of the index used to store task information.') .default('.kibana_task_manager'), max_workers: Joi.number() - .description('The maximum number of tasks that this Kibana instance will run simultaneously.') + .description( + 'The maximum number of tasks that this Kibana instance will run simultaneously.' + ) .min(1) // disable the task manager rather than trying to specify it with 0 workers .default(10), override_num_workers: Joi.object() .pattern(/.*/, Joi.number().greater(0)) - .description('Customize the number of workers occupied by specific tasks (e.g. override_num_workers.reporting: 2)') - .default({}) + .description( + 'Customize the number of workers occupied by specific tasks (e.g. override_num_workers.reporting: 2)' + ) + .default({}), }).default(); }, - init(server) { + init(server: Legacy.Server) { const config = server.config(); const schema = new SavedObjectsSchema(this.kbnServer.uiExports.savedObjectSchemas); const serializer = new SavedObjectsSerializer(schema); @@ -48,13 +60,22 @@ export function taskManager(kibana) { ['task'] ); - const taskManager = new TaskManager({ + const taskManagerInstance = new TaskMaangerClass({ kbnServer: this.kbnServer, config, savedObjectsRepository, serializer, }); - server.decorate('server', 'taskManager', taskManager); + const exposedFunctions: TaskManager = { + fetch: taskManagerInstance.fetch.bind(taskManagerInstance), + remove: taskManagerInstance.remove.bind(taskManagerInstance), + schedule: taskManagerInstance.schedule.bind(taskManagerInstance), + addMiddleware: taskManagerInstance.addMiddleware.bind(taskManagerInstance), + registerTaskDefinitions: taskManagerInstance.registerTaskDefinitions.bind( + taskManagerInstance + ), + }; + server.expose(exposedFunctions); }, uiExports: { mappings, @@ -64,7 +85,7 @@ export function taskManager(kibana) { hidden: true, isNamespaceAgnostic: true, convertToAliasScript: `ctx._id = ctx._source.type + ':' + ctx._id`, - indexPattern(config) { + indexPattern(config: any) { return config.get('xpack.task_manager.index'); }, }, diff --git a/x-pack/legacy/plugins/task_manager/task_manager.mock.ts b/x-pack/legacy/plugins/task_manager/task_manager.mock.ts index 7b3b2671cdd35..9a978508c3a16 100644 --- a/x-pack/legacy/plugins/task_manager/task_manager.mock.ts +++ b/x-pack/legacy/plugins/task_manager/task_manager.mock.ts @@ -4,12 +4,10 @@ * you may not use this file except in compliance with the Elastic License. */ -import { TaskManager } from './task_manager'; - -type Schema = PublicMethodsOf; +import { TaskManager } from './types'; const createTaskManagerMock = () => { - const mocked: jest.Mocked = { + const mocked: jest.Mocked = { registerTaskDefinitions: jest.fn(), addMiddleware: jest.fn(), schedule: jest.fn(), diff --git a/x-pack/test/plugin_api_integration/plugins/task_manager/index.js b/x-pack/test/plugin_api_integration/plugins/task_manager/index.js index ad9ff9f668c41..5806d57a8a0f2 100644 --- a/x-pack/test/plugin_api_integration/plugins/task_manager/index.js +++ b/x-pack/test/plugin_api_integration/plugins/task_manager/index.js @@ -18,7 +18,7 @@ export default function (kibana) { }, init(server) { - const { taskManager } = server; + const taskManager = server.plugins.task_manager; taskManager.registerTaskDefinitions({ sampleTask: { diff --git a/x-pack/test/plugin_api_integration/plugins/task_manager/init_routes.js b/x-pack/test/plugin_api_integration/plugins/task_manager/init_routes.js index 0892b64870c65..ad06fb15fd9ae 100644 --- a/x-pack/test/plugin_api_integration/plugins/task_manager/init_routes.js +++ b/x-pack/test/plugin_api_integration/plugins/task_manager/init_routes.js @@ -24,7 +24,7 @@ const taskManagerQuery = { }; export function initRoutes(server) { - const { taskManager } = server; + const taskManager = server.plugins.task_manager; server.route({ path: '/api/sample_tasks', diff --git a/x-pack/test/typings/hapi.d.ts b/x-pack/test/typings/hapi.d.ts index f84d97ed6fc07..f12e3c0c1c763 100644 --- a/x-pack/test/typings/hapi.d.ts +++ b/x-pack/test/typings/hapi.d.ts @@ -15,9 +15,6 @@ import { TaskManager } from '../../legacy/plugins/task_manager'; import { AlertingPlugin, AlertsClient } from '../../legacy/plugins/alerting'; declare module 'hapi' { - interface Server { - taskManager?: TaskManager; - } interface Request { getActionsClient?: () => ActionsClient; getAlertsClient?: () => AlertsClient; @@ -29,5 +26,6 @@ declare module 'hapi' { encrypted_saved_objects?: EncryptedSavedObjectsPlugin; actions?: ActionsPlugin; alerting?: AlertingPlugin; + task_manager?: TaskManager; } } diff --git a/x-pack/typings/hapi.d.ts b/x-pack/typings/hapi.d.ts index 41445ce035094..c5c02ef3765a7 100644 --- a/x-pack/typings/hapi.d.ts +++ b/x-pack/typings/hapi.d.ts @@ -15,9 +15,6 @@ import { TaskManager } from '../legacy/plugins/task_manager'; import { AlertingPlugin, AlertsClient } from '../legacy/plugins/alerting'; declare module 'hapi' { - interface Server { - taskManager?: TaskManager; - } interface Request { getActionsClient?: () => ActionsClient; getAlertsClient?: () => AlertsClient; @@ -29,5 +26,6 @@ declare module 'hapi' { encrypted_saved_objects?: EncryptedSavedObjectsPlugin; actions?: ActionsPlugin; alerting?: AlertingPlugin; + task_manager?: TaskManager; } } From aa2a58daaa5af290779d97c473d8d4087fc24479 Mon Sep 17 00:00:00 2001 From: Mike Cote Date: Thu, 8 Aug 2019 13:52:15 -0400 Subject: [PATCH 2/3] Cleanup --- x-pack/legacy/plugins/task_manager/index.d.ts | 8 -------- x-pack/legacy/plugins/task_manager/index.ts | 12 +++++------- 2 files changed, 5 insertions(+), 15 deletions(-) delete mode 100644 x-pack/legacy/plugins/task_manager/index.d.ts diff --git a/x-pack/legacy/plugins/task_manager/index.d.ts b/x-pack/legacy/plugins/task_manager/index.d.ts deleted file mode 100644 index ca106b6c2372a..0000000000000 --- a/x-pack/legacy/plugins/task_manager/index.d.ts +++ /dev/null @@ -1,8 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ - -export { TaskManager } from './types'; -export { TaskInstance, ConcreteTaskInstance, TaskRunCreatorFunction } from './task'; diff --git a/x-pack/legacy/plugins/task_manager/index.ts b/x-pack/legacy/plugins/task_manager/index.ts index 0701f77539cde..5bbb6f695a221 100644 --- a/x-pack/legacy/plugins/task_manager/index.ts +++ b/x-pack/legacy/plugins/task_manager/index.ts @@ -67,13 +67,11 @@ export function taskManager(kibana: any) { serializer, }); const exposedFunctions: TaskManager = { - fetch: taskManagerInstance.fetch.bind(taskManagerInstance), - remove: taskManagerInstance.remove.bind(taskManagerInstance), - schedule: taskManagerInstance.schedule.bind(taskManagerInstance), - addMiddleware: taskManagerInstance.addMiddleware.bind(taskManagerInstance), - registerTaskDefinitions: taskManagerInstance.registerTaskDefinitions.bind( - taskManagerInstance - ), + fetch: (...args) => taskManagerInstance.fetch(...args), + remove: (...args) => taskManagerInstance.remove(...args), + schedule: (...args) => taskManagerInstance.schedule(...args), + addMiddleware: (...args) => taskManagerInstance.addMiddleware(...args), + registerTaskDefinitions: (...args) => taskManagerInstance.registerTaskDefinitions(...args), }; server.expose(exposedFunctions); }, From 2a62206427660cfe2dd2ff23d12088d332cfd99e Mon Sep 17 00:00:00 2001 From: Mike Cote Date: Thu, 8 Aug 2019 13:56:16 -0400 Subject: [PATCH 3/3] Fix typo --- x-pack/legacy/plugins/task_manager/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x-pack/legacy/plugins/task_manager/index.ts b/x-pack/legacy/plugins/task_manager/index.ts index 5bbb6f695a221..42d3879d8c72e 100644 --- a/x-pack/legacy/plugins/task_manager/index.ts +++ b/x-pack/legacy/plugins/task_manager/index.ts @@ -7,7 +7,7 @@ import { Root } from 'joi'; import { Legacy } from 'kibana'; import { SavedObjectsSerializer, SavedObjectsSchema } from '../../../../src/core/server'; -import { TaskManager as TaskMaangerClass } from './task_manager'; +import { TaskManager as TaskManagerClass } from './task_manager'; import mappings from './mappings.json'; import { migrations } from './migrations'; import { TaskManager } from './types'; @@ -60,7 +60,7 @@ export function taskManager(kibana: any) { ['task'] ); - const taskManagerInstance = new TaskMaangerClass({ + const taskManagerInstance = new TaskManagerClass({ kbnServer: this.kbnServer, config, savedObjectsRepository,