feat: max retries for failure

This commit is contained in:
karishmas6
2024-09-19 18:14:56 +05:30
parent f6a7f5582c
commit 871a9b88bd

View File

@@ -8,9 +8,12 @@ import { readFile } from "../storage";
interface GoogleSheetUpdateTask { interface GoogleSheetUpdateTask {
name: string; name: string;
runId: string; runId: string;
status: 'pending' | 'completed'; status: 'pending' | 'completed' | 'failed';
retries: number;
} }
const MAX_RETRIES = 5;
export let googleSheetUpdateTasks: { [runId: string]: GoogleSheetUpdateTask } = {}; export let googleSheetUpdateTasks: { [runId: string]: GoogleSheetUpdateTask } = {};
@@ -93,16 +96,22 @@ export async function writeDataToSheet(spreadsheetId: string, range: string, dat
const processGoogleSheetUpdates = async () => { const processGoogleSheetUpdates = async () => {
while (true) { while (true) {
let hasPendingTasks = false; let hasPendingTasks = false;
for (const runId in googleSheetUpdateTasks) { for (const runId in googleSheetUpdateTasks) {
const task = googleSheetUpdateTasks[runId]; const task = googleSheetUpdateTasks[runId];
if (task.status === 'pending') { if (task.status === 'pending') {
hasPendingTasks = true; hasPendingTasks = true;
try { try {
await updateGoogleSheet(task.name, task.runId); await updateGoogleSheet(task.name, task.runId);
console.log(`Successfully updated Google Sheets for run ${task.runId}`); console.log(`Successfully updated Google Sheets for run ${task.runId}`);
delete googleSheetUpdateTasks[runId]; delete googleSheetUpdateTasks[runId];
} catch (error: any) { } catch (error: any) {
if (task.retries < MAX_RETRIES) {
googleSheetUpdateTasks[runId].retries += 1;
} else {
// Mark as failed after maximum retries
googleSheetUpdateTasks[runId].status = 'failed';
}
console.error(`Failed to update Google Sheets for run ${task.runId}:`, error); console.error(`Failed to update Google Sheets for run ${task.runId}:`, error);
} }
} }