Skip to content

Commit

Permalink
Telemetry plugin graceful setup() + stop() (#134236)
Browse files Browse the repository at this point in the history
* Telemetry plugin graceful setup() + stop()

* Propagate abort signal to ES call

* Import type instead

* Document non-breaking updates to one of the core APIs

* Await for current getOptInStatus call

* Undo md changes

* Undo md changes
  • Loading branch information
gsoldevila authored Jun 17, 2022
1 parent ea6b79d commit afd25eb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
11 changes: 8 additions & 3 deletions src/plugins/telemetry/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
distinctUntilChanged,
filter,
} from 'rxjs';

import { ElasticV3ServerShipper } from '@kbn/analytics-shippers-elastic-v3-server';

import type { UsageCollectionSetup } from '@kbn/usage-collection-plugin/server';
Expand Down Expand Up @@ -90,6 +89,7 @@ export class TelemetryPlugin implements Plugin<TelemetryPluginSetup, TelemetryPl
private readonly isOptedIn$ = new BehaviorSubject<boolean | undefined>(undefined);
private readonly isDev: boolean;
private readonly fetcherTask: FetcherTask;
private optInPromise?: Promise<boolean | undefined>;
/**
* @private Used to mark the completion of the old UI Settings migration
*/
Expand All @@ -111,7 +111,10 @@ export class TelemetryPlugin implements Plugin<TelemetryPluginSetup, TelemetryPl
*/
private readonly optInPollerSubscription = timer(0, OPT_IN_POLL_INTERVAL_MS)
.pipe(
exhaustMap(() => this.getOptInStatus()),
exhaustMap(() => {
this.optInPromise = this.getOptInStatus();
return this.optInPromise;
}),
distinctUntilChanged()
)
.subscribe((isOptedIn) => this.isOptedIn$.next(isOptedIn));
Expand Down Expand Up @@ -207,9 +210,11 @@ export class TelemetryPlugin implements Plugin<TelemetryPluginSetup, TelemetryPl
};
}

public stop() {
public async stop() {
this.optInPollerSubscription.unsubscribe();
this.isOptedIn$.complete();
this.fetcherTask.stop();
if (this.optInPromise) await this.optInPromise;
}

private async getOptInStatus(): Promise<boolean | undefined> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
* Side Public License, v 1.
*/

import { SavedObjectsErrorHelpers, SavedObjectsClientContract } from '@kbn/core/server';
import { TelemetrySavedObject } from '.';
import { SavedObjectsErrorHelpers, type SavedObjectsClientContract } from '@kbn/core/server';
import type { TelemetrySavedObject } from '.';

type GetTelemetrySavedObject = (
repository: SavedObjectsClientContract
Expand Down

0 comments on commit afd25eb

Please sign in to comment.