Bug 842370 - Use Tasks for provider collection; r=rnewman
authorGregory Szorc <gps@mozilla.com>
Mon, 18 Feb 2013 16:11:43 -0800
changeset 132163 5060a8ce0be450649a5ef4a8163c3eab701bcdc4
parent 132162 412fa61ea374c3b7a5619874178f8784aaba40b6
child 132164 19d084d39eab11b42a8dfd2e73c02a47e0683e23
push id2323
push userbbajaj@mozilla.com
push dateMon, 01 Apr 2013 19:47:02 +0000
treeherdermozilla-beta@7712be144d91 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersrnewman
bugs842370
milestone21.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 842370 - Use Tasks for provider collection; r=rnewman
services/metrics/collector.jsm
--- a/services/metrics/collector.jsm
+++ b/services/metrics/collector.jsm
@@ -108,52 +108,39 @@ Collector.prototype = Object.freeze({
     if (!this._providerInitQueue.length || this._providerInitializing) {
       return;
     }
 
     let [provider, deferred] = this._providerInitQueue.shift();
     this._providerInitializing = true;
 
     this._log.info("Initializing provider with storage: " + provider.name);
-    let initPromise;
-    try {
-      initPromise = provider.init(this._storage);
-    } catch (ex) {
-      this._log.warn("Provider failed to initialize: " +
-                     CommonUtils.exceptionStr(ex));
-      this._providerInitializing = false;
-      deferred.reject(ex);
-      this._popAndInitProvider();
-      return;
-    }
 
-    initPromise.then(
-      function onSuccess(result) {
-        this._log.info("Provider finished initialization: " + provider.name);
-        this._providerInitializing = false;
+    Task.spawn(function initProvider() {
+      try {
+        let result = yield provider.init(this._storage);
+        this._log.info("Provider successfully initialized: " + provider.name);
 
         this._providers.set(provider.name, {
           provider: provider,
           constantsCollected: false,
         });
 
         this.providerErrors.set(provider.name, []);
 
         deferred.resolve(result);
-        this._popAndInitProvider();
-      }.bind(this),
-      function onError(error) {
-        this._log.warn("Provider initialization failed: " +
-                       CommonUtils.exceptionStr(error));
+      } catch (ex) {
+        this._log.warn("Provider failed to initialize: " + provider.name +
+                       ": " + CommonUtils.exceptionStr(ex));
+        deferred.reject(ex);
+      } finally {
         this._providerInitializing = false;
-        deferred.reject(error);
         this._popAndInitProvider();
-      }.bind(this)
-    );
-
+      }
+    }.bind(this));
   },
 
   /**
    * Collects all constant measurements from all providers.
    *
    * Returns a Promise that will be fulfilled once all data providers have
    * provided their constant data. A side-effect of this promise fulfillment
    * is that the collector is populated with the obtained collection results.
@@ -233,36 +220,30 @@ Collector.prototype = Object.freeze({
    *
    * This consumes the data resolved by the promises and returns a new promise
    * that will be resolved once all promises have been resolved.
    *
    * The promise is resolved even if one of the underlying collection
    * promises is rejected.
    */
   _handleCollectionPromises: function (promises) {
-    if (!promises.length) {
-      return Promise.resolve(this);
-    }
-
-    let deferred = Promise.defer();
-    let finishedCount = 0;
-
-    let onComplete = function () {
-      finishedCount++;
-      if (finishedCount >= promises.length) {
-        deferred.resolve(this);
+    return Task.spawn(function waitForPromises() {
+      for (let [name, promise] of promises) {
+        try {
+          yield promise;
+          this._log.debug("Provider collected successfully: " + name);
+        } catch (ex) {
+          this._log.warn("Provider failed to collect: " + name + ": " +
+                         CommonUtils.exceptionStr(ex));
+          try {
+            this.providerErrors.get(name).push(ex);
+          } catch (ex2) {
+            this._log.error("Error updating provider errors. This should " +
+                            "never happen: " + CommonUtils.exceptionStr(ex2));
+          }
+        }
       }
-    }.bind(this);
 
-    for (let [name, promise] of promises) {
-      let onError = function (error) {
-        this._log.warn("Collection promise was rejected: " +
-                       CommonUtils.exceptionStr(error));
-        this.providerErrors.get(name).push(error);
-        onComplete();
-      }.bind(this);
-      promise.then(onComplete, onError);
-    }
-
-    return deferred.promise;
+      throw new Task.Result(this);
+    }.bind(this));
   },
 });