Skip to content

Commit

Permalink
[CON-215] - Make bl init asynchronous but exit if err (#3231)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
vicky-g authored and raymondjacobson committed Jun 25, 2022
1 parent cc02841 commit cdfd16d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
2 changes: 1 addition & 1 deletion creator-node/src/blacklistManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
}
}

Expand Down
3 changes: 3 additions & 0 deletions creator-node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
35 changes: 31 additions & 4 deletions creator-node/src/serviceRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -56,15 +56,14 @@ class ServiceRegistry {
this.trustedNotifierManager = null

this.servicesInitialized = false
this.asynchronousServicesInitialized = false
this.servicesThatRequireServerInitialized = false
}

/**
* Configure all services
*/
async initServices() {
await this.blacklistManager.init()

// init libs
this.libs = await this._initAudiusLibs()

Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit cdfd16d

Please sign in to comment.