Skip to content

Commit

Permalink
feat: better bootstrap process + output RAM usage
Browse files Browse the repository at this point in the history
  • Loading branch information
evereq committed Jun 23, 2019
1 parent ae0f734 commit eb6c1af
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 43 deletions.
2 changes: 1 addition & 1 deletion backend/api/src/@pyro/io/router/handler.ts
Expand Up @@ -17,7 +17,7 @@ export class RouterHandler {
this.listeners = getListeners(router);
}

listen(): void {
async listen(): Promise<void> {
this.log.info(`Starting router listening`, {
routerName: this.routerName,
listeners: this.listeners
Expand Down
10 changes: 5 additions & 5 deletions backend/api/src/@pyro/io/routers-manager.ts
Expand Up @@ -15,18 +15,18 @@ export class RoutersManager implements IRoutersManager {

protected io: SocketIO.Server;

startListening(io: SocketIO.Server) {
async startListening(io: SocketIO.Server) {
this.io = io;

this.routers.forEach((router) => {
this.startRouterListening(router);
this.routers.forEach(async (router) => {
await this.startRouterListening(router);
});
}

private startRouterListening(router: IRouter) {
private async startRouterListening(router: IRouter) {
try {
const routerHandler = new RouterHandler(this.io, router, this.log);
routerHandler.listen();
await routerHandler.listen();
} catch (err) {
this.log.fatal("Couldn't start router listening!", { err });
}
Expand Down
15 changes: 8 additions & 7 deletions backend/api/src/main.ts
Expand Up @@ -42,7 +42,7 @@ process.on('uncaughtException', (err) => {
try {
console.error("Can't write to log!!!!!!");
console.error(logWritingErr);
} catch (consoleWritingError) { }
} catch (consoleWritingError) {}
}

console.error(err);
Expand All @@ -55,7 +55,7 @@ process.on('unhandledRejection', (err, promise) => {
try {
console.error("Can't write to log!!!!!!");
console.error(logWritingErr);
} catch (consoleWritingError) { }
} catch (consoleWritingError) {}
}

console.error(err);
Expand All @@ -68,10 +68,11 @@ process.on('unhandledRejection', (err, promise) => {
await ServicesApp.CreateTypeORMConnection();

const app = servicesContainer.get<ServicesApp>(ServicesApp);
await app.start();

// load NestJS modules dynamically, because needs all services to be initialized before
const bootstrapNest = await require('./nest-bootstrap').bootstrapNest;
// bootstrap NestJS modules/controllers/DI/etc
bootstrapNest();
await app.start(async () => {
// load NestJS modules dynamically, because needs all services to be initialized before
const bootstrapNest = await require('./nest-bootstrap').bootstrapNest;
// bootstrap NestJS modules/controllers/DI/etc
bootstrapNest();
});
})();
1 change: 1 addition & 0 deletions backend/api/src/nest-bootstrap.ts
Expand Up @@ -44,4 +44,5 @@ export async function bootstrapNest(): Promise<void> {
}

log.info(`Swagger UI available at http://localhost:${port}/api`);
console.log(`Swagger UI available at http://localhost:${port}/api`);
}
80 changes: 50 additions & 30 deletions backend/api/src/services/services.app.ts
Expand Up @@ -60,6 +60,8 @@ export class ServicesApp {
// TODO: put to config
private static _connectTimeoutMS: number = 40000;

private callback: () => void;

constructor(
@multiInject(ServiceSymbol)
protected services: IService[],
Expand All @@ -77,13 +79,14 @@ export class ServicesApp {
const maxSockets = _configService.Env.MAX_SOCKETS;

// see https://webapplog.com/seven-things-you-should-stop-doing-with-node-js
https.globalAgent.maxSockets = maxSockets;
http.globalAgent.maxSockets = maxSockets;
https.globalAgent.maxSockets = maxSockets;

this._configDB();
}

async start() {
async start(callback: () => void) {
this.callback = callback;
await this._connectDB();
}

Expand All @@ -104,6 +107,8 @@ export class ServicesApp {
}

static async CreateTypeORMConnection() {
const typeORMLog = createEverLogger({ name: 'TypeORM' });

// list of entities for which Repositories will be greated in TypeORM
const entities = ServicesApp.getEntities();

Expand All @@ -126,6 +131,10 @@ export class ServicesApp {
`TypeORM DB connection created. DB connected: ${conn.isConnected}`
);

typeORMLog.info(
`TypeORM DB connection created. DB connected: ${conn.isConnected}`
);

return conn;
}

Expand Down Expand Up @@ -175,36 +184,50 @@ export class ServicesApp {

private async _connectDB() {
try {
mongoose.connect(
const connectionOptions: mongoose.ConnectionOptions = {
useCreateIndex: true,
useNewUrlParser: true,
autoReconnect: true,
useFindAndModify: false,
reconnectTries: Number.MAX_VALUE,
poolSize: ServicesApp._poolSize,
connectTimeoutMS: ServicesApp._connectTimeoutMS
};

const mongoConnect: mongoose.Mongoose = await mongoose.connect(
env.DB_URI,
{
useCreateIndex: true,
useNewUrlParser: true,
autoReconnect: true,
useFindAndModify: false,
reconnectTries: Number.MAX_VALUE,
poolSize: ServicesApp._poolSize,
connectTimeoutMS: ServicesApp._connectTimeoutMS
} as any,
(err) => {
if (err != null) {
this.log.error(err);
}
}
connectionOptions
);

this.log.info('Trying to connect to DB ' + this.db_server);
} catch (err) {
this.log.error(err, 'Sever initialization failed!');
this.log.error(
err,
'Sever initialization failed! Cannot connect to DB'
);
}
}

private async _onDBConnect() {
// that's important to see even if logs disabled, do not remove!
console.log('Connected to DB');

this.log.info({ db: this.db_server }, 'Connected to DB');

await this._registerModels();
await this._registerEntityAdministrator();
this._passportSetup();
await this._startExpress();
this._startSocketIO();
await this._startSocketIO();

// execute callback defined at main.ts
await this.callback();

// let's report RAM usage after all is bootstrapped
await this.reportMemoryUsage();
}

private async reportMemoryUsage() {
console.log('Memory usage: ');
console.log(process.memoryUsage());
}

/**
Expand Down Expand Up @@ -274,11 +297,6 @@ export class ServicesApp {
}

private async _startExpress() {
// that's important to see even if logs disabled, do not remove!
console.log('Connected to DB');

this.log.info({ db: this.db_server }, 'Connected to DB');

this.expressApp = express();

const hbs = exphbs.create({
Expand Down Expand Up @@ -328,9 +346,11 @@ export class ServicesApp {
// TODO: add to settings file
// set connections timeouts to 30 minutes (for long running requests)
const timeout = 30 * 60 * 1000;

if (this.httpsServer) {
this.httpsServer.setTimeout(timeout);
}

this.httpServer.setTimeout(timeout);

this.expressApp.set('httpsPort', env.HTTPSPORT);
Expand Down Expand Up @@ -464,7 +484,7 @@ export class ServicesApp {
try {
this.log.info('Generating SSL Certificates for HTTPS');

let { success } = await this._createCertificateAsync(
const { success } = await this._createCertificateAsync(
httpsCertPath,
httpsKeyPath
);
Expand Down Expand Up @@ -525,12 +545,12 @@ export class ServicesApp {
});
}

private _startSocketIO() {
private async _startSocketIO() {
const ioHttps = socketIO(this.httpsServer);
const ioHttp = socketIO(this.httpServer);

this.routersManager.startListening(ioHttps);
this.routersManager.startListening(ioHttp);
await this.routersManager.startListening(ioHttps);
await this.routersManager.startListening(ioHttp);
}

private _setupStaticRoutes() {
Expand Down

0 comments on commit eb6c1af

Please sign in to comment.