Files
JogyoGaori/src/handlers/processHandlers.js
2025-08-07 01:18:49 +09:00

31 lines
1.1 KiB
JavaScript

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 };