feat(webhooks): add script to retry failed webhooks and log results

Introduce a new script to process and retry failed webhooks stored in a designated directory. The script reads JSON files containing webhook payloads and error information, attempts to resend the payloads, and logs the success or failure of each retry. Additionally, it creates a processed directory to organize successfully retried webhooks. This enhances error handling and recovery for webhook failures.
This commit is contained in:
sebseb7
2025-08-04 16:43:56 +02:00
parent e5f50d3db3
commit f1ef4a167e
3 changed files with 130 additions and 0 deletions

33
x.js
View File

@@ -229,6 +229,12 @@ if (!fs.existsSync(logsDir)) {
fs.mkdirSync(logsDir);
}
// Create failed webhooks directory if it doesn't exist
const failedWebhooksDir = path.join(__dirname, 'failed_webhooks');
if (!fs.existsSync(failedWebhooksDir)) {
fs.mkdirSync(failedWebhooksDir);
}
// Logger function
function logMessage(message, type = 'info') {
const timestamp = new Date().toISOString();
@@ -241,6 +247,29 @@ function logMessage(message, type = 'info') {
fs.appendFileSync(logFile, logEntry + '\n');
}
// Function to dump failed webhooks for retry
function dumpFailedWebhook(payload, error, reason = 'unknown') {
try {
const timestamp = new Date().toISOString();
const filename = `failed_webhook_${timestamp.replace(/[:.]/g, '-')}_${payload?.repository?.name || 'unknown'}.json`;
const filepath = path.join(failedWebhooksDir, filename);
const failedWebhookData = {
timestamp,
reason,
error: error?.message || String(error),
payload
};
fs.writeFileSync(filepath, JSON.stringify(failedWebhookData, null, 2));
logMessage(`Failed webhook dumped to: ${filename}`, 'info');
return filepath;
} catch (dumpError) {
logMessage(`Failed to dump webhook: ${dumpError.message}`, 'error');
return null;
}
}
// Telegram bot function
async function sendTelegramMessage(message) {
const botToken = process.env.TELEGRAM_BOT_TOKEN;
@@ -267,6 +296,8 @@ async function sendTelegramMessage(message) {
if (error.response) {
logMessage(`Telegram API error: ${JSON.stringify(error.response.data)}`, 'error');
}
// Re-throw the error so it can be caught by the calling code
throw error;
}
}
@@ -393,6 +424,7 @@ function formatCommitMessage(payload) {
}
} catch (e) {
// already logged inside summarizer; keep silent here
logMessage(`Executive summary generation failed: ${e.message}`, 'warn');
}
// Ensure each section starts on its own line; compare may be plain URL text.
@@ -419,6 +451,7 @@ app.post('/releasehook_kjfhdkf987987', (req, res) => {
.then((telegramMessage) => sendTelegramMessage(telegramMessage))
.catch(error => {
logMessage(`Error sending Telegram message: ${error.message}`, 'error');
dumpFailedWebhook(payload, error, 'telegram_send_error');
});
// Set a flag to track if we've sent a response