const errorTypes = ['unhandledRejection', 'uncaughtException'] const signalTraps = ['SIGTERM', 'SIGINT', 'SIGUSR2'] const disconnectAll = async () => { await Promise.all(Object.values(clusters).map(async cluster => cluster.admin.disconnect())) await Promise.all( Array.from(consumers.values()) .map(async consumer => consumer.disconnect())) } errorTypes.map(type => { process.on(type, async e => { try { console.log(`process.on ${type}`) console.error(e) await disconnectAll() process.exit(0) } catch (_) { process.exit(1) } }) }) signalTraps.map(type => { process.once(type, async () => { try { console.log('signalTrap: ' + type) await disconnectAll() } finally { process.kill(process.pid, type) } }) }) let consumers let clusters module.exports = (consumerMap, clusterObject) => { consumers = consumerMap clusters = clusterObject }