vulcain-corejs
Version:
Vulcain micro-service framework
120 lines (118 loc) • 4.49 kB
JavaScript
;
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 = [];
}
ensuresPolling() {
if (!this.disposed && !this._polling) {
this.polling();
this._polling = true;
}
}
polling() {
return __awaiter(this, void 0, void 0, function* () {
let list = this._sources;
let results = [];
if (this.disposed)
return;
if (list) {
for (let src of list) {
try {
results.push(yield src.pollPropertiesAsync(this.sourceTimeoutInMs));
}
catch (e) {
system_1.System.log.error(null, e, "CONFIG: Error when polling configuration source ");
}
}
this.loadPropertiesFromSources(results);
}
setTimeout(this.polling.bind(this), this.pollingIntervalInSeconds * 1000);
});
}
loadPropertiesFromSources(results) {
for (let result of results) {
try {
if (!result) {
continue;
}
if (result.values && result.values.size > 0)
this.loadProperties(result);
// First time only
let src = result.source;
if (src && src.__onInitialized) {
src.__onInitialized();
delete src.__onInitialized;
}
}
catch (e) {
}
}
}
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 {
var 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);
system_1.System.log.info(null, `CONFIG: Setting property value ${item.value} for key ${key}`);
}
catch (e) {
system_1.System.log.error(null, e, `CONFIG: Error on loadProperties for key ${key}`);
}
});
}
dispose() {
this.disposed = true;
}
/**
* Initialize source(s) and return only when all sources are initialized
* @param sources List of sources
* @returns {Promise<T>}
*/
registerSourcesAsync(sources) {
return new Promise((resolve) => {
let latch = 0;
for (let source of sources) {
if (this._sources.indexOf(source) >= 0)
continue;
source.__onInitialized = () => {
latch--;
if (latch === 0) {
resolve();
}
};
this._sources.push(source);
latch++;
}
// Run initialization
this.ensuresPolling();
if (latch === 0)
resolve();
});
}
}
exports.ConfigurationManager = ConfigurationManager;
//# sourceMappingURL=configurationManager.js.map