Skip to content

Commit

Permalink
Shut down if kafka error
Browse files Browse the repository at this point in the history
  • Loading branch information
ashish-egov committed Jul 4, 2024
1 parent 480d7d4 commit a9ffb5e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
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 @@ -4,7 +4,7 @@ import { getFormattedStringForDebug, logger } from '../utils/logger'; // Importi
import { producer } from './Producer'; // Importing producer from the Producer module
import { processCampaignMapping } from '../utils/campaignMappingUtils';
import { enrichAndPersistCampaignWithError } from '../utils/campaignUtils';
import { throwError } from '../utils/genericUtils';
import { shutdownGracefully, throwError } from '../utils/genericUtils';



Expand Down Expand Up @@ -58,6 +58,7 @@ export function listener() {
// Set up error event handlers
consumerGroup.on('error', (err) => {
console.error(`Consumer Error: ${err}`);
shutdownGracefully();
});

consumerGroup.on('offsetOutOfRange', (err) => {
Expand Down
2 changes: 2 additions & 0 deletions utilities/project-factory/src/server/kafka/Producer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import config from '../config'; // Importing configuration settings
import { Producer, KafkaClient } from 'kafka-node'; // Importing Producer and KafkaClient from 'kafka-node' library
import { logger } from "../utils/logger";
import { shutdownGracefully } from '../utils/genericUtils';

// Creating a new Kafka client instance using the configured Kafka broker host
const kafkaClient = new KafkaClient({
Expand All @@ -20,6 +21,7 @@ producer.on('ready', () => {
producer.on('error', (err) => {
logger.error('Producer is in error state'); // Log message indicating producer is in error state
console.error(err.stack || err); // Log the error stack or message
shutdownGracefully();
});

export { producer }; // Exporting the producer instance for external use
9 changes: 8 additions & 1 deletion utilities/project-factory/src/server/utils/genericUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ const throwErrorViaRequest = (message: any = "Internal Server Error") => {
}
};

function shutdownGracefully() {
logger.info('Shutting down gracefully...');
// Perform any cleanup tasks here, like closing database connections
process.exit(1); // Exit with a non-zero code to indicate an error
}

function capitalizeFirstLetter(str: string | undefined) {
if (!str) return str;
return str.charAt(0).toUpperCase() + str.slice(1);
Expand Down Expand Up @@ -1136,7 +1142,8 @@ export {
changeFirstRowColumnColour,
getConfigurableColumnHeadersFromSchemaForTargetSheet,
createBoundaryDataMainSheet,
getMdmsDataBasedOnCampaignType
getMdmsDataBasedOnCampaignType,
shutdownGracefully
};


0 comments on commit a9ffb5e

Please sign in to comment.