config validation & webhook retry
This commit is contained in:
@@ -25,7 +25,20 @@ const logger = winston.createLogger({
|
|||||||
]
|
]
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function validateConfig() {
|
||||||
|
const requiredKeys = ['PORT', 'SMTP_SECURE', 'WEBHOOK_URL', 'WEBHOOK_CONCURRENCY'];
|
||||||
|
for (const key of requiredKeys) {
|
||||||
|
if (!(key in config)) {
|
||||||
|
throw new Error(`Missing required configuration: ${key}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const webhookQueue = new Queue(async function (parsed, cb) {
|
const webhookQueue = new Queue(async function (parsed, cb) {
|
||||||
|
const maxRetries = 3;
|
||||||
|
let retries = 0;
|
||||||
|
|
||||||
|
const attemptWebhook = async () => {
|
||||||
try {
|
try {
|
||||||
await sendToWebhook(parsed);
|
await sendToWebhook(parsed);
|
||||||
logger.info('Successfully sent to webhook');
|
logger.info('Successfully sent to webhook');
|
||||||
@@ -38,8 +51,17 @@ const webhookQueue = new Queue(async function (parsed, cb) {
|
|||||||
data: error.response.data
|
data: error.response.data
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
if (retries < maxRetries) {
|
||||||
|
retries++;
|
||||||
|
logger.info(`Retrying webhook (attempt ${retries}/${maxRetries})`);
|
||||||
|
setTimeout(attemptWebhook, 1000 * retries);
|
||||||
|
} else {
|
||||||
cb(error);
|
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);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user