diff --git a/x-pack/legacy/plugins/oss_telemetry/index.ts b/x-pack/legacy/plugins/oss_telemetry/index.ts index 17573935408d8..26b122a1de4c2 100644 --- a/x-pack/legacy/plugins/oss_telemetry/index.ts +++ b/x-pack/legacy/plugins/oss_telemetry/index.ts @@ -5,6 +5,8 @@ */ import { Logger, PluginInitializerContext } from 'kibana/server'; +import { Legacy } from 'kibana'; +import { isFunction } from 'lodash'; import { PLUGIN_ID } from './constants'; import { OssTelemetryPlugin } from './server/plugin'; import { LegacyPluginInitializer } from '../../../../src/legacy/plugin_discovery/types'; @@ -16,7 +18,7 @@ export const ossTelemetry: LegacyPluginInitializer = kibana => { require: ['elasticsearch', 'xpack_main'], configPrefix: 'xpack.oss_telemetry', - init(server) { + init(server: Legacy.Server) { const plugin = new OssTelemetryPlugin({ logger: { get: () => @@ -30,10 +32,7 @@ export const ossTelemetry: LegacyPluginInitializer = kibana => { } as PluginInitializerContext); plugin.setup(server.newPlatform.setup.core, { usageCollection: server.newPlatform.setup.plugins.usageCollection, - taskManager: { - ...server.newPlatform.setup.plugins.kibanaTaskManager, - ...server.newPlatform.start.plugins.kibanaTaskManager, - } as TaskManager, + taskManager: getTaskManager(server), __LEGACY: { config: server.config(), xpackMainStatus: ((server.plugins.xpack_main as unknown) as { status: any }).status @@ -43,3 +42,11 @@ export const ossTelemetry: LegacyPluginInitializer = kibana => { }, }); }; + +function getTaskManager(server: Legacy.Server) { + const taskManager = { + ...(server?.newPlatform?.setup?.plugins?.kibanaTaskManager || {}), + ...(server?.newPlatform?.start?.plugins?.kibanaTaskManager || {}), + } as TaskManager; + return isFunction(taskManager.ensureScheduled) ? taskManager : undefined; +} diff --git a/x-pack/plugins/kibana_task_manager/server/plugin.ts b/x-pack/plugins/kibana_task_manager/server/plugin.ts index 620ab8003a0d7..06151fa4cde51 100644 --- a/x-pack/plugins/kibana_task_manager/server/plugin.ts +++ b/x-pack/plugins/kibana_task_manager/server/plugin.ts @@ -41,11 +41,7 @@ export type TaskManagerPluginStartContract = Omit< export class TaskManagerPlugin implements Plugin { legacySetup$: AsyncSubject = new AsyncSubject(); - legacySetupResult: Promise = new Promise(resolve => { - this.legacySetup$.subscribe(__LEGACY => { - resolve(__LEGACY); - }); - }); + legacySetupResult: Promise = resolveOnFirstValue(this.legacySetup$); currentConfig: TaskManagerConfig; constructor(private readonly initContext: PluginInitializerContext) { @@ -84,13 +80,15 @@ export class TaskManagerPlugin } public start(): TaskManagerPluginStartContract { - const taskManager = this.legacySetupResult.then(({ legacyPlugin }) => legacyPlugin.start()); + const legacyTaskManager = this.legacySetupResult.then(({ legacyPlugin }) => + legacyPlugin.start() + ); return { - fetch: (...args) => taskManager.then(tm => tm.fetch(...args)), - remove: (...args) => taskManager.then(tm => tm.remove(...args)), - schedule: (...args) => taskManager.then(tm => tm.schedule(...args)), - runNow: (...args) => taskManager.then(tm => tm.runNow(...args)), - ensureScheduled: (...args) => taskManager.then(tm => tm.ensureScheduled(...args)), + fetch: (...args) => legacyTaskManager.then(tm => tm.fetch(...args)), + remove: (...args) => legacyTaskManager.then(tm => tm.remove(...args)), + schedule: (...args) => legacyTaskManager.then(tm => tm.schedule(...args)), + runNow: (...args) => legacyTaskManager.then(tm => tm.runNow(...args)), + ensureScheduled: (...args) => legacyTaskManager.then(tm => tm.ensureScheduled(...args)), }; } public stop() { @@ -99,3 +97,11 @@ export class TaskManagerPlugin }); } } + +function resolveOnFirstValue(stream$: AsyncSubject): Promise { + return new Promise(resolve => { + stream$.subscribe(value => { + resolve(value); + }); + }); +}