Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return __awaiter(this, void 0, void 0, function* () {
const env = di_1.Container.get("ENVIRONMENT");
this.nodeId = env.nodeId;
this.eventEmitter = new eventemitter2_1.EventEmitter2(this.CONFIG.eventEmitter);
this.dbClient = di_1.Container.get(db_1.DbAppClient);
yield this.finishInitialisation();
// set listeners that were cached during booting, clean cache afterwards
Object.values(this.listenersCache).forEach((eventListeners) => {
Object.values(eventListeners).forEach((listener) => {
this._on(listener.eventName, listener.options, listener.callback);
});
});
this.listenersCache = {};
// fire events that were cached during booting, clean cache afterwards
Object.entries(this.emittersCache).forEach((emitterEntry) => {
const eventName = emitterEntry[0];
const eventEmitters = emitterEntry[1];
Object.values(eventEmitters).forEach((emitter) => {
this._emit(eventName, emitter.nodeId, ...emitter.args);
});
});
boot() {
// get settings from DI container
this.ENVIRONMENT = di_1.Container.get("ENVIRONMENT");
terminus(di_1.Container.get(server_1.Server).getServer(), {
// healtcheck options
healthChecks: {
// for now we only resolve a promise to make sure the server runs
"/_health/liveness": () => Promise.resolve(),
// make sure we are ready to answer requests
"/_health/readiness": () => di_1.Container.get(boot_loader_1.BootLoader).getReadyPromise()
},
// cleanup options
timeout: 1000,
logger: this.logger.info
});
// release resources here before node exits
exitHook((callback) => __awaiter(this, void 0, void 0, function* () {
this.logger.info("exiting");
this.logger.info("starting cleanup");
return __awaiter(this, void 0, void 0, function* () {
const env = di_1.Container.get("ENVIRONMENT");
this.nodeId = env.nodeId;
this.eventEmitter = new eventemitter2_1.EventEmitter2(this.CONFIG.eventEmitter);
this.dbClient = di_1.Container.get(db_1.DbAppClient);
yield this.finishInitialisation();
// set listeners that were cached during booting, clean cache afterwards
Object.values(this.listenersCache).forEach((eventListeners) => {
Object.values(eventListeners).forEach((listener) => {
this._on(listener.eventName, listener.options, listener.callback);
});
});
this.listenersCache = {};
// fire events that were cached during booting, clean cache afterwards
Object.entries(this.emittersCache).forEach((emitterEntry) => {
const eventName = emitterEntry[0];
const eventEmitters = emitterEntry[1];
Object.values(eventEmitters).forEach((emitter) => {
this.ENVIRONMENT = {
frameworkVersion: null,
NODE_ENV: process.env.NODE_ENV,
name: null,
version: null,
path: null,
namespace: null,
// unique instance ID (6 char)
nodeId: null
};
this.configModules = [];
this.projectConfig = {};
this.config = {};
// start with empty objects in DI
di_1.Container.set('CONFIG', {});
di_1.Container.set('ENVIRONMENT', {});
// load project config
const projectConfigFolderPath = path.dirname(require.main.filename) + '/config';
this.projectConfig = this.requireConfigFiles(projectConfigFolderPath);
// register package config
this.myConfig = this.registerConfig('Config', __dirname + '/../config');
}
requireConfigFiles(moduleConfigPath) {
const projectPath = path.dirname(require.main.filename);
const PROJECT_PACKAGE = require(projectPath + '/package.json');
// each package in the mono repo has the same version
const MODULE_PACKAGE = require('../package.json');
// update ENV
this.ENVIRONMENT.frameworkVersion = MODULE_PACKAGE.version;
this.ENVIRONMENT.NODE_ENV = process.env.NODE_ENV;
this.ENVIRONMENT.name = PROJECT_PACKAGE.name;
this.ENVIRONMENT.version = PROJECT_PACKAGE.version;
this.ENVIRONMENT.path = projectPath;
// unique instance ID (6 char)
this.ENVIRONMENT.nodeId = this.ENVIRONMENT.nodeId || crypto_1.randomBytes(20).toString('hex').substr(5, 6);
// wait until core config is set
this.ENVIRONMENT.namespace = this.config.Config.namespace;
// put config into DI
di_1.Container.set('ENVIRONMENT', this.ENVIRONMENT);
}
/* PUBLIC */
return __awaiter(this, void 0, void 0, function* () {
// get DB pgClient from DI container
const dbAppClient = di_1.Container.get(db_1.DbAppClient);
const dbClient = dbAppClient.pgClient;
// check latest version migrated
let latestVersion = 0;
try {
const dbInitVersion = (yield dbClient.query("SELECT value FROM _meta.info WHERE key = 'version';")).rows[0];
if (dbInitVersion != null && dbInitVersion.value != null) {
latestVersion = parseInt(dbInitVersion.value, 10);
this.logger.debug("migration.db.init.version.detected", latestVersion);
}
}
catch (err) {
this.logger.info("migration.db.init.not.found");
}
// find init scripts to ignore (version lower than the current one)
const initSqlFolders = [];
// run through all registered packages
constructor(
// @Inject(type => EventEmitter) eventEmitter?,
loggerFactory, config, bootLoader) {
this.config = config;
this.loggerFactory = loggerFactory;
this.bootLoader = bootLoader;
// register package config
this.serverConfig = config.registerConfig("Server", `${__dirname}/../config`);
// this.eventEmitter = eventEmitter;
this.logger = this.loggerFactory.create(this.constructor.name);
// get env from DI container
this.ENVIRONMENT = di_1.Container.get("ENVIRONMENT");
this.bootKoa();
bootLoader.addBootFunction(this.constructor.name, this.boot.bind(this));
}
boot() {
constructor(loggerFactory, bootLoader, config, schemaBuilder, dbAppClient) {
// DI
this.schemaBuilder = schemaBuilder;
this.config = config;
// init logger
this.logger = loggerFactory.create(this.constructor.name);
// get settings from DI container
this.ENVIRONMENT = di_1.Container.get("ENVIRONMENT");
bootLoader.addBootFunction(this.constructor.name, this.boot.bind(this));
}
boot() {
return __awaiter(this, void 0, void 0, function* () {
const configDbGeneral = this.CONFIG.general;
// get known nodes from container, initially assume we are the first one
let knownNodesCount = 1;
try {
const knownNodes = di_1.Container.get("knownNodeIds");
knownNodesCount = knownNodes.length;
}
catch (_a) {
// ignore error and continue assuming we are the first client
}
// reserve one for DbAppClient connection
const connectionsPerInstance = Math.floor(configDbGeneral.totalMax / knownNodesCount - 1);
// readjust pool only if number of max connections has changed
if (this.credentials == null || this.credentials.max !== connectionsPerInstance) {
// gracefully end previous pool if already available
if (this.managedPool != null) {
// don't wait for promise, we just immediately create a new pool
// this one will end as soon as the last connection is released
this.end();
}
// credentials for general connection pool with calculated pool size
return __awaiter(this, void 0, void 0, function* () {
// get DB pgClient from DI container
const dbClient = di_1.Container.get(db_1.DbAppClient).pgClient;
// check latest version migrated
let latestVersion = 0;
try {
const dbInitVersion = (yield dbClient.query(`SELECT value FROM _meta.info WHERE key = 'version';`)).rows[0];
if (dbInitVersion != null && dbInitVersion.value != null) {
latestVersion = parseInt(dbInitVersion.value, 10);
this.logger.debug('migration.db.init.version.detected', latestVersion);
}
}
catch (err) {
this.logger.info('migration.db.init.not.found');
}
// find init scripts to ignore (version lower than the current one)
const initSqlFolders = [];
// run through all registered packages
this.initSqlPaths.map((initSqlPath) => {