18
18
*/
19
19
20
20
import { URL } from 'url' ;
21
- import { Observable } from 'rxjs' ;
21
+ import { AsyncSubject , Observable } from 'rxjs' ;
22
22
import { UsageCollectionSetup } from 'src/plugins/usage_collection/server' ;
23
23
import {
24
24
TelemetryCollectionManagerPluginSetup ,
@@ -30,11 +30,11 @@ import {
30
30
PluginInitializerContext ,
31
31
ISavedObjectsRepository ,
32
32
CoreStart ,
33
- IUiSettingsClient ,
34
33
SavedObjectsClient ,
35
34
Plugin ,
36
35
Logger ,
37
36
IClusterClient ,
37
+ UiSettingsServiceStart ,
38
38
} from '../../../core/server' ;
39
39
import { registerRoutes } from './routes' ;
40
40
import { registerCollection } from './telemetry_collection' ;
@@ -82,8 +82,11 @@ export class TelemetryPlugin implements Plugin<TelemetryPluginSetup, TelemetryPl
82
82
private readonly config$ : Observable < TelemetryConfigType > ;
83
83
private readonly isDev : boolean ;
84
84
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 ( ) ;
85
89
private savedObjectsClient ?: ISavedObjectsRepository ;
86
- private uiSettingsClient ?: IUiSettingsClient ;
87
90
private elasticsearchClient ?: IClusterClient ;
88
91
89
92
constructor ( initializerContext : PluginInitializerContext < TelemetryConfigType > ) {
@@ -97,10 +100,10 @@ export class TelemetryPlugin implements Plugin<TelemetryPluginSetup, TelemetryPl
97
100
} ) ;
98
101
}
99
102
100
- public async setup (
103
+ public setup (
101
104
{ elasticsearch, http, savedObjects } : CoreSetup ,
102
105
{ usageCollection, telemetryCollectionManager } : TelemetryPluginsDepsSetup
103
- ) : Promise < TelemetryPluginSetup > {
106
+ ) : TelemetryPluginSetup {
104
107
const currentKibanaVersion = this . currentKibanaVersion ;
105
108
const config$ = this . config$ ;
106
109
const isDev = this . isDev ;
@@ -131,25 +134,21 @@ export class TelemetryPlugin implements Plugin<TelemetryPluginSetup, TelemetryPl
131
134
} ;
132
135
}
133
136
134
- public async start ( core : CoreStart , { telemetryCollectionManager } : TelemetryPluginsDepsStart ) {
137
+ public start ( core : CoreStart , { telemetryCollectionManager } : TelemetryPluginsDepsStart ) {
135
138
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 ;
139
141
this . elasticsearchClient = elasticsearch . client ;
140
142
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 ) ;
148
146
149
147
return {
150
148
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 ) ;
153
152
const config = await this . config$ . pipe ( take ( 1 ) ) . toPromise ( ) ;
154
153
const allowChangingOptInStatus = config . allowChangingOptInStatus ;
155
154
const configTelemetryOptIn = typeof config . optIn === 'undefined' ? null : config . optIn ;
@@ -166,6 +165,27 @@ export class TelemetryPlugin implements Plugin<TelemetryPluginSetup, TelemetryPl
166
165
} ;
167
166
}
168
167
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
+
169
189
private registerMappings ( registerType : SavedObjectsRegisterType ) {
170
190
registerType ( {
171
191
name : 'telemetry' ,
0 commit comments