Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion packages/node/bin/streamr-node.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#!/usr/bin/env node
import { program } from 'commander'
import pkg from '../package.json'

import { Logger } from '@streamr/utils'
import { createBroker } from '../src/broker'
import { readConfigAndMigrateIfNeeded } from '../src/config/migration'
import { overrideConfigToEnvVarsIfGiven } from '../src/config/config'

const logger = new Logger(module)

program
.version(pkg.version)
.name('streamr-node')
Expand All @@ -17,6 +19,28 @@ program
const config = readConfigAndMigrateIfNeeded(configFile)
overrideConfigToEnvVarsIfGiven(config)
const broker = await createBroker(config)

// Set up graceful shutdown handlers
const shutdown = async (exitCode: number) => {
await broker.stop()
process.exit(exitCode)
}

const exitEvents = ['SIGINT', 'SIGTERM', 'SIGUSR1', 'SIGUSR2']
exitEvents.forEach((event) => {
process.on(event, () => shutdown(0))
})

process.on('uncaughtException', (err) => {
logger.fatal('Encountered uncaughtException', { err })
shutdown(1)
})

process.on('unhandledRejection', (err) => {
logger.fatal('Encountered unhandledRejection', { err })
shutdown(1)
})

if (!program.opts().test) {
await broker.start()
} else {
Expand Down
10 changes: 0 additions & 10 deletions packages/node/src/broker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,3 @@ export const createBroker = async (configWithoutDefaults: Config): Promise<Broke
}
}
}

process.on('uncaughtException', (err) => {
logger.fatal('Encountered uncaughtException', { err })
process.exit(1)
})

process.on('unhandledRejection', (err) => {
logger.fatal('Encountered unhandledRejection', { err })
process.exit(1)
})
11 changes: 0 additions & 11 deletions packages/trackerless-network/src/NetworkStack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,6 @@ const stopInstances = async () => {
const clonedInstances = [...instances]
await Promise.all(clonedInstances.map((instance) => instance.stop()))
}
const EXIT_EVENTS = [`exit`, `SIGINT`, `SIGUSR1`, `SIGUSR2`, `uncaughtException`, `unhandledRejection`, `SIGTERM`]
EXIT_EVENTS.forEach((event) => {
process.on(event, async (eventArg) => {
const isError = (event === 'uncaughtException') || (event === 'unhandledRejection')
if (isError) {
logger.error(`exit event: ${event}`, eventArg)
}
await stopInstances()
process.exit(isError ? 1 : 0)
})
})
declare let window: any
if (typeof window === 'object') {
window.addEventListener('unload', async () => {
Expand Down