From 5776f5b39bc0ba1d1fe535375a91e6383683a37a Mon Sep 17 00:00:00 2001 From: Sergey Matvienko Date: Thu, 27 Oct 2022 15:22:45 +0300 Subject: [PATCH] js-executor - improved lifecycle events and added kill for crash event --- msa/js-executor/queue/kafkaTemplate.ts | 4 ++++ msa/js-executor/server.ts | 22 +++++++++++++--------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/msa/js-executor/queue/kafkaTemplate.ts b/msa/js-executor/queue/kafkaTemplate.ts index 7c34d99889..6cb8281837 100644 --- a/msa/js-executor/queue/kafkaTemplate.ts +++ b/msa/js-executor/queue/kafkaTemplate.ts @@ -30,6 +30,8 @@ import { TopicMessages } from 'kafkajs'; +import process, { kill, exit } from 'process'; + export class KafkaTemplate implements IQueue { private logger = _logger(`kafkaTemplate`); @@ -122,6 +124,7 @@ export class KafkaTemplate implements IQueue { this.logger.error(`Got consumer CRASH event, should restart: ${e.payload.restart}`); if (!e.payload.restart) { this.logger.error('Going to exit due to not retryable error!'); + kill(process.pid, 'SIGTERM'); //sending signal to myself process to trigger the handler await this.destroy(); } }); @@ -253,6 +256,7 @@ export class KafkaTemplate implements IQueue { } } this.logger.info('Kafka resources stopped.'); + exit(0); //same as in version before } private async disconnectProducer(): Promise { diff --git a/msa/js-executor/server.ts b/msa/js-executor/server.ts index 035bf77183..a64dcc2c98 100644 --- a/msa/js-executor/server.ts +++ b/msa/js-executor/server.ts @@ -81,15 +81,19 @@ process.on('exit', (code: number) => { async function exit(status: number) { logger.info('Exiting with status: %d ...', status); - if (httpServer) { - const _httpServer = httpServer; - httpServer = null; - await _httpServer.stop(); - } - if (queues) { - const _queues = queues; - queues = null; - await _queues.destroy(); + try { + if (httpServer) { + const _httpServer = httpServer; + httpServer = null; + await _httpServer.stop(); + } + if (queues) { + const _queues = queues; + queues = null; + await _queues.destroy(); + } + } catch (e) { + logger.error('Error on exit'); } process.exit(status); }