Skip to content

Commit 2426b7f

Browse files
authored
[7.x] [Telemetry] Synchronous setup and start methods (elastic#79457) (elastic#79638)
1 parent e754fb7 commit 2426b7f

File tree

2 files changed

+44
-25
lines changed

2 files changed

+44
-25
lines changed

src/plugins/telemetry/server/fetcher.ts

+6-7
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
import moment from 'moment';
21-
import { Observable } from 'rxjs';
21+
import { Observable, Subscription, timer } from 'rxjs';
2222
import { take } from 'rxjs/operators';
2323
// @ts-ignore
2424
import fetch from 'node-fetch';
@@ -61,7 +61,7 @@ export class FetcherTask {
6161
private readonly config$: Observable<TelemetryConfigType>;
6262
private readonly currentKibanaVersion: string;
6363
private readonly logger: Logger;
64-
private intervalId?: NodeJS.Timeout;
64+
private intervalId?: Subscription;
6565
private lastReported?: number;
6666
private isSending = false;
6767
private internalRepository?: SavedObjectsClientContract;
@@ -82,15 +82,14 @@ export class FetcherTask {
8282
this.telemetryCollectionManager = telemetryCollectionManager;
8383
this.elasticsearchClient = elasticsearch.legacy.createClient('telemetry-fetcher');
8484

85-
setTimeout(() => {
86-
this.sendIfDue();
87-
this.intervalId = setInterval(() => this.sendIfDue(), this.checkIntervalMs);
88-
}, this.initialCheckDelayMs);
85+
this.intervalId = timer(this.initialCheckDelayMs, this.checkIntervalMs).subscribe(() =>
86+
this.sendIfDue()
87+
);
8988
}
9089

9190
public stop() {
9291
if (this.intervalId) {
93-
clearInterval(this.intervalId);
92+
this.intervalId.unsubscribe();
9493
}
9594
if (this.elasticsearchClient) {
9695
this.elasticsearchClient.close();

src/plugins/telemetry/server/plugin.ts

+38-18
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
import { URL } from 'url';
21-
import { Observable } from 'rxjs';
21+
import { AsyncSubject, Observable } from 'rxjs';
2222
import { UsageCollectionSetup } from 'src/plugins/usage_collection/server';
2323
import {
2424
TelemetryCollectionManagerPluginSetup,
@@ -30,11 +30,11 @@ import {
3030
PluginInitializerContext,
3131
ISavedObjectsRepository,
3232
CoreStart,
33-
IUiSettingsClient,
3433
SavedObjectsClient,
3534
Plugin,
3635
Logger,
3736
IClusterClient,
37+
UiSettingsServiceStart,
3838
} from '../../../core/server';
3939
import { registerRoutes } from './routes';
4040
import { registerCollection } from './telemetry_collection';
@@ -82,8 +82,11 @@ export class TelemetryPlugin implements Plugin<TelemetryPluginSetup, TelemetryPl
8282
private readonly config$: Observable<TelemetryConfigType>;
8383
private readonly isDev: boolean;
8484
private readonly fetcherTask: FetcherTask;
85+
/**
86+
* @private Used to mark the completion of the old UI Settings migration
87+
*/
88+
private readonly oldUiSettingsHandled$ = new AsyncSubject();
8589
private savedObjectsClient?: ISavedObjectsRepository;
86-
private uiSettingsClient?: IUiSettingsClient;
8790
private elasticsearchClient?: IClusterClient;
8891

8992
constructor(initializerContext: PluginInitializerContext<TelemetryConfigType>) {
@@ -97,10 +100,10 @@ export class TelemetryPlugin implements Plugin<TelemetryPluginSetup, TelemetryPl
97100
});
98101
}
99102

100-
public async setup(
103+
public setup(
101104
{ elasticsearch, http, savedObjects }: CoreSetup,
102105
{ usageCollection, telemetryCollectionManager }: TelemetryPluginsDepsSetup
103-
): Promise<TelemetryPluginSetup> {
106+
): TelemetryPluginSetup {
104107
const currentKibanaVersion = this.currentKibanaVersion;
105108
const config$ = this.config$;
106109
const isDev = this.isDev;
@@ -131,25 +134,21 @@ export class TelemetryPlugin implements Plugin<TelemetryPluginSetup, TelemetryPl
131134
};
132135
}
133136

134-
public async start(core: CoreStart, { telemetryCollectionManager }: TelemetryPluginsDepsStart) {
137+
public start(core: CoreStart, { telemetryCollectionManager }: TelemetryPluginsDepsStart) {
135138
const { savedObjects, uiSettings, elasticsearch } = core;
136-
this.savedObjectsClient = savedObjects.createInternalRepository();
137-
const savedObjectsClient = new SavedObjectsClient(this.savedObjectsClient);
138-
this.uiSettingsClient = uiSettings.asScopedToClient(savedObjectsClient);
139+
const savedObjectsInternalRepository = savedObjects.createInternalRepository();
140+
this.savedObjectsClient = savedObjectsInternalRepository;
139141
this.elasticsearchClient = elasticsearch.client;
140142

141-
try {
142-
await handleOldSettings(savedObjectsClient, this.uiSettingsClient);
143-
} catch (error) {
144-
this.logger.warn('Unable to update legacy telemetry configs.');
145-
}
146-
147-
this.fetcherTask.start(core, { telemetryCollectionManager });
143+
// Not catching nor awaiting these promises because they should never reject
144+
this.handleOldUiSettings(uiSettings);
145+
this.startFetcherWhenOldSettingsAreHandled(core, telemetryCollectionManager);
148146

149147
return {
150148
getIsOptedIn: async () => {
151-
const internalRepository = new SavedObjectsClient(savedObjects.createInternalRepository());
152-
const telemetrySavedObject = await getTelemetrySavedObject(internalRepository!);
149+
await this.oldUiSettingsHandled$.pipe(take(1)).toPromise(); // Wait for the old settings to be handled
150+
const internalRepository = new SavedObjectsClient(savedObjectsInternalRepository);
151+
const telemetrySavedObject = await getTelemetrySavedObject(internalRepository);
153152
const config = await this.config$.pipe(take(1)).toPromise();
154153
const allowChangingOptInStatus = config.allowChangingOptInStatus;
155154
const configTelemetryOptIn = typeof config.optIn === 'undefined' ? null : config.optIn;
@@ -166,6 +165,27 @@ export class TelemetryPlugin implements Plugin<TelemetryPluginSetup, TelemetryPl
166165
};
167166
}
168167

168+
private async handleOldUiSettings(uiSettings: UiSettingsServiceStart) {
169+
const savedObjectsClient = new SavedObjectsClient(this.savedObjectsClient!);
170+
const uiSettingsClient = uiSettings.asScopedToClient(savedObjectsClient);
171+
172+
try {
173+
await handleOldSettings(savedObjectsClient, uiSettingsClient);
174+
} catch (error) {
175+
this.logger.warn('Unable to update legacy telemetry configs.');
176+
}
177+
// Set the mark in the AsyncSubject as complete so all the methods that require this method to be completed before working, can move on
178+
this.oldUiSettingsHandled$.complete();
179+
}
180+
181+
private async startFetcherWhenOldSettingsAreHandled(
182+
core: CoreStart,
183+
telemetryCollectionManager: TelemetryCollectionManagerPluginStart
184+
) {
185+
await this.oldUiSettingsHandled$.pipe(take(1)).toPromise(); // Wait for the old settings to be handled
186+
this.fetcherTask.start(core, { telemetryCollectionManager });
187+
}
188+
169189
private registerMappings(registerType: SavedObjectsRegisterType) {
170190
registerType({
171191
name: 'telemetry',

0 commit comments

Comments
 (0)