From cdfd16d89ff0027b0211921590b32eee647587d8 Mon Sep 17 00:00:00 2001 From: "vicky :)" <60366641+vicky-g@users.noreply.github.com> Date: Tue, 21 Jun 2022 19:31:22 -0400 Subject: [PATCH] [CON-215] - Make bl init asynchronous but exit if err (#3231) * make bl init asynchronous but exit if err * update init err msg * remove comment * add comment + simplify err msg * revert non-relevant changes * add time logs + thrown err update --- creator-node/src/blacklistManager.js | 2 +- creator-node/src/index.ts | 3 +++ creator-node/src/serviceRegistry.js | 35 ++++++++++++++++++++++++---- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/creator-node/src/blacklistManager.js b/creator-node/src/blacklistManager.js index 708dd8a233e..b1dfde76520 100644 --- a/creator-node/src/blacklistManager.js +++ b/creator-node/src/blacklistManager.js @@ -36,7 +36,7 @@ class BlacklistManager { }) this.initialized = true } catch (e) { - throw new Error(`BLACKLIST ERROR ${e}`) + throw new Error(`Could not init BlacklistManager: ${e.message}`) } } diff --git a/creator-node/src/index.ts b/creator-node/src/index.ts index f9a0e9df385..8c85d742253 100644 --- a/creator-node/src/index.ts +++ b/creator-node/src/index.ts @@ -115,6 +115,9 @@ const startApp = async () => { const appInfo = initializeApp(getPort(), serviceRegistry) logger.info('Initialized app and server') + // Initialize services that do not require the server, but do not need to be awaited. + serviceRegistry.initServicesAsynchronously() + // Some Services cannot start until server is up. Start them now // No need to await on this as this process can take a while and can run in the background serviceRegistry.initServicesThatRequireServer(appInfo.app) diff --git a/creator-node/src/serviceRegistry.js b/creator-node/src/serviceRegistry.js index db2e436edbc..6d099678f16 100644 --- a/creator-node/src/serviceRegistry.js +++ b/creator-node/src/serviceRegistry.js @@ -4,7 +4,7 @@ const BlacklistManager = require('./blacklistManager') const { SnapbackSM } = require('./snapbackSM/snapbackSM') const config = require('./config') const URSMRegistrationManager = require('./services/URSMRegistrationManager') -const { logger } = require('./logging') +const { logger, getStartTime, logInfoWithDuration } = require('./logging') const utils = require('./utils') const { createBullBoard } = require('@bull-board/api') const { BullAdapter } = require('@bull-board/api/bullAdapter') @@ -56,6 +56,7 @@ class ServiceRegistry { this.trustedNotifierManager = null this.servicesInitialized = false + this.asynchronousServicesInitialized = false this.servicesThatRequireServerInitialized = false } @@ -63,8 +64,6 @@ class ServiceRegistry { * Configure all services */ async initServices() { - await this.blacklistManager.init() - // init libs this.libs = await this._initAudiusLibs() @@ -82,6 +81,28 @@ class ServiceRegistry { this.servicesInitialized = true } + /** + * These services do not need to be awaited and do not require the server. + */ + async initServicesAsynchronously() { + const start = getStartTime() + + // Initialize BlacklistManager. If error occurs, do not continue with app start up. + try { + await this.blacklistManager.init() + } catch (e) { + this.logError(e.message) + process.exit(1) + } + + this.asynchronousServicesInitialized = true + + logInfoWithDuration( + { logger, startTime: start }, + 'ServiceRegistry || Initialized asynchronous services' + ) + } + /** * Initializes the blacklistManager if it is not already initialized, and then returns it * @returns initialized blacklistManager instance @@ -142,6 +163,8 @@ class ServiceRegistry { * - create bull queue monitoring dashboard, which needs other server-dependent services to be running */ async initServicesThatRequireServer(app) { + const start = getStartTime() + // Cannot progress without recovering spID from node's record on L1 ServiceProviderFactory contract // Retries indefinitely await this._recoverNodeL1Identity() @@ -183,7 +206,11 @@ class ServiceRegistry { } this.servicesThatRequireServerInitialized = true - this.logInfo(`All services that require server successfully initialized!`) + + logInfoWithDuration( + { logger, startTime: start }, + 'ServiceRegistry || Initialized services that require server' + ) } logInfo(msg) {