From cba9b5c72fe95a0924d378d708494d22273dfb45 Mon Sep 17 00:00:00 2001 From: Leo Singer Date: Wed, 12 Jun 2024 09:47:04 -0400 Subject: [PATCH] Close Kafka connection on SIGINT or SIGTERM, not beforeExit The beforeExit event occurs when the runtime has drained the event loop and has no more work to perform, and is about to exit. However the purpose of this signal handler is to disconnect the Kafka client which is holding some unsettled promises, which prevent beforeExit from ever being called. Instead, handle SIGINT and SIGTERM events directly. --- app/lib/kafka.server.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/app/lib/kafka.server.ts b/app/lib/kafka.server.ts index bf07a3ef6..dcaa60ba0 100644 --- a/app/lib/kafka.server.ts +++ b/app/lib/kafka.server.ts @@ -51,11 +51,13 @@ if (process.env.ARC_SANDBOX) { async () => { const producer = kafka.producer() await producer.connect() - process.once('beforeExit', async () => { - console.log('Disconnecting from Kafka') - await producer.disconnect() - console.log('Disconnected from Kafka') - }) + ;['SIGINT', 'SIGTERM'].forEach((event) => + process.once(event, async () => { + console.log('Disconnecting from Kafka') + await producer.disconnect() + console.log('Disconnected from Kafka') + }) + ) return producer }, { promise: true }