2012-11-06 01:45:35 +04: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 23:26:48 +04:00
|
|
|
#ifndef MERGED_COMPARTMENT
|
2013-03-07 17:06:46 +04:00
|
|
|
this.EXPORTED_SYMBOLS = ["ProviderManager"];
|
2012-11-06 01:45:35 +04:00
|
|
|
|
2013-03-12 01:12:24 +04:00
|
|
|
const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
|
2012-11-06 01:45:35 +04:00
|
|
|
|
2013-01-27 23:26:48 +04:00
|
|
|
Cu.import("resource://gre/modules/services/metrics/dataprovider.jsm");
|
|
|
|
#endif
|
|
|
|
|
2013-02-01 23:43:15 +04:00
|
|
|
Cu.import("resource://gre/modules/commonjs/sdk/core/promise.js");
|
2013-01-07 00:13:19 +04:00
|
|
|
Cu.import("resource://gre/modules/Task.jsm");
|
2012-11-06 01:45:35 +04:00
|
|
|
Cu.import("resource://services-common/log4moz.js");
|
2012-11-09 03:32:49 +04:00
|
|
|
Cu.import("resource://services-common/utils.js");
|
2012-11-06 01:45:35 +04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles and coordinates the collection of metrics data from providers.
|
|
|
|
*
|
2013-01-07 00:13:19 +04:00
|
|
|
* This provides an interface for managing `Metrics.Provider` instances. It
|
2012-11-06 01:45:35 +04:00
|
|
|
* provides APIs for bulk collection of data.
|
|
|
|
*/
|
2013-03-07 17:06:46 +04:00
|
|
|
this.ProviderManager = function (storage) {
|
|
|
|
this._log = Log4Moz.repository.getLogger("Services.Metrics.ProviderManager");
|
2013-01-07 00:13:19 +04:00
|
|
|
|
|
|
|
this._providers = new Map();
|
|
|
|
this._storage = storage;
|
2012-11-06 01:45:35 +04:00
|
|
|
|
2013-01-07 10:46:30 +04:00
|
|
|
this._providerInitQueue = [];
|
|
|
|
this._providerInitializing = false;
|
2013-03-12 01:12:24 +04:00
|
|
|
|
|
|
|
this._pullOnlyProviders = {};
|
2013-04-16 22:46:05 +04:00
|
|
|
this._pullOnlyProvidersRegisterCount = 0;
|
|
|
|
this._pullOnlyProvidersState = this.PULL_ONLY_NOT_REGISTERED;
|
|
|
|
this._pullOnlyProvidersCurrentPromise = null;
|
2013-03-12 01:12:24 +04: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-06 01:45:35 +04:00
|
|
|
}
|
|
|
|
|
2013-03-07 17:06:46 +04:00
|
|
|
this.ProviderManager.prototype = Object.freeze({
|
2013-04-16 22:46:05 +04:00
|
|
|
PULL_ONLY_NOT_REGISTERED: "none",
|
|
|
|
PULL_ONLY_REGISTERING: "registering",
|
|
|
|
PULL_ONLY_UNREGISTERING: "unregistering",
|
|
|
|
PULL_ONLY_REGISTERED: "registered",
|
|
|
|
|
2013-01-07 00:13:19 +04:00
|
|
|
get providers() {
|
|
|
|
let providers = [];
|
|
|
|
for (let [name, entry] of this._providers) {
|
|
|
|
providers.push(entry.provider);
|
|
|
|
}
|
|
|
|
|
|
|
|
return providers;
|
|
|
|
},
|
|
|
|
|
2013-02-13 04:32:45 +04: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-12 01:12:24 +04: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-22 07:13:56 +04: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-12 01:12:24 +04:00
|
|
|
*
|
|
|
|
* Then to load them:
|
|
|
|
*
|
|
|
|
* let reporter = getHealthReporter("healthreport.");
|
2013-03-22 07:13:56 +04: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-12 01:12:24 +04:00
|
|
|
*
|
|
|
|
* @param category
|
2013-03-22 07:13:56 +04:00
|
|
|
* (string) Name of category from which to query and load.
|
|
|
|
* @return a newly spawned Task.
|
2013-03-12 01:12:24 +04: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-06 01:45:35 +04:00
|
|
|
/**
|
2013-03-07 17:06:46 +04:00
|
|
|
* Registers a `MetricsProvider` with this manager.
|
2012-11-06 01:45:35 +04:00
|
|
|
*
|
|
|
|
* Once a `MetricsProvider` is registered, data will be collected from it
|
|
|
|
* whenever we collect data.
|
|
|
|
*
|
2013-01-07 00:13:19 +04:00
|
|
|
* The returned value is a promise that will be resolved once registration
|
|
|
|
* is complete.
|
|
|
|
*
|
2013-01-07 10:46:30 +04:00
|
|
|
* Providers are initialized as part of registration by calling
|
|
|
|
* provider.init().
|
|
|
|
*
|
2012-11-06 01:45:35 +04:00
|
|
|
* @param provider
|
2013-01-07 00:13:19 +04:00
|
|
|
* (Metrics.Provider) The provider instance to register.
|
|
|
|
*
|
|
|
|
* @return Promise<null>
|
2012-11-06 01:45:35 +04:00
|
|
|
*/
|
2013-01-07 00:13:19 +04:00
|
|
|
registerProvider: function (provider) {
|
|
|
|
if (!(provider instanceof Provider)) {
|
|
|
|
throw new Error("Argument must be a Provider instance.");
|
2012-11-06 01:45:35 +04:00
|
|
|
}
|
|
|
|
|
2013-01-07 00:13:19 +04:00
|
|
|
if (this._providers.has(provider.name)) {
|
2013-03-19 07:47:34 +04:00
|
|
|
return CommonUtils.laterTickResolvingPromise();
|
2012-11-06 01:45:35 +04:00
|
|
|
}
|
|
|
|
|
2013-01-07 10:46:30 +04:00
|
|
|
let deferred = Promise.defer();
|
|
|
|
this._providerInitQueue.push([provider, deferred]);
|
2013-01-07 00:13:19 +04:00
|
|
|
|
2013-01-07 10:46:30 +04:00
|
|
|
if (this._providerInitQueue.length == 1) {
|
|
|
|
this._popAndInitProvider();
|
|
|
|
}
|
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
},
|
|
|
|
|
2013-03-12 01:12:24 +04: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 20:58:19 +04:00
|
|
|
/**
|
2013-03-07 17:06:46 +04:00
|
|
|
* Remove a named provider from the manager.
|
2013-01-31 20:58:19 +04:00
|
|
|
*
|
|
|
|
* It is the caller's responsibility to shut down the provider
|
|
|
|
* instance.
|
|
|
|
*/
|
|
|
|
unregisterProvider: function (name) {
|
|
|
|
this._providers.delete(name);
|
|
|
|
},
|
|
|
|
|
2013-03-12 01:12:24 +04:00
|
|
|
/**
|
|
|
|
* Ensure that pull-only providers are registered.
|
|
|
|
*/
|
|
|
|
ensurePullOnlyProvidersRegistered: function () {
|
2013-04-16 22:46:05 +04: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-19 07:47:34 +04:00
|
|
|
return CommonUtils.laterTickResolvingPromise();
|
2013-03-12 01:12:24 +04:00
|
|
|
}
|
|
|
|
|
2013-04-16 22:46:05 +04: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-12 01:12:24 +04:00
|
|
|
|
2013-04-16 22:46:05 +04: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-12 01:12:24 +04:00
|
|
|
|
|
|
|
for each (let providerType in this._pullOnlyProviders) {
|
2013-04-16 22:46:05 +04: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-12 01:12:24 +04:00
|
|
|
try {
|
|
|
|
let provider = this._initProviderFromType(providerType);
|
2013-04-16 22:46:05 +04: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-12 01:12:24 +04:00
|
|
|
yield this.registerProvider(provider);
|
|
|
|
} catch (ex) {
|
|
|
|
this._recordError("Error registering pull-only provider", ex);
|
|
|
|
}
|
|
|
|
}
|
2013-04-16 22:46:05 +04: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-12 01:12:24 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
ensurePullOnlyProvidersUnregistered: function () {
|
2013-04-16 22:46:05 +04: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-19 07:47:34 +04:00
|
|
|
return CommonUtils.laterTickResolvingPromise();
|
2013-03-12 01:12:24 +04:00
|
|
|
}
|
|
|
|
|
2013-04-16 22:46:05 +04: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-12 01:12:24 +04:00
|
|
|
|
2013-04-16 22:46:05 +04: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-19 07:47:34 +04:00
|
|
|
return CommonUtils.laterTickResolvingPromise();
|
2013-04-16 22:46:05 +04: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-12 01:12:24 +04:00
|
|
|
|
|
|
|
for (let provider of this.providers) {
|
2013-04-16 22:46:05 +04:00
|
|
|
if (this._pullOnlyProvidersState != this.PULL_ONLY_UNREGISTERING) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-03-12 01:12:24 +04: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 22:46:05 +04: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-12 01:12:24 +04:00
|
|
|
},
|
|
|
|
|
2013-01-07 10:46:30 +04:00
|
|
|
_popAndInitProvider: function () {
|
|
|
|
if (!this._providerInitQueue.length || this._providerInitializing) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-08 04:29:25 +04:00
|
|
|
let [provider, deferred] = this._providerInitQueue.shift();
|
2013-01-07 10:46:30 +04:00
|
|
|
this._providerInitializing = true;
|
|
|
|
|
|
|
|
this._log.info("Initializing provider with storage: " + provider.name);
|
|
|
|
|
2013-02-19 04:11:43 +04:00
|
|
|
Task.spawn(function initProvider() {
|
|
|
|
try {
|
|
|
|
let result = yield provider.init(this._storage);
|
|
|
|
this._log.info("Provider successfully initialized: " + provider.name);
|
2013-01-07 10:46:30 +04:00
|
|
|
|
|
|
|
this._providers.set(provider.name, {
|
|
|
|
provider: provider,
|
|
|
|
constantsCollected: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
deferred.resolve(result);
|
2013-02-19 04:11:43 +04:00
|
|
|
} catch (ex) {
|
2013-02-28 04:52:26 +04:00
|
|
|
this._recordProviderError(provider.name, "Failed to initialize", ex);
|
2013-02-19 04:11:43 +04:00
|
|
|
deferred.reject(ex);
|
|
|
|
} finally {
|
2013-01-07 10:46:30 +04:00
|
|
|
this._providerInitializing = false;
|
|
|
|
this._popAndInitProvider();
|
2013-02-19 04:11:43 +04:00
|
|
|
}
|
|
|
|
}.bind(this));
|
2012-11-06 01:45:35 +04: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 17:06:46 +04:00
|
|
|
* is that the manager is populated with the obtained collection results.
|
|
|
|
* The resolved value to the promise is this `ProviderManager` instance.
|
2012-11-06 01:45:35 +04:00
|
|
|
*/
|
2013-01-07 00:13:19 +04:00
|
|
|
collectConstantData: function () {
|
2013-02-05 21:59:10 +04:00
|
|
|
let entries = [];
|
2012-11-06 01:45:35 +04:00
|
|
|
|
2013-01-07 00:13:19 +04:00
|
|
|
for (let [name, entry] of this._providers) {
|
|
|
|
if (entry.constantsCollected) {
|
2012-11-06 01:45:35 +04:00
|
|
|
this._log.trace("Provider has already provided constant data: " +
|
2012-11-09 03:32:49 +04:00
|
|
|
name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-02-05 21:59:10 +04:00
|
|
|
entries.push(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
let onCollect = function (entry, result) {
|
|
|
|
entry.constantsCollected = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
return this._callCollectOnProviders(entries, "collectConstantData",
|
|
|
|
onCollect);
|
|
|
|
},
|
|
|
|
|
2013-02-05 21:59:13 +04:00
|
|
|
/**
|
|
|
|
* Calls collectDailyData on all providers.
|
|
|
|
*/
|
|
|
|
collectDailyData: function () {
|
|
|
|
return this._callCollectOnProviders(this._providers.values(),
|
|
|
|
"collectDailyData");
|
|
|
|
},
|
|
|
|
|
2013-02-05 21:59:10 +04:00
|
|
|
_callCollectOnProviders: function (entries, fnProperty, onCollect=null) {
|
|
|
|
let promises = [];
|
|
|
|
|
|
|
|
for (let entry of entries) {
|
|
|
|
let provider = entry.provider;
|
2013-01-07 00:13:19 +04:00
|
|
|
let collectPromise;
|
2012-11-09 03:32:49 +04:00
|
|
|
try {
|
2013-02-05 21:59:10 +04:00
|
|
|
collectPromise = provider[fnProperty].call(provider);
|
2012-11-09 03:32:49 +04:00
|
|
|
} catch (ex) {
|
2013-02-28 04:52:26 +04:00
|
|
|
this._recordProviderError(provider.name, "Exception when calling " +
|
|
|
|
"collect function: " + fnProperty, ex);
|
2012-11-06 01:45:35 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-01-07 00:13:19 +04:00
|
|
|
if (!collectPromise) {
|
2013-02-28 04:52:26 +04:00
|
|
|
this._recordProviderError(provider.name, "Does not return a promise " +
|
|
|
|
"from " + fnProperty + "()");
|
2013-02-05 21:59:10 +04:00
|
|
|
continue;
|
2012-11-09 03:32:49 +04:00
|
|
|
}
|
|
|
|
|
2013-01-07 00:13:19 +04:00
|
|
|
let promise = collectPromise.then(function onCollected(result) {
|
2013-02-05 21:59:10 +04:00
|
|
|
if (onCollect) {
|
|
|
|
try {
|
|
|
|
onCollect(entry, result);
|
|
|
|
} catch (ex) {
|
|
|
|
this._log.warn("onCollect callback threw: " +
|
|
|
|
CommonUtils.exceptionStr(ex));
|
|
|
|
}
|
|
|
|
}
|
2012-11-06 01:45:35 +04:00
|
|
|
|
2013-03-19 07:47:34 +04:00
|
|
|
return CommonUtils.laterTickResolvingPromise(result);
|
2012-11-06 01:45:35 +04:00
|
|
|
});
|
|
|
|
|
2013-02-05 21:59:10 +04:00
|
|
|
promises.push([provider.name, promise]);
|
2012-11-06 01:45:35 +04: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-07 00:13:19 +04:00
|
|
|
*
|
|
|
|
* The promise is resolved even if one of the underlying collection
|
|
|
|
* promises is rejected.
|
2012-11-06 01:45:35 +04:00
|
|
|
*/
|
2013-01-07 00:13:19 +04:00
|
|
|
_handleCollectionPromises: function (promises) {
|
2013-02-19 04:11:43 +04: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-28 04:52:26 +04:00
|
|
|
this._recordProviderError(name, "Failed to collect", ex);
|
2013-02-19 04:11:43 +04:00
|
|
|
}
|
2012-11-06 01:45:35 +04:00
|
|
|
}
|
|
|
|
|
2013-02-19 04:11:43 +04:00
|
|
|
throw new Task.Result(this);
|
|
|
|
}.bind(this));
|
2012-11-06 01:45:35 +04:00
|
|
|
},
|
2013-02-28 04:52:26 +04: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-19 02:07:07 +04:00
|
|
|
msg += ": " + CommonUtils.exceptionStr(ex);
|
2013-02-28 04:52:26 +04: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-07 00:13:19 +04:00
|
|
|
});
|
2012-11-06 01:45:35 +04:00
|
|
|
|