config validation & webhook retry

This commit is contained in:
Martin
2024-09-22 15:57:52 +10:00
parent fa941a1272
commit 9a71c58a90
+43 -13
View File
@@ -25,21 +25,43 @@ const logger = winston.createLogger({
] ]
}); });
const webhookQueue = new Queue(async function (parsed, cb) { function validateConfig() {
try { const requiredKeys = ['PORT', 'SMTP_SECURE', 'WEBHOOK_URL', 'WEBHOOK_CONCURRENCY'];
await sendToWebhook(parsed); for (const key of requiredKeys) {
logger.info('Successfully sent to webhook'); if (!(key in config)) {
cb(null); throw new Error(`Missing required configuration: ${key}`);
} catch (error) {
logger.error('Webhook error:', { message: error.message, stack: error.stack });
if (error.response) {
logger.error('Webhook response error:', {
status: error.response.status,
data: error.response.data
});
} }
cb(error);
} }
}
const webhookQueue = new Queue(async function (parsed, cb) {
const maxRetries = 3;
let retries = 0;
const attemptWebhook = async () => {
try {
await sendToWebhook(parsed);
logger.info('Successfully sent to webhook');
cb(null);
} catch (error) {
logger.error('Webhook error:', { message: error.message, stack: error.stack });
if (error.response) {
logger.error('Webhook response error:', {
status: error.response.status,
data: error.response.data
});
}
if (retries < maxRetries) {
retries++;
logger.info(`Retrying webhook (attempt ${retries}/${maxRetries})`);
setTimeout(attemptWebhook, 1000 * retries);
} else {
cb(error);
}
}
};
attemptWebhook();
}, { concurrent: config.WEBHOOK_CONCURRENCY || 5 }); }, { concurrent: config.WEBHOOK_CONCURRENCY || 5 });
const server = new SMTPServer({ const server = new SMTPServer({
@@ -95,3 +117,11 @@ process.on('SIGTERM', () => {
process.on('SIGINT', () => { process.on('SIGINT', () => {
gracefulShutdown('SIGINT signal received'); gracefulShutdown('SIGINT signal received');
}); });
// Add configuration validation at startup
try {
validateConfig();
} catch (error) {
logger.error('Configuration error:', { message: error.message });
process.exit(1);
}