2022-08-18 16:45:43 -04:00
|
|
|
const errorTypes = ['unhandledRejection', 'uncaughtException']
|
|
|
|
const signalTraps = ['SIGTERM', 'SIGINT', 'SIGUSR2']
|
|
|
|
|
2022-08-20 19:15:59 -04:00
|
|
|
const disconnectAll = async (consumers, clusters) => {
|
|
|
|
await Promise.all([
|
|
|
|
...Object.values(clusters).map(async cluster => cluster.admin.disconnect()),
|
|
|
|
...Array.from(consumers.values()).map(async consumer => consumer.disconnect())
|
|
|
|
])
|
2022-08-18 16:45:43 -04:00
|
|
|
}
|
|
|
|
|
2022-08-20 19:15:59 -04:00
|
|
|
const buildEscape = (consumers, clusters) => {
|
|
|
|
const disconnect = async () => disconnectAll(consumers, clusters)
|
|
|
|
errorTypes.map(type => {
|
|
|
|
process.on(type, async e => {
|
|
|
|
try {
|
|
|
|
console.log(`process.on ${type}`)
|
|
|
|
console.error(e)
|
|
|
|
await disconnect()
|
|
|
|
process.exit(0)
|
|
|
|
} catch (_) {
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
})
|
2022-08-18 16:45:43 -04:00
|
|
|
})
|
|
|
|
|
2022-08-20 19:15:59 -04:00
|
|
|
signalTraps.map(type => {
|
|
|
|
process.once(type, async () => {
|
|
|
|
try {
|
|
|
|
console.log('signalTrap: ' + type)
|
|
|
|
await disconnect()
|
|
|
|
} finally {
|
|
|
|
process.kill(process.pid, type)
|
|
|
|
}
|
|
|
|
})
|
2022-08-18 16:45:43 -04:00
|
|
|
})
|
2022-08-20 19:15:59 -04:00
|
|
|
}
|
2022-08-18 16:45:43 -04:00
|
|
|
|
2022-08-20 19:15:59 -04:00
|
|
|
module.exports = {
|
|
|
|
buildEscape
|
2022-08-18 16:45:43 -04:00
|
|
|
}
|