From 348888c87a91a180dbd52e67966c33e086778771 Mon Sep 17 00:00:00 2001 From: ashish-egov <137176738+ashish-egov@users.noreply.github.com> Date: Tue, 30 Jul 2024 10:16:53 +0530 Subject: [PATCH] Update Producer.ts --- .../src/server/kafka/Producer.ts | 32 ++++++++----------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/utilities/project-factory/src/server/kafka/Producer.ts b/utilities/project-factory/src/server/kafka/Producer.ts index bba3e19ae8b..10938d880fd 100644 --- a/utilities/project-factory/src/server/kafka/Producer.ts +++ b/utilities/project-factory/src/server/kafka/Producer.ts @@ -12,39 +12,33 @@ const kafkaClient = new KafkaClient({ // Creating a new Kafka producer instance using the Kafka client const producer = new Producer(kafkaClient, { partitionerType: 2 }); // Using partitioner type 2 -// Function to check broker availability +// Function to send a test message to check broker availability const checkBrokerAvailability = () => { - kafkaClient.loadMetadataForTopics([], (err, data) => { + const payloads = [ + { + topic: config.kafka.KAFKA_TEST_TOPIC, + messages: JSON.stringify({ message: 'Test message to check broker availability' }), + }, + ]; + + producer.send(payloads, (err, data) => { if (err) { if (err.message && err.message.toLowerCase().includes('broker not available')) { logger.error('Broker not available. Shutting down the service.'); shutdownGracefully(); } else { - logger.error('Error checking broker availability:', err); + logger.error('Error sending test message:', err); } } else { - logger.info('Broker is available:', data); + logger.info('Test message sent successfully:', data); } }); }; -// Event listener for 'ready' event, indicating that the client is ready to check broker availability -kafkaClient.on('ready', () => { - logger.info('Kafka client is ready'); // Log message indicating client is ready - checkBrokerAvailability(); // Check broker availability -}); - // Event listener for 'ready' event, indicating that the producer is ready to send messages producer.on('ready', () => { logger.info('Producer is ready'); // Log message indicating producer is ready - checkBrokerAvailability(); -}); - -// Event listener for 'error' event, indicating that the client encountered an error -kafkaClient.on('error', (err) => { - logger.error('Kafka client is in error state'); // Log message indicating client is in error state - console.error(err.stack || err); // Log the error stack or message - shutdownGracefully(); + checkBrokerAvailability(); // Check broker availability by sending a test message }); // Event listener for 'error' event, indicating that the producer encountered an error @@ -54,4 +48,4 @@ producer.on('error', (err) => { shutdownGracefully(); }); -export { producer }; // Exporting the producer instance for external use \ No newline at end of file +export { producer }; // Exporting the producer instance for external use