Skip to content

Commit

Permalink
Update index.ts (#1129)
Browse files Browse the repository at this point in the history
* Update index.ts

* Update Listener.ts

* Update Producer.ts
  • Loading branch information
ashish-egov authored Jul 24, 2024
1 parent ad39c8e commit ec43c6a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
3 changes: 2 additions & 1 deletion utilities/project-factory/src/server/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const getDBSchemaName = (dbSchema = "") => {
}
// Configuration object containing various environment variables
const config = {
isCallGenerateWhenDeliveryConditionsDiffer:true,
isCallGenerateWhenDeliveryConditionsDiffer: true,
enableDynamicTargetTemplate: true,
prefixForMicroplanCampaigns: "MP",
excludeHierarchyTypeFromBoundaryCodes: false,
Expand Down Expand Up @@ -62,6 +62,7 @@ const config = {
KAFKA_CREATE_GENERATED_RESOURCE_DETAILS_TOPIC: process.env.KAFKA_CREATE_GENERATED_RESOURCE_DETAILS_TOPIC || "create-generated-resource-details",
KAFKA_SAVE_PROCESS_TRACK_TOPIC: process.env.KAFKA_SAVE_PROCESS_TRACK_TOPIC || "save-process-track",
KAFKA_UPDATE_PROCESS_TRACK_TOPIC: process.env.KAFKA_UPDATE_PROCESS_TRACK_TOPIC || "update-process-track",
KAFKA_TEST_TOPIC: "test-topic-project-factory",
},

// Database configuration
Expand Down
3 changes: 2 additions & 1 deletion utilities/project-factory/src/server/kafka/Listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ const kafkaConfig: ConsumerGroupOptions = {
// Topic Names
const topicNames = [
config.kafka.KAFKA_START_CAMPAIGN_MAPPING_TOPIC,
config.kafka.KAFKA_PROCESS_CAMPAIGN_MAPPING_TOPIC
config.kafka.KAFKA_PROCESS_CAMPAIGN_MAPPING_TOPIC,
config.kafka.KAFKA_TEST_TOPIC
];

// Consumer Group Initialization
Expand Down
24 changes: 24 additions & 0 deletions utilities/project-factory/src/server/kafka/Producer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +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 send a test message to check broker availability
const checkBrokerAvailability = () => {
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 sending test message:', err);
}
} else {
logger.info('Test message sent successfully:', data);
}
});
};

// 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(); // Check broker availability by sending a test message
});

// Event listener for 'error' event, indicating that the producer encountered an error
Expand Down

0 comments on commit ec43c6a

Please sign in to comment.