@@ -14,6 +14,7 @@ import EventsDatabase from "metrics/events_database";
1414import UUIDMetricType from "metrics/types/uuid" ;
1515import DatetimeMetricType , { DatetimeMetric } from "metrics/types/datetime" ;
1616import Dispatcher from "dispatcher" ;
17+ import CorePings from "internal_pings" ;
1718
1819class Glean {
1920 // The Glean singleton.
@@ -29,6 +30,8 @@ class Glean {
2930 private _initialized : boolean ;
3031 // Instances of Glean's core metrics.
3132 private _coreMetrics : CoreMetrics ;
33+ // Instances of Glean's core pings.
34+ private _corePings : CorePings ;
3235 // The ping uploader.
3336 private _pingUploader : PingUploader
3437 // A task dispatcher to help execute in order asynchronous external API calls.
@@ -53,6 +56,7 @@ class Glean {
5356 this . _dispatcher = new Dispatcher ( ) ;
5457 this . _pingUploader = new PingUploader ( ) ;
5558 this . _coreMetrics = new CoreMetrics ( ) ;
59+ this . _corePings = new CorePings ( ) ;
5660 this . _initialized = false ;
5761 this . _db = {
5862 metrics : new MetricsDatabase ( ) ,
@@ -77,6 +81,10 @@ class Glean {
7781 return Glean . instance . _coreMetrics ;
7882 }
7983
84+ private static get corePings ( ) : CorePings {
85+ return Glean . instance . _corePings ;
86+ }
87+
8088 private static get uploadEnabled ( ) : boolean {
8189 return Glean . instance . _uploadEnabled || false ;
8290 }
@@ -109,6 +117,9 @@ class Glean {
109117 private static async onUploadDisabled ( ) : Promise < void > {
110118 Glean . uploadEnabled = false ;
111119 await Glean . clearMetrics ( ) ;
120+ // Note that `submit` is a dispatched function.
121+ // The actual submission will only happen after we leave `onUploadDisabled`.
122+ Glean . corePings . deletionRequest . submit ( ) ;
112123 }
113124
114125 /**
@@ -126,7 +137,7 @@ class Glean {
126137 //
127138 // Note: This will throw in case the stored metric is incorrect or inexistent.
128139 // The most likely is that it throws if the metrics hasn't been set,
129- // e.g. we start Glean for the first with tupload disabled.
140+ // e.g. we start Glean for the first with upload disabled.
130141 let firstRunDate : Date ;
131142 try {
132143 firstRunDate = new DatetimeMetric (
@@ -220,6 +231,13 @@ class Glean {
220231 // IMPORTANT!
221232 // Any pings we want to send upon initialization should happen before this.
222233 await Glean . metricsDatabase . clear ( Lifetime . Application ) ;
234+
235+ // We need to mark Glean as initialized before dealing with the upload status,
236+ // otherwise we will not be able to submit deletion-request pings if necessary.
237+ //
238+ // This is fine, we are inside a dispatched task that is guaranteed to run before any
239+ // other task. No external API call will be executed before we leave this task.
240+ Glean . instance . _initialized = true ;
223241
224242 // The upload enabled flag may have changed since the last run, for
225243 // example by the changing of a config file.
@@ -242,96 +260,52 @@ class Glean {
242260
243261 if ( clientId ) {
244262 if ( clientId !== KNOWN_CLIENT_ID ) {
245- // Temporarily enable uploading so we can submit a
246- // deletion request ping.
247- Glean . uploadEnabled = true ;
248263 await Glean . onUploadDisabled ( ) ;
249264 }
250265 } else {
266+ // Call `clearMetrics` directly here instead of `onUploadDisabled` to avoid sending
267+ // a deletion-request ping for a user that has already done that.
251268 await Glean . clearMetrics ( ) ;
252269 }
253270 }
254271
255- Glean . instance . _initialized = true ;
256-
257272 await Glean . pingUploader . scanPendingPings ( ) ;
258273
259274 // Even though this returns a promise, there is no need to block on it returning.
260275 //
261- // On the contrary we _want_ the dispatcher to execute tasks async.
276+ // On the contrary we _want_ the uploading tasks to be executed async.
262277 void Glean . pingUploader . triggerUpload ( ) ;
263278 } ) ;
264279 }
265280
266- /**
267- * Gets this Glean's instance metrics database.
268- *
269- * @returns This Glean's instance metrics database.
270- */
271281 static get metricsDatabase ( ) : MetricsDatabase {
272282 return Glean . instance . _db . metrics ;
273283 }
274284
275- /**
276- * Gets this Glean's instance events database.
277- *
278- * @returns This Glean's instance events database.
279- */
280285 static get eventsDatabase ( ) : EventsDatabase {
281286 return Glean . instance . _db . events ;
282287 }
283288
284-
285- /**
286- * Gets this Glean's instance pings database.
287- *
288- * @returns This Glean's instance pings database.
289- */
290289 static get pingsDatabase ( ) : PingsDatabase {
291290 return Glean . instance . _db . pings ;
292291 }
293292
294- /**
295- * Gets this Glean's instance initialization status.
296- *
297- * @returns Whether or not the Glean singleton has been initialized.
298- */
299293 static get initialized ( ) : boolean {
300294 return Glean . instance . _initialized ;
301295 }
302296
303- /**
304- * Gets this Glean's instance application id.
305- *
306- * @returns The application id or `undefined` in case Glean has not been initialized yet.
307- */
308297 static get applicationId ( ) : string | undefined {
309298 return Glean . instance . _applicationId ;
310299 }
311300
312- /**
313- * Gets this Glean's instance server endpoint.
314- *
315- * @returns The server endpoint or `undefined` in case Glean has not been initialized yet.
316- */
317301 static get serverEndpoint ( ) : string | undefined {
318302 return Glean . instance . _config ?. serverEndpoint ;
319303 }
320304
321- /**
322- * Whether or not to log pings upon collection.
323- *
324- * @returns Whether or not to log pings upon collection.
325- */
326305 static get logPings ( ) : boolean {
327306 return Glean . instance . _config ?. debug ?. logPings || false ;
328307 }
329308
330- /**
331- * Gets this Gleans's instance dispatcher.
332- *
333- * @returns The dispatcher instance.
334- */
335309 static get dispatcher ( ) : Dispatcher {
336310 return Glean . instance . _dispatcher ;
337311 }
0 commit comments