@@ -99,12 +99,13 @@ class Glean {
9999 * Afterward, the upload_enabled flag is set to false.
100100 */
101101 private static async onUploadDisabled ( ) : Promise < void > {
102+ // It's fine to set this before submitting the deletion request ping,
103+ // that ping is still sent even if upload is disabled.
104+ Context . uploadEnabled = false ;
102105 // We need to use an undispatched submission to guarantee that the
103106 // ping is collected before metric are cleared, otherwise we end up
104107 // with malformed pings.
105108 await PingType . _private_submitUndispatched ( Glean . corePings . deletionRequest ) ;
106-
107- Context . uploadEnabled = false ;
108109 await Glean . clearMetrics ( ) ;
109110 }
110111
@@ -210,15 +211,19 @@ class Glean {
210211 return ;
211212 }
212213
214+ Context . applicationId = sanitizeApplicationId ( applicationId ) ;
215+
213216 // The configuration constructor will throw in case config has any incorrect prop.
214217 const correctConfig = new Configuration ( config ) ;
218+ Context . debugOptions = correctConfig . debug ;
219+ Glean . instance . _config = correctConfig ;
215220
216221 Context . metricsDatabase = new MetricsDatabase ( Glean . platform . Storage ) ;
217222 Context . eventsDatabase = new EventsDatabase ( Glean . platform . Storage ) ;
218223 Context . pingsDatabase = new PingsDatabase ( Glean . platform . Storage ) ;
219224 Context . errorManager = new ErrorManager ( ) ;
220225
221- Glean . instance . _pingUploader = new PingUploader ( correctConfig , Glean . platform ) ;
226+ Glean . instance . _pingUploader = new PingUploader ( correctConfig , Glean . platform , Context . pingsDatabase ) ;
222227
223228 Context . pingsDatabase . attachObserver ( Glean . pingUploader ) ;
224229
@@ -235,26 +240,19 @@ class Glean {
235240 //
236241 // The dispatcher will catch and log any exceptions.
237242 Context . dispatcher . flushInit ( async ( ) => {
238- Context . applicationId = sanitizeApplicationId ( applicationId ) ;
239- Context . debugOptions = correctConfig . debug ;
240- Glean . instance . _config = correctConfig ;
241-
242-
243- // IMPORTANT!
244- // Any pings we want to send upon initialization should happen before these two lines.
245- //
246- // Clear application lifetime metrics.
247- await Context . metricsDatabase . clear ( Lifetime . Application ) ;
248- // Initialize the events database.
249- await Context . eventsDatabase . initialize ( ) ;
250-
251243 // We need to mark Glean as initialized before dealing with the upload status,
252244 // otherwise we will not be able to submit deletion-request pings if necessary.
253245 //
254246 // This is fine, we are inside a dispatched task that is guaranteed to run before any
255247 // other task. No external API call will be executed before we leave this task.
256248 Context . initialized = true ;
257249
250+ // IMPORTANT!
251+ // Any pings we want to send upon initialization should happen before this line.
252+ //
253+ // Clear application lifetime metrics.
254+ await Context . metricsDatabase . clear ( Lifetime . Application ) ;
255+
258256 // The upload enabled flag may have changed since the last run, for
259257 // example by the changing of a config file.
260258 if ( uploadEnabled ) {
@@ -285,9 +283,16 @@ class Glean {
285283 }
286284 }
287285
286+ // Initialize the events database.
287+ //
288+ // It's important this happens _after_ the upload state is dealt with,
289+ // because initializing the events database may record the execution_counter and
290+ // glean.restarted metrics. If the upload state is not defined these metrics can't be recorded.
291+ await Context . eventsDatabase . initialize ( ) ;
292+
288293 // We only scan the pendings pings **after** dealing with the upload state.
289- // If upload is disabled, we delete all pending pings files
290- // and we need to do that **before** scanning the pending pings
294+ // If upload is disabled, pending pings files are deleted
295+ // so we need to know that state **before** scanning the pending pings
291296 // to ensure we don't enqueue pings before their files are deleted.
292297 await Context . pingsDatabase . scanPendingPings ( ) ;
293298 } ) ;
@@ -505,22 +510,29 @@ class Glean {
505510 /**
506511 * Test-only API**
507512 *
508- * Resets the Glean to an uninitialized state.
513+ * Resets Glean to an uninitialized state.
514+ * This is a no-op in case Glean has not been initialized.
509515 *
510516 * TODO: Only allow this function to be called on test mode (depends on Bug 1682771).
517+ *
518+ * @param clearStores Whether or not to clear the events, metrics and pings databases on uninitialize.
511519 */
512- static async testUninitialize ( ) : Promise < void > {
513- // Get back to an uninitialized state.
514- await Context . testUninitialize ( ) ;
520+ static async testUninitialize ( clearStores = true ) : Promise < void > {
521+ if ( Context . initialized ) {
522+ await Glean . shutdown ( ) ;
515523
516- // Shutdown the current uploader.
517- //
518- // This is fine because a new uploader is created on initialize.
519- // It will also guarantee all pings to be sent before uninitializing.
520- await Glean . pingUploader ?. shutdown ( ) ;
524+ if ( clearStores ) {
525+ await Context . eventsDatabase . clearAll ( ) ;
526+ await Context . metricsDatabase . clearAll ( ) ;
527+ await Context . pingsDatabase . clearAll ( ) ;
528+ }
529+
530+ // Get back to an uninitialized state.
531+ Context . testUninitialize ( ) ;
521532
522- // Deregister all plugins
523- testResetEvents ( ) ;
533+ // Deregister all plugins
534+ testResetEvents ( ) ;
535+ }
524536 }
525537
526538 /**
@@ -535,25 +547,15 @@ class Glean {
535547 * If disabled, all persisted metrics, events and queued pings (except
536548 * first_run_date) are cleared. Default to `true`.
537549 * @param config Glean configuration options.
550+ * @param clearStores Whether or not to clear the events, metrics and pings databases on reset.
538551 */
539552 static async testResetGlean (
540553 applicationId : string ,
541554 uploadEnabled = true ,
542- config ?: ConfigurationInterface
555+ config ?: ConfigurationInterface ,
556+ clearStores = true ,
543557 ) : Promise < void > {
544- await Glean . testUninitialize ( ) ;
545-
546- // Clear the databases.
547- try {
548- await Context . eventsDatabase . clearAll ( ) ;
549- await Context . metricsDatabase . clearAll ( ) ;
550- await Context . pingsDatabase . clearAll ( ) ;
551- } catch {
552- // Nothing to do here.
553- // It is expected that these will fail in case we are initializing Glean for the first time.
554- }
555-
556- // Re-Initialize Glean.
558+ await Glean . testUninitialize ( clearStores ) ;
557559 await Glean . testInitialize ( applicationId , uploadEnabled , config ) ;
558560 }
559561}
0 commit comments