2012-11-05 13:45:35 -08:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2013-01-27 11:26:48 -08:00
|
|
|
#ifndef MERGED_COMPARTMENT
|
2013-03-07 05:06:46 -08:00
|
|
|
this.EXPORTED_SYMBOLS = ["ProviderManager"];
|
2012-11-05 13:45:35 -08:00
|
|
|
|
2013-03-11 14:12:24 -07:00
|
|
|
const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
|
2012-11-05 13:45:35 -08:00
|
|
|
|
2013-01-27 11:26:48 -08:00
|
|
|
Cu.import("resource://gre/modules/services/metrics/dataprovider.jsm");
|
|
|
|
#endif
|
|
|
|
|
2013-02-01 11:43:15 -08:00
|
|
|
Cu.import("resource://gre/modules/commonjs/sdk/core/promise.js");
|
2013-01-06 12:13:19 -08:00
|
|
|
Cu.import("resource://gre/modules/Task.jsm");
|
2012-11-05 13:45:35 -08:00
|
|
|
Cu.import("resource://services-common/log4moz.js");
|
2012-11-08 15:32:49 -08:00
|
|
|
Cu.import("resource://services-common/utils.js");
|
2012-11-05 13:45:35 -08:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles and coordinates the collection of metrics data from providers.
|
|
|
|
*
|
2013-01-06 12:13:19 -08:00
|
|
|
* This provides an interface for managing `Metrics.Provider` instances. It
|
2012-11-05 13:45:35 -08:00
|
|
|
* provides APIs for bulk collection of data.
|
|
|
|
*/
|
2013-03-07 05:06:46 -08:00
|
|
|
this.ProviderManager = function (storage) {
|
|
|
|
this._log = Log4Moz.repository.getLogger("Services.Metrics.ProviderManager");
|
2013-01-06 12:13:19 -08:00
|
|
|
|
|
|
|
this._providers = new Map();
|
|
|
|
this._storage = storage;
|
2012-11-05 13:45:35 -08:00
|
|
|
|
2013-01-06 22:46:30 -08:00
|
|
|
this._providerInitQueue = [];
|
|
|
|
this._providerInitializing = false;
|
2013-03-11 14:12:24 -07:00
|
|
|
|
|
|
|
this._pullOnlyProviders = {};
|
2013-04-16 11:46:05 -07:00
|
|
|
this._pullOnlyProvidersRegisterCount = 0;
|
|
|
|
this._pullOnlyProvidersState = this.PULL_ONLY_NOT_REGISTERED;
|
|
|
|
this._pullOnlyProvidersCurrentPromise = null;
|
2013-03-11 14:12:24 -07:00
|
|
|
|
|
|
|
// Callback to allow customization of providers after they are constructed
|
|
|
|
// but before they call out into their initialization code.
|
|
|
|
this.onProviderInit = null;
|
2012-11-05 13:45:35 -08:00
|
|
|
}
|
|
|
|
|
2013-03-07 05:06:46 -08:00
|
|
|
this.ProviderManager.prototype = Object.freeze({
|
2013-04-16 11:46:05 -07:00
|
|
|
PULL_ONLY_NOT_REGISTERED: "none",
|
|
|
|
PULL_ONLY_REGISTERING: "registering",
|
|
|
|
PULL_ONLY_UNREGISTERING: "unregistering",
|
|
|
|
PULL_ONLY_REGISTERED: "registered",
|
|
|
|
|
2013-01-06 12:13:19 -08:00
|
|
|
get providers() {
|
|
|
|
let providers = [];
|
|
|
|
for (let [name, entry] of this._providers) {
|
|
|
|
providers.push(entry.provider);
|
|
|
|
}
|
|
|
|
|
|
|
|
return providers;
|
|
|
|
},
|
|
|
|
|
2013-02-12 16:32:45 -08:00
|
|
|
/**
|
|
|
|
* Obtain a provider from its name.
|
|
|
|
*/
|
|
|
|
getProvider: function (name) {
|
|
|
|
let provider = this._providers.get(name);
|
|
|
|
|
|
|
|
if (!provider) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return provider.provider;
|
|
|
|
},
|
|
|
|
|
2013-03-11 14:12:24 -07:00
|
|
|
/**
|
|
|
|
* Registers providers from a category manager category.
|
|
|
|
*
|
|
|
|
* This examines the specified category entries and registers found
|
|
|
|
* providers.
|
|
|
|
*
|
|
|
|
* Category entries are essentially JS modules and the name of the symbol
|
|
|
|
* within that module that is a `Metrics.Provider` instance.
|
|
|
|
*
|
|
|
|
* The category entry name is the name of the JS type for the provider. The
|
|
|
|
* value is the resource:// URI to import which makes this type available.
|
|
|
|
*
|
|
|
|
* Example entry:
|
|
|
|
*
|
|
|
|
* FooProvider resource://gre/modules/foo.jsm
|
|
|
|
*
|
|
|
|
* One can register entries in the application's .manifest file. e.g.
|
|
|
|
*
|
2013-03-21 20:13:56 -07:00
|
|
|
* category healthreport-js-provider-default FooProvider resource://gre/modules/foo.jsm
|
|
|
|
* category healthreport-js-provider-nightly EyeballProvider resource://gre/modules/eyeball.jsm
|
2013-03-11 14:12:24 -07:00
|
|
|
*
|
|
|
|
* Then to load them:
|
|
|
|
*
|
|
|
|
* let reporter = getHealthReporter("healthreport.");
|
2013-03-21 20:13:56 -07:00
|
|
|
* reporter.registerProvidersFromCategoryManager("healthreport-js-provider-default");
|
|
|
|
*
|
|
|
|
* If the category has no defined members, this call has no effect, and no error is raised.
|
2013-03-11 14:12:24 -07:00
|
|
|
*
|
|
|
|
* @param category
|
2013-03-21 20:13:56 -07:00
|
|
|
* (string) Name of category from which to query and load.
|
|
|
|
* @return a newly spawned Task.
|
2013-03-11 14:12:24 -07:00
|
|
|
*/
|
|
|
|
registerProvidersFromCategoryManager: function (category) {
|
|
|
|
this._log.info("Registering providers from category: " + category);
|
|
|
|
let cm = Cc["@mozilla.org/categorymanager;1"]
|
|
|
|
.getService(Ci.nsICategoryManager);
|
|
|
|
|
|
|
|
let promises = [];
|
|
|
|
let enumerator = cm.enumerateCategory(category);
|
|
|
|
while (enumerator.hasMoreElements()) {
|
|
|
|
let entry = enumerator.getNext()
|
|
|
|
.QueryInterface(Ci.nsISupportsCString)
|
|
|
|
.toString();
|
|
|
|
|
|
|
|
let uri = cm.getCategoryEntry(category, entry);
|
|
|
|
this._log.info("Attempting to load provider from category manager: " +
|
|
|
|
entry + " from " + uri);
|
|
|
|
|
|
|
|
try {
|
|
|
|
let ns = {};
|
|
|
|
Cu.import(uri, ns);
|
|
|
|
|
|
|
|
let promise = this.registerProviderFromType(ns[entry]);
|
|
|
|
if (promise) {
|
|
|
|
promises.push(promise);
|
|
|
|
}
|
|
|
|
} catch (ex) {
|
|
|
|
this._recordError("Error registering provider from category manager : " +
|
|
|
|
entry + ": ", ex);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Task.spawn(function wait() {
|
|
|
|
for (let promise of promises) {
|
|
|
|
yield promise;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2012-11-05 13:45:35 -08:00
|
|
|
/**
|
2013-03-07 05:06:46 -08:00
|
|
|
* Registers a `MetricsProvider` with this manager.
|
2012-11-05 13:45:35 -08:00
|
|
|
*
|
|
|
|
* Once a `MetricsProvider` is registered, data will be collected from it
|
|
|
|
* whenever we collect data.
|
|
|
|
*
|
2013-01-06 12:13:19 -08:00
|
|
|
* The returned value is a promise that will be resolved once registration
|
|
|
|
* is complete.
|
|
|
|
*
|
2013-01-06 22:46:30 -08:00
|
|
|
* Providers are initialized as part of registration by calling
|
|
|
|
* provider.init().
|
|
|
|
*
|
2012-11-05 13:45:35 -08:00
|
|
|
* @param provider
|
2013-01-06 12:13:19 -08:00
|
|
|
* (Metrics.Provider) The provider instance to register.
|
|
|
|
*
|
|
|
|
* @return Promise<null>
|
2012-11-05 13:45:35 -08:00
|
|
|
*/
|
2013-01-06 12:13:19 -08:00
|
|
|
registerProvider: function (provider) {
|
|
|
|
if (!(provider instanceof Provider)) {
|
|
|
|
throw new Error("Argument must be a Provider instance.");
|
2012-11-05 13:45:35 -08:00
|
|
|
}
|
|
|
|
|
2013-01-06 12:13:19 -08:00
|
|
|
if (this._providers.has(provider.name)) {
|
2013-03-18 20:47:34 -07:00
|
|
|
return CommonUtils.laterTickResolvingPromise();
|
2012-11-05 13:45:35 -08:00
|
|
|
}
|
|
|
|
|
2013-01-06 22:46:30 -08:00
|
|
|
let deferred = Promise.defer();
|
|
|
|
this._providerInitQueue.push([provider, deferred]);
|
2013-01-06 12:13:19 -08:00
|
|
|
|
2013-01-06 22:46:30 -08:00
|
|
|
if (this._providerInitQueue.length == 1) {
|
|
|
|
this._popAndInitProvider();
|
|
|
|
}
|
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
},
|
|
|
|
|
2013-03-11 14:12:24 -07:00
|
|
|
/**
|
|
|
|
* Registers a provider from its constructor function.
|
|
|
|
*
|
|
|
|
* If the provider is pull-only, it will be stashed away and
|
|
|
|
* initialized later. Null will be returned.
|
|
|
|
*
|
|
|
|
* If it is not pull-only, it will be initialized immediately and a
|
|
|
|
* promise will be returned. The promise will be resolved when the
|
|
|
|
* provider has finished initializing.
|
|
|
|
*/
|
|
|
|
registerProviderFromType: function (type) {
|
|
|
|
let proto = type.prototype;
|
|
|
|
if (proto.pullOnly) {
|
|
|
|
this._log.info("Provider is pull-only. Deferring initialization: " +
|
|
|
|
proto.name);
|
|
|
|
this._pullOnlyProviders[proto.name] = type;
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
let provider = this._initProviderFromType(type);
|
|
|
|
return this.registerProvider(provider);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes a provider from its type.
|
|
|
|
*
|
|
|
|
* This is how a constructor function should be turned into a provider
|
|
|
|
* instance.
|
|
|
|
*
|
|
|
|
* A side-effect is the provider is registered with the manager.
|
|
|
|
*/
|
|
|
|
_initProviderFromType: function (type) {
|
|
|
|
let provider = new type();
|
|
|
|
if (this.onProviderInit) {
|
|
|
|
this.onProviderInit(provider);
|
|
|
|
}
|
|
|
|
|
|
|
|
return provider;
|
|
|
|
},
|
|
|
|
|
2013-01-31 08:58:19 -08:00
|
|
|
/**
|
2013-03-07 05:06:46 -08:00
|
|
|
* Remove a named provider from the manager.
|
2013-01-31 08:58:19 -08:00
|
|
|
*
|
|
|
|
* It is the caller's responsibility to shut down the provider
|
|
|
|
* instance.
|
|
|
|
*/
|
|
|
|
unregisterProvider: function (name) {
|
|
|
|
this._providers.delete(name);
|
|
|
|
},
|
|
|
|
|
2013-03-11 14:12:24 -07:00
|
|
|
/**
|
|
|
|
* Ensure that pull-only providers are registered.
|
|
|
|
*/
|
|
|
|
ensurePullOnlyProvidersRegistered: function () {
|
2013-04-16 11:46:05 -07:00
|
|
|
let state = this._pullOnlyProvidersState;
|
|
|
|
|
|
|
|
this._pullOnlyProvidersRegisterCount++;
|
|
|
|
|
|
|
|
if (state == this.PULL_ONLY_REGISTERED) {
|
|
|
|
this._log.debug("Requested pull-only provider registration and " +
|
|
|
|
"providers are already registered.");
|
2013-03-18 20:47:34 -07:00
|
|
|
return CommonUtils.laterTickResolvingPromise();
|
2013-03-11 14:12:24 -07:00
|
|
|
}
|
|
|
|
|
2013-04-16 11:46:05 -07:00
|
|
|
// If we're in the process of registering, chain off that request.
|
|
|
|
if (state == this.PULL_ONLY_REGISTERING) {
|
|
|
|
this._log.debug("Requested pull-only provider registration and " +
|
|
|
|
"registration is already in progress.");
|
|
|
|
return this._pullOnlyProvidersCurrentPromise;
|
|
|
|
}
|
2013-03-11 14:12:24 -07:00
|
|
|
|
2013-04-16 11:46:05 -07:00
|
|
|
this._log.debug("Pull-only provider registration requested.");
|
|
|
|
|
|
|
|
// A side-effect of setting this is that an active unregistration will
|
|
|
|
// effectively short circuit and finish as soon as the in-flight
|
|
|
|
// unregistration (if any) finishes.
|
|
|
|
this._pullOnlyProvidersState = this.PULL_ONLY_REGISTERING;
|
|
|
|
|
|
|
|
let inFlightPromise = this._pullOnlyProvidersCurrentPromise;
|
|
|
|
|
|
|
|
this._pullOnlyProvidersCurrentPromise =
|
|
|
|
Task.spawn(function registerPullProviders() {
|
|
|
|
|
|
|
|
if (inFlightPromise) {
|
|
|
|
this._log.debug("Waiting for in-flight pull-only provider activity " +
|
|
|
|
"to finish before registering.");
|
|
|
|
try {
|
|
|
|
yield inFlightPromise;
|
|
|
|
} catch (ex) {
|
|
|
|
this._log.warn("Error when waiting for existing pull-only promise: " +
|
|
|
|
CommonUtils.exceptionStr(ex));
|
|
|
|
}
|
|
|
|
}
|
2013-03-11 14:12:24 -07:00
|
|
|
|
|
|
|
for each (let providerType in this._pullOnlyProviders) {
|
2013-04-16 11:46:05 -07:00
|
|
|
// Short-circuit if we're no longer registering.
|
|
|
|
if (this._pullOnlyProvidersState != this.PULL_ONLY_REGISTERING) {
|
|
|
|
this._log.debug("Aborting pull-only provider registration.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-03-11 14:12:24 -07:00
|
|
|
try {
|
|
|
|
let provider = this._initProviderFromType(providerType);
|
2013-04-16 11:46:05 -07:00
|
|
|
|
|
|
|
// This is a no-op if the provider is already registered. So, the
|
|
|
|
// only overhead is constructing an instance. This should be cheap
|
|
|
|
// and isn't worth optimizing.
|
2013-03-11 14:12:24 -07:00
|
|
|
yield this.registerProvider(provider);
|
|
|
|
} catch (ex) {
|
|
|
|
this._recordError("Error registering pull-only provider", ex);
|
|
|
|
}
|
|
|
|
}
|
2013-04-16 11:46:05 -07:00
|
|
|
|
|
|
|
// It's possible we changed state while registering. Only mark as
|
|
|
|
// registered if we didn't change state.
|
|
|
|
if (this._pullOnlyProvidersState == this.PULL_ONLY_REGISTERING) {
|
|
|
|
this._pullOnlyProvidersState = this.PULL_ONLY_REGISTERED;
|
|
|
|
this._pullOnlyProvidersCurrentPromise = null;
|
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
return this._pullOnlyProvidersCurrentPromise;
|
2013-03-11 14:12:24 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
ensurePullOnlyProvidersUnregistered: function () {
|
2013-04-16 11:46:05 -07:00
|
|
|
let state = this._pullOnlyProvidersState;
|
|
|
|
|
|
|
|
// If we're not registered, this is a no-op.
|
|
|
|
if (state == this.PULL_ONLY_NOT_REGISTERED) {
|
|
|
|
this._log.debug("Requested pull-only provider unregistration but none " +
|
|
|
|
"are registered.");
|
2013-03-18 20:47:34 -07:00
|
|
|
return CommonUtils.laterTickResolvingPromise();
|
2013-03-11 14:12:24 -07:00
|
|
|
}
|
|
|
|
|
2013-04-16 11:46:05 -07:00
|
|
|
// If we're currently unregistering, recycle the promise from last time.
|
|
|
|
if (state == this.PULL_ONLY_UNREGISTERING) {
|
|
|
|
this._log.debug("Requested pull-only provider unregistration and " +
|
|
|
|
"unregistration is in progress.");
|
|
|
|
this._pullOnlyProvidersRegisterCount =
|
|
|
|
Math.max(0, this._pullOnlyProvidersRegisterCount - 1);
|
2013-03-11 14:12:24 -07:00
|
|
|
|
2013-04-16 11:46:05 -07:00
|
|
|
return this._pullOnlyProvidersCurrentPromise;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We ignore this request while multiple entities have requested
|
|
|
|
// registration because we don't want a request from an "inner,"
|
|
|
|
// short-lived request to overwrite the desire of the "parent,"
|
|
|
|
// longer-lived request.
|
|
|
|
if (this._pullOnlyProvidersRegisterCount > 1) {
|
|
|
|
this._log.debug("Requested pull-only provider unregistration while " +
|
|
|
|
"other callers still want them registered. Ignoring.");
|
|
|
|
this._pullOnlyProvidersRegisterCount--;
|
2013-03-18 20:47:34 -07:00
|
|
|
return CommonUtils.laterTickResolvingPromise();
|
2013-04-16 11:46:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// We are either fully registered or registering with a single consumer.
|
|
|
|
// In both cases we are authoritative and can commence unregistration.
|
|
|
|
|
|
|
|
this._log.debug("Pull-only providers being unregistered.");
|
|
|
|
this._pullOnlyProvidersRegisterCount =
|
|
|
|
Math.max(0, this._pullOnlyProvidersRegisterCount - 1);
|
|
|
|
this._pullOnlyProvidersState = this.PULL_ONLY_UNREGISTERING;
|
|
|
|
let inFlightPromise = this._pullOnlyProvidersCurrentPromise;
|
|
|
|
|
|
|
|
this._pullOnlyProvidersCurrentPromise =
|
|
|
|
Task.spawn(function unregisterPullProviders() {
|
|
|
|
|
|
|
|
if (inFlightPromise) {
|
|
|
|
this._log.debug("Waiting for in-flight pull-only provider activity " +
|
|
|
|
"to complete before unregistering.");
|
|
|
|
try {
|
|
|
|
yield inFlightPromise;
|
|
|
|
} catch (ex) {
|
|
|
|
this._log.warn("Error when waiting for existing pull-only promise: " +
|
|
|
|
CommonUtils.exceptionStr(ex));
|
|
|
|
}
|
|
|
|
}
|
2013-03-11 14:12:24 -07:00
|
|
|
|
|
|
|
for (let provider of this.providers) {
|
2013-04-16 11:46:05 -07:00
|
|
|
if (this._pullOnlyProvidersState != this.PULL_ONLY_UNREGISTERING) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-03-11 14:12:24 -07:00
|
|
|
if (!provider.pullOnly) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._log.info("Shutting down pull-only provider: " +
|
|
|
|
provider.name);
|
|
|
|
|
|
|
|
try {
|
|
|
|
yield provider.shutdown();
|
|
|
|
} catch (ex) {
|
|
|
|
this._recordError("Error when shutting down provider: " +
|
|
|
|
provider.name, ex);
|
|
|
|
} finally {
|
|
|
|
this.unregisterProvider(provider.name);
|
|
|
|
}
|
|
|
|
}
|
2013-04-16 11:46:05 -07:00
|
|
|
|
|
|
|
if (this._pullOnlyProvidersState == this.PULL_ONLY_UNREGISTERING) {
|
|
|
|
this._pullOnlyProvidersState = this.PULL_ONLY_NOT_REGISTERED;
|
|
|
|
this._pullOnlyProvidersCurrentPromise = null;
|
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
return this._pullOnlyProvidersCurrentPromise;
|
2013-03-11 14:12:24 -07:00
|
|
|
},
|
|
|
|
|
2013-01-06 22:46:30 -08:00
|
|
|
_popAndInitProvider: function () {
|
|
|
|
if (!this._providerInitQueue.length || this._providerInitializing) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-07 16:29:25 -08:00
|
|
|
let [provider, deferred] = this._providerInitQueue.shift();
|
2013-01-06 22:46:30 -08:00
|
|
|
this._providerInitializing = true;
|
|
|
|
|
|
|
|
this._log.info("Initializing provider with storage: " + provider.name);
|
|
|
|
|
2013-02-18 16:11:43 -08:00
|
|
|
Task.spawn(function initProvider() {
|
|
|
|
try {
|
|
|
|
let result = yield provider.init(this._storage);
|
|
|
|
this._log.info("Provider successfully initialized: " + provider.name);
|
2013-01-06 22:46:30 -08:00
|
|
|
|
|
|
|
this._providers.set(provider.name, {
|
|
|
|
provider: provider,
|
|
|
|
constantsCollected: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
deferred.resolve(result);
|
2013-02-18 16:11:43 -08:00
|
|
|
} catch (ex) {
|
2013-02-27 16:52:26 -08:00
|
|
|
this._recordProviderError(provider.name, "Failed to initialize", ex);
|
2013-02-18 16:11:43 -08:00
|
|
|
deferred.reject(ex);
|
|
|
|
} finally {
|
2013-01-06 22:46:30 -08:00
|
|
|
this._providerInitializing = false;
|
|
|
|
this._popAndInitProvider();
|
2013-02-18 16:11:43 -08:00
|
|
|
}
|
|
|
|
}.bind(this));
|
2012-11-05 13:45:35 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2013-03-07 05:06:46 -08:00
|
|
|
* is that the manager is populated with the obtained collection results.
|
|
|
|
* The resolved value to the promise is this `ProviderManager` instance.
|
2012-11-05 13:45:35 -08:00
|
|
|
*/
|
2013-01-06 12:13:19 -08:00
|
|
|
collectConstantData: function () {
|
2013-02-05 09:59:10 -08:00
|
|
|
let entries = [];
|
2012-11-05 13:45:35 -08:00
|
|
|
|
2013-01-06 12:13:19 -08:00
|
|
|
for (let [name, entry] of this._providers) {
|
|
|
|
if (entry.constantsCollected) {
|
2012-11-05 13:45:35 -08:00
|
|
|
this._log.trace("Provider has already provided constant data: " +
|
2012-11-08 15:32:49 -08:00
|
|
|
name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-02-05 09:59:10 -08:00
|
|
|
entries.push(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
let onCollect = function (entry, result) {
|
|
|
|
entry.constantsCollected = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
return this._callCollectOnProviders(entries, "collectConstantData",
|
|
|
|
onCollect);
|
|
|
|
},
|
|
|
|
|
2013-02-05 09:59:13 -08:00
|
|
|
/**
|
|
|
|
* Calls collectDailyData on all providers.
|
|
|
|
*/
|
|
|
|
collectDailyData: function () {
|
|
|
|
return this._callCollectOnProviders(this._providers.values(),
|
|
|
|
"collectDailyData");
|
|
|
|
},
|
|
|
|
|
2013-02-05 09:59:10 -08:00
|
|
|
_callCollectOnProviders: function (entries, fnProperty, onCollect=null) {
|
|
|
|
let promises = [];
|
|
|
|
|
|
|
|
for (let entry of entries) {
|
|
|
|
let provider = entry.provider;
|
2013-01-06 12:13:19 -08:00
|
|
|
let collectPromise;
|
2012-11-08 15:32:49 -08:00
|
|
|
try {
|
2013-02-05 09:59:10 -08:00
|
|
|
collectPromise = provider[fnProperty].call(provider);
|
2012-11-08 15:32:49 -08:00
|
|
|
} catch (ex) {
|
2013-02-27 16:52:26 -08:00
|
|
|
this._recordProviderError(provider.name, "Exception when calling " +
|
|
|
|
"collect function: " + fnProperty, ex);
|
2012-11-05 13:45:35 -08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-01-06 12:13:19 -08:00
|
|
|
if (!collectPromise) {
|
2013-02-27 16:52:26 -08:00
|
|
|
this._recordProviderError(provider.name, "Does not return a promise " +
|
|
|
|
"from " + fnProperty + "()");
|
2013-02-05 09:59:10 -08:00
|
|
|
continue;
|
2012-11-08 15:32:49 -08:00
|
|
|
}
|
|
|
|
|
2013-01-06 12:13:19 -08:00
|
|
|
let promise = collectPromise.then(function onCollected(result) {
|
2013-02-05 09:59:10 -08:00
|
|
|
if (onCollect) {
|
|
|
|
try {
|
|
|
|
onCollect(entry, result);
|
|
|
|
} catch (ex) {
|
|
|
|
this._log.warn("onCollect callback threw: " +
|
|
|
|
CommonUtils.exceptionStr(ex));
|
|
|
|
}
|
|
|
|
}
|
2012-11-05 13:45:35 -08:00
|
|
|
|
2013-03-18 20:47:34 -07:00
|
|
|
return CommonUtils.laterTickResolvingPromise(result);
|
2012-11-05 13:45:35 -08:00
|
|
|
});
|
|
|
|
|
2013-02-05 09:59:10 -08:00
|
|
|
promises.push([provider.name, promise]);
|
2012-11-05 13:45:35 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return this._handleCollectionPromises(promises);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles promises returned by the collect* functions.
|
|
|
|
*
|
|
|
|
* This consumes the data resolved by the promises and returns a new promise
|
|
|
|
* that will be resolved once all promises have been resolved.
|
2013-01-06 12:13:19 -08:00
|
|
|
*
|
|
|
|
* The promise is resolved even if one of the underlying collection
|
|
|
|
* promises is rejected.
|
2012-11-05 13:45:35 -08:00
|
|
|
*/
|
2013-01-06 12:13:19 -08:00
|
|
|
_handleCollectionPromises: function (promises) {
|
2013-02-18 16:11:43 -08:00
|
|
|
return Task.spawn(function waitForPromises() {
|
|
|
|
for (let [name, promise] of promises) {
|
|
|
|
try {
|
|
|
|
yield promise;
|
|
|
|
this._log.debug("Provider collected successfully: " + name);
|
|
|
|
} catch (ex) {
|
2013-02-27 16:52:26 -08:00
|
|
|
this._recordProviderError(name, "Failed to collect", ex);
|
2013-02-18 16:11:43 -08:00
|
|
|
}
|
2012-11-05 13:45:35 -08:00
|
|
|
}
|
|
|
|
|
2013-02-18 16:11:43 -08:00
|
|
|
throw new Task.Result(this);
|
|
|
|
}.bind(this));
|
2012-11-05 13:45:35 -08:00
|
|
|
},
|
2013-02-27 16:52:26 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Record an error that occurred operating on a provider.
|
|
|
|
*/
|
|
|
|
_recordProviderError: function (name, msg, ex) {
|
|
|
|
let msg = "Provider error: " + name + ": " + msg;
|
|
|
|
if (ex) {
|
2013-03-18 15:07:07 -07:00
|
|
|
msg += ": " + CommonUtils.exceptionStr(ex);
|
2013-02-27 16:52:26 -08:00
|
|
|
}
|
|
|
|
this._log.warn(msg);
|
|
|
|
|
|
|
|
if (this.onProviderError) {
|
|
|
|
try {
|
|
|
|
this.onProviderError(msg);
|
|
|
|
} catch (callError) {
|
|
|
|
this._log.warn("Exception when calling onProviderError callback: " +
|
|
|
|
CommonUtils.exceptionStr(callError));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2013-01-06 12:13:19 -08:00
|
|
|
});
|
2012-11-05 13:45:35 -08:00
|
|
|
|