can we do anything to increase the performance of this 
const express = require("express");
const app = express();
require("dotenv").config();
const createLogger = require("./src/services/Logger");
const logger = createLogger("medics-notification-server");
const LockManager = require("node-locksmith");
const SQLConnectionManager = require("./src/services/SQLConnectionManager");
const KafkaService = require("./src/services/KafkaService");
const config = require("./src/config/config.json");
const kafkaConfig = config.kafka;
const kafkaService = new KafkaService(kafkaConfig, logger);

const SQLService = new SQLConnectionManager(
	logger,
	require("./src/config/config.json"),
);

const POLL_INTERVAL = 2000; // Poll every 2 seconds

const lockManager = new LockManager({
	fileName: "medics-event-publisher.lock",
});

/**
 * Initialize termination event handlers for graceful application shutdown.
 */
lockManager.initializeTerminationHandlers();

let isProcessing = false;

/**
 * Process pending events and publish them to Kafka.
 * @param {Object} connection - Database connection object.
 */
async function processPendingEvents(connection) {
	if (isProcessing) {
		return;
	}

	isProcessing = true;

	try {
		const pendingRecords = await fetchPendingEvents(connection);
		// Inside the processPendingEvents function
		for (const record of pendingRecords) {
			const { id, topic, message } = record;
			try {
				await kafkaService.publishMessageWithRetry(
					topic,
					JSON.stringify(message),
				);
				await markEventAsDelivered(id, connection, true); // Mark as delivered
			} catch (error) {
				logger.error(`Error publishing message to Kafka: ${error.message}`);
				await markEventAsDelivered(id, connection, false, error.message); // Mark as failed
			}
		}
	} catch (error) {
		logger.error(
			"Error during processing pending events:",
			error.message ? error.message : error,
		);
	} finally {
		isProcessing = false;
		// Schedule the next execution after a specific interval
		setTimeout(() => processPendingEvents(connection), POLL_INTERVAL);
	}
}

/**
 * Fetch pending events from the database.
 * @param {Object} connection - Database connection object.
 * @returns {Promise<Array>} - Array of pending events.
 */
async function fetchPendingEvents(connection) {
	const sql = "SELECT * FROM notification_queue WHERE status = 'PENDING'";
	return await SQLService.executeQuery(sql, connection);
}

/**
 * Mark an event as delivered or update retry information in the database.
 * @param {number} id - Event ID.
 * @param {Object} connection - Database connection object.
 * @param {boolean} success - Indicates whether the event was delivered successfully.
 * @param {string|null} errorMessage - Error message in case of failure.
 * @param {number} maxRetries - Maximum number of retry attempts allowed.
 * @returns {Promise<void>} - A promise indicating the completion of the operation.
 */
async function markEventAsDelivered(
	id,
	connection,
	success = true,
	errorMessage = null,
	maxRetries = 3,
) {
	let sql;

	if (success) {
		sql = `UPDATE notification_queue SET status = 'DELIVERED', delivered_at = NOW() WHERE id = ${id}`;
	} else {
		sql = `
            UPDATE notification_queue 
            SET 
                retry_count = retry_count + 1, 
                error_message = '${errorMessage}', 
                status = CASE WHEN retry_count + 1 >= ${maxRetries} THEN 'FAILED' ELSE status END
            WHERE id = ${id}`;
	}

	await SQLService.executeQuery(sql, connection);
}

/**
 * Initialize the application by creating worker threads.
 */
async function initializeApp() {
	try {
		// Ensure the application is not already running
		await lockManager.checkLock();
		// Create a lock to prevent multiple instances of the application
		await lockManager.createLock(Infinity, 3);

		// Get a connection from the database pool
		const connection = await SQLService.getConnection();

		app.get(["/hc", "/healthcheck"], (req, res) => {
			res.status(200).send("OK");
		});

		app.listen(config.serverPort, () => {
			console.log(`Server running at http://localhost:${config.serverPort}`);
		});

		processPendingEvents(connection);

		// Handle unhandled promise rejections and log them
		process.on("unhandledRejection", (error, promise) => {
			logger.error("Unhandled Promise Rejection:", error);
		});

		// Graceful shutdown on SIGTERM
		process.on("SIGTERM", () => {
			logger.info("Received SIGTERM. Shutting down gracefully.");
			process.exit(0);
		});
	} catch (error) {
		// Log initialization errors and exit the process with an error code
		logger.error("Initialization error:", error);
		process.exit(1);
	}
}

// Initialize the application
initializeApp();