const logger = require('../../modules/colorfulLogger'); const { handleFatalError } = require('../../utils/errorHandler'); const { saveData } = require('../data/dataManager'); function setupProcessHandlers(client) { async function gracefulShutdown(signal) { logger.info(`${signal} signal received. Shutting down gracefully...`); try { await client.destroy(); await saveData('history'); process.exit(0); } catch (e) { logger.error(`Error during ${signal} shutdown: ${e.message}`); process.exit(1); } } process.on('SIGINT', () => gracefulShutdown('SIGINT')); process.on('SIGTERM', () => gracefulShutdown('SIGTERM')); process.on('uncaughtException', (error) => { handleFatalError(error, 'Uncaught Exception', client, () => saveData('history')); }); process.on('unhandledRejection', (reason) => { const error = reason instanceof Error ? reason : new Error(String(reason)); handleFatalError(error, 'Unhandled Rejection', client, () => saveData('history')); }); } module.exports = { setupProcessHandlers };