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

93
retry_failed_webhooks.js Executable file
View File

@@ -0,0 +1,93 @@
#!/usr/bin/env node
// Script to retry failed webhooks after code fixes
const fs = require('fs');
const path = require('path');
const axios = require('axios');
const failedWebhooksDir = path.join(__dirname, 'failed_webhooks');
const WEBHOOK_URL = 'http://localhost:9304/releasehook_kjfhdkf987987';
async function retryFailedWebhook(filepath) {
try {
console.log(`\nProcessing: ${path.basename(filepath)}`);
const data = JSON.parse(fs.readFileSync(filepath, 'utf8'));
const payload = data.payload;
console.log(`Repository: ${payload?.repository?.name || 'unknown'}`);
console.log(`Original error: ${data.error}`);
console.log(`Failed at: ${data.timestamp}`);
// Retry the webhook
const response = await axios.post(WEBHOOK_URL, payload, {
headers: {
'Content-Type': 'application/json'
},
timeout: 30000
});
console.log(`✅ Retry successful: ${response.status}`);
// Move the file to processed directory
const processedDir = path.join(failedWebhooksDir, 'processed');
if (!fs.existsSync(processedDir)) {
fs.mkdirSync(processedDir);
}
const newPath = path.join(processedDir, path.basename(filepath));
fs.renameSync(filepath, newPath);
console.log(`Moved to processed: ${path.basename(newPath)}`);
return true;
} catch (error) {
console.log(`❌ Retry failed: ${error.message}`);
if (error.response) {
console.log(`Response status: ${error.response.status}`);
console.log(`Response data:`, error.response.data);
}
return false;
}
}
async function main() {
if (!fs.existsSync(failedWebhooksDir)) {
console.log('No failed_webhooks directory found.');
return;
}
const files = fs.readdirSync(failedWebhooksDir)
.filter(file => file.endsWith('.json'))
.map(file => path.join(failedWebhooksDir, file));
if (files.length === 0) {
console.log('No failed webhooks to retry.');
return;
}
console.log(`Found ${files.length} failed webhook(s) to retry:`);
let successCount = 0;
let failCount = 0;
for (const filepath of files) {
const success = await retryFailedWebhook(filepath);
if (success) {
successCount++;
} else {
failCount++;
}
// Add a small delay between retries
await new Promise(resolve => setTimeout(resolve, 1000));
}
console.log(`\n📊 Summary:`);
console.log(`✅ Successful retries: ${successCount}`);
console.log(`❌ Failed retries: ${failCount}`);
}
if (require.main === module) {
main().catch(console.error);
}
module.exports = { retryFailedWebhook, main };