feat: use googleSheetUpdateTasks

This commit is contained in:
karishmas6
2024-09-19 17:58:38 +05:30
parent 0ab33f29e8
commit 683242ae28

View File

@@ -13,7 +13,7 @@ import { uuid } from "uuidv4";
import { workflowQueue } from '../workflow-management/scheduler';
import moment from 'moment-timezone';
import cron from 'node-cron';
import { updateGoogleSheet } from '../workflow-management/integrations/gsheet';
import { googleSheetUpdateTasks } from '../workflow-management/integrations/gsheet';
export const router = Router();
@@ -140,16 +140,6 @@ router.get('/runs/run/:fileName/:runId', async (req, res) => {
}
});
interface GoogleSheetUpdateTask {
name: string;
runId: string;
status: 'pending' | 'completed';
}
let googleSheetUpdateTasks: { [runId: string]: GoogleSheetUpdateTask } = {};
console.log('googleSheetUpdateTasks:', googleSheetUpdateTasks);
/**
* PUT endpoint for finishing a run and saving it to the storage.
*/
@@ -347,29 +337,4 @@ router.post('/runs/abort/:fileName/:runId', async (req, res) => {
logger.log('info', `Error while running a recording with name: ${req.params.fileName}_${req.params.runId}.json`);
return res.send(false);
}
});
const processGoogleSheetUpdates = async () => {
while (true) {
for (const runId in googleSheetUpdateTasks) {
const task = googleSheetUpdateTasks[runId];
if (task.status === 'pending') {
try {
try {
await updateGoogleSheet(task.name, task.runId);
console.log(`Successfully updated Google Sheets for run ${task.runId}`);
googleSheetUpdateTasks[runId].status = 'completed';
} catch (error: any) {
console.error(`update google sheet error ${task.runId}:`, error);
}
} catch (error: any) {
console.error(`Failed to update Google Sheets for run ${task.runId}:`, error);
}
}
}
await new Promise(resolve => setTimeout(resolve, 5000));
}
};
processGoogleSheetUpdates();
});