Skip to content

Commit

Permalink
fix(core): small changes to botpress fatal errors
Browse files Browse the repository at this point in the history
  • Loading branch information
allardy committed Oct 9, 2019
1 parent 30a9f95 commit 10dc57c
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 11 deletions.
4 changes: 3 additions & 1 deletion modules/nlu/src/backend/index.ts
Expand Up @@ -25,7 +25,9 @@ export const initializeLangServer = async (bp: typeof sdk) => {
} catch (e) {
if (e.failure && e.failure.code === 'ECONNREFUSED') {
bp.logger.error(`Language server can't be reached at adress ${e.failure.address}:${e.failure.port}`)
process.exit()
if (!process.IS_FAILSAFE) {
process.exit()
}
}
throw e
}
Expand Down
16 changes: 9 additions & 7 deletions src/bp/bootstrap.ts
Expand Up @@ -86,7 +86,10 @@ async function start() {
Please make sure that Botpress has the right to access this folder or change the folder path by providing the 'APP_DATA_PATH' env variable.
This is a fatal error, process will exit.`
)
process.exit(1)

if (!process.IS_FAILSAFE) {
process.exit(1)
}
}
}

Expand Down Expand Up @@ -123,16 +126,15 @@ This is a fatal error, process will exit.`
logger.info(`Using ${chalk.bold(modules.length.toString())} modules` + modulesLog)

for (const err of loadingErrors) {
logger.attachError(err).error('Error starting Botpress')
}

if (loadingErrors.length) {
process.exit(1)
logger.attachError(err).error('Error while loading some modules, they will be disabled')
}

await Botpress.start({ modules }).catch(err => {
logger.attachError(err).error('Error starting Botpress')
process.exit(1)

if (!process.IS_FAILSAFE) {
process.exit(1)
}
})

logger.info(`Botpress is listening at: ${process.LOCAL_URL}`)
Expand Down
13 changes: 11 additions & 2 deletions src/bp/core/services/migration/index.ts
Expand Up @@ -69,7 +69,13 @@ export class MigrationService {
await this.logger.error(
`Botpress needs to migrate your data. Please make a copy of your data, then start it with "./bp --auto-migrate"`
)
process.exit(0)

// When failsafe is enabled, simply stop executing migrations
if (!process.IS_FAILSAFE) {
process.exit(0)
} else {
return
}
}

await this.executeMigrations(missingMigrations)
Expand Down Expand Up @@ -154,7 +160,10 @@ export class MigrationService {
await this.logger.error(
`Some steps failed to complete. Please fix errors manually, then restart Botpress so the update process may finish.`
)
process.exit(0)

if (!process.IS_FAILSAFE) {
process.exit(0)
}
}

await this.updateAllVersions()
Expand Down
5 changes: 4 additions & 1 deletion src/bp/index.ts
Expand Up @@ -43,6 +43,7 @@ if (process.env.APP_DATA_PATH) {
process.APP_DATA_PATH = getAppDataPath()
}

process.IS_FAILSAFE = yn(process.env.BP_FAILSAFE)
process.BOTPRESS_EVENTS = new EventEmitter()
process.BOTPRESS_EVENTS.setMaxListeners(1000)

Expand All @@ -59,7 +60,9 @@ process.on('unhandledRejection', err => {

process.on('uncaughtException', err => {
global.printErrorDefault(err)
process.exit(1)
if (!process.IS_FAILSAFE) {
process.exit(1)
}
})

try {
Expand Down
7 changes: 7 additions & 0 deletions src/typings/global.d.ts
Expand Up @@ -44,6 +44,7 @@ declare namespace NodeJS {
distro: OSDistribution
BOTPRESS_EVENTS: EventEmitter
AUTO_MIGRATE: boolean
IS_FAILSAFE: boolean
}
}

Expand Down Expand Up @@ -141,6 +142,12 @@ declare type BotpressEnvironementVariables = {
* @default false
*/
readonly BP_DISABLE_SERVER_CONFIG?: boolean

/**
* Prevents Botpress from closing cleanly when an error is encountered.
* This only affects fatal errors, it will not affect business rules checks (eg: licensing)
*/
readonly BP_FAILSAFE?: boolean
}

interface IDebug {
Expand Down

0 comments on commit 10dc57c

Please sign in to comment.