From ddf991e7dc59eb3a2cfe0a9ae18627840f9df31a Mon Sep 17 00:00:00 2001 From: Michael Dokolin Date: Thu, 11 Nov 2021 07:24:57 +0100 Subject: [PATCH] [Reporting] Fix task manager not to run tasks before Kibana is available (#118206) --- x-pack/plugins/reporting/server/core.ts | 18 +++++++++++++++++- x-pack/plugins/reporting/server/plugin.ts | 2 +- .../create_mock_reportingplugin.ts | 3 ++- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/x-pack/plugins/reporting/server/core.ts b/x-pack/plugins/reporting/server/core.ts index e89ba6af3e28f5a..bc74f5463ba33fb 100644 --- a/x-pack/plugins/reporting/server/core.ts +++ b/x-pack/plugins/reporting/server/core.ts @@ -7,7 +7,7 @@ import Hapi from '@hapi/hapi'; import * as Rx from 'rxjs'; -import { first, map, take } from 'rxjs/operators'; +import { filter, first, map, take } from 'rxjs/operators'; import { ScreenshotModePluginSetup } from 'src/plugins/screenshot_mode/server'; import { BasePath, @@ -17,6 +17,8 @@ import { PluginInitializerContext, SavedObjectsClientContract, SavedObjectsServiceStart, + ServiceStatusLevels, + StatusServiceSetup, UiSettingsServiceStart, } from '../../../../src/core/server'; import { PluginStart as DataPluginStart } from '../../../../src/plugins/data/server'; @@ -44,6 +46,7 @@ export interface ReportingInternalSetup { taskManager: TaskManagerSetupContract; screenshotMode: ScreenshotModePluginSetup; logger: LevelLogger; + status: StatusServiceSetup; } export interface ReportingInternalStart { @@ -111,12 +114,25 @@ export class ReportingCore { this.pluginStart$.next(startDeps); // trigger the observer this.pluginStartDeps = startDeps; // cache + await this.assertKibanaIsAvailable(); + const { taskManager } = startDeps; const { executeTask, monitorTask } = this; // enable this instance to generate reports and to monitor for pending reports await Promise.all([executeTask.init(taskManager), monitorTask.init(taskManager)]); } + private async assertKibanaIsAvailable(): Promise { + const { status } = this.getPluginSetupDeps(); + + await status.overall$ + .pipe( + filter((current) => current.level === ServiceStatusLevels.available), + first() + ) + .toPromise(); + } + /* * Blocks the caller until setup is done */ diff --git a/x-pack/plugins/reporting/server/plugin.ts b/x-pack/plugins/reporting/server/plugin.ts index 07d61ff1630fc53..8969a698a8ce4e0 100644 --- a/x-pack/plugins/reporting/server/plugin.ts +++ b/x-pack/plugins/reporting/server/plugin.ts @@ -52,7 +52,6 @@ export class ReportingPlugin const router = http.createRouter(); const basePath = http.basePath; - reportingCore.pluginSetup({ screenshotMode, features, @@ -63,6 +62,7 @@ export class ReportingPlugin spaces, taskManager, logger: this.logger, + status: core.status, }); registerUiSettings(core); diff --git a/x-pack/plugins/reporting/server/test_helpers/create_mock_reportingplugin.ts b/x-pack/plugins/reporting/server/test_helpers/create_mock_reportingplugin.ts index d62cc750ccfcc2c..c05b2c54aeabfb7 100644 --- a/x-pack/plugins/reporting/server/test_helpers/create_mock_reportingplugin.ts +++ b/x-pack/plugins/reporting/server/test_helpers/create_mock_reportingplugin.ts @@ -11,7 +11,7 @@ jest.mock('../browsers'); import _ from 'lodash'; import * as Rx from 'rxjs'; -import { coreMock, elasticsearchServiceMock } from 'src/core/server/mocks'; +import { coreMock, elasticsearchServiceMock, statusServiceMock } from 'src/core/server/mocks'; // eslint-disable-next-line @kbn/eslint/no-restricted-paths import { dataPluginMock } from 'src/plugins/data/server/mocks'; import { FieldFormatsRegistry } from 'src/plugins/field_formats/common'; @@ -45,6 +45,7 @@ export const createMockPluginSetup = (setupMock?: any): ReportingInternalSetup = licensing: { license$: Rx.of({ isAvailable: true, isActive: true, type: 'basic' }) } as any, taskManager: taskManagerMock.createSetup(), logger: createMockLevelLogger(), + status: statusServiceMock.createSetupContract(), ...setupMock, }; };