UNPKG

vulcain-corejs

Version:
124 lines (122 loc) 4.81 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments)).next()); }); }; const system_1 = require("./../globals/system"); const dynamicProperty_1 = require("../properties/dynamicProperty"); class ConfigurationManager { constructor(properties, pollingIntervalInSeconds, sourceTimeoutInMs) { this.properties = properties; this.pollingIntervalInSeconds = pollingIntervalInSeconds; this.sourceTimeoutInMs = sourceTimeoutInMs; this._sources = []; } /** * Initialize source(s) and return only when all sources are initialized * @param sources List of sources * @returns {Promise<T>} */ startAsync(sources, pollSources = true) { return __awaiter(this, void 0, void 0, function* () { if (sources) { sources.forEach(source => { if (this._sources.indexOf(source) < 0) this._sources.push(source); }); } // Run initialization let tries = 2; while (tries > 0) { if (yield this.pollingAsync(3000, false)) { // All sources are OK if (pollSources) this.repeatPolling(); return; } tries--; if (tries) system_1.System.log.info(null, "CONFIG: Some dynamic properties sources failed. Retry polling."); } throw new Error("CONFIG: Cannot read properties for all sources."); }); } /** * Pull properties for all sources * * @private * @param {any} [timeout] * @returns * * @memberOf ConfigurationManager */ pollingAsync(timeout, pollSources = true) { return __awaiter(this, void 0, void 0, function* () { let ok = true; try { let list = this._sources; if (this.disposed || !list) return; let promises = []; list.forEach(src => { promises.push( // pollPropertiesAsync cannot failed src.pollPropertiesAsync(timeout || this.sourceTimeoutInMs)); }); let results = yield Promise.all(promises); // Ignore null result results.forEach(res => { if (!res) { ok = false; } else if (res.values && res.values.size > 0) this.loadProperties(res); }); } catch (e) { ok = false; system_1.System.log.error(null, e, "CONFIG: Error when polling sources"); } // Restart if (pollSources) this.repeatPolling(); return ok; }); } repeatPolling() { setTimeout(this.pollingAsync.bind(this), this.pollingIntervalInSeconds * 1000); } loadProperties(props) { if (!props.values) { this.properties.clear(); return; } props.values.forEach((item, key) => { if (!item || item.deleted) { system_1.System.log.info(null, "CONFIG: Removing property value for key " + key); this.properties.Updater_removeProperty(key); return; } try { let prop = this.properties.Updater_getOrCreate(key, () => { return new dynamicProperty_1.DynamicProperty(this.properties, key); }); prop.set(item.encrypted ? JSON.parse(system_1.System.decrypt(item.value)) : item.value); let v = item.encrypted ? "********" : item.value; system_1.System.log.info(null, `CONFIG: Setting property value '${v}' for key ${key}`); } catch (e) { system_1.System.log.error(null, e, `CONFIG: Error on loadProperties for key ${key}`); } }); } dispose() { this.disposed = true; } } exports.ConfigurationManager = ConfigurationManager; //# sourceMappingURL=configurationManager.js.map