Files
deployHook/retry_failed_webhooks.js
sebseb7 7228d4b763 fix(webhooks): extend timeout for webhook retries to 15 minutes
Increase the timeout duration in the retryFailedWebhook function from 30 seconds to 15 minutes to accommodate longer processing times for webhook retries. This change aims to improve the reliability of the retry mechanism.
2025-08-05 19:49:14 +02:00

102 lines
3.0 KiB
JavaScript
Executable File

#!/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 failure reason: ${data.reason || '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: 900000 // 15 minutes
});
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);
}
if (error.code) {
console.log(`Error code: ${error.code}`);
}
if (error.config) {
console.log(`Request URL: ${error.config.url}`);
console.log(`Request method: ${error.config.method}`);
}
console.log(`Full error details:`, error.toJSON ? error.toJSON() : error);
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 };