feat(temp): use in-memory store
This commit is contained in:
@@ -7,12 +7,13 @@ import logger from "../logger";
|
|||||||
import { deleteFile, readFile, readFiles, saveFile } from "../workflow-management/storage";
|
import { deleteFile, readFile, readFiles, saveFile } from "../workflow-management/storage";
|
||||||
import { createRemoteBrowserForRun, destroyRemoteBrowser } from "../browser-management/controller";
|
import { createRemoteBrowserForRun, destroyRemoteBrowser } from "../browser-management/controller";
|
||||||
import { chromium } from "playwright";
|
import { chromium } from "playwright";
|
||||||
import { browserPool, io } from "../server";
|
import { browserPool } from "../server";
|
||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
import { uuid } from "uuidv4";
|
import { uuid } from "uuidv4";
|
||||||
import { workflowQueue } from '../workflow-management/scheduler';
|
import { workflowQueue } from '../workflow-management/scheduler';
|
||||||
import moment from 'moment-timezone';
|
import moment from 'moment-timezone';
|
||||||
import cron from 'node-cron';
|
import cron from 'node-cron';
|
||||||
|
import { updateGoogleSheet } from './integration';
|
||||||
|
|
||||||
export const router = Router();
|
export const router = Router();
|
||||||
|
|
||||||
@@ -139,6 +140,16 @@ 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.
|
* PUT endpoint for finishing a run and saving it to the storage.
|
||||||
*/
|
*/
|
||||||
@@ -169,7 +180,7 @@ router.post('/runs/run/:fileName/:runId', async (req, res) => {
|
|||||||
await destroyRemoteBrowser(parsedRun.browserId);
|
await destroyRemoteBrowser(parsedRun.browserId);
|
||||||
const run_meta = {
|
const run_meta = {
|
||||||
...parsedRun,
|
...parsedRun,
|
||||||
status: interpretationInfo.result,
|
status: 'success',
|
||||||
finishedAt: new Date().toLocaleString(),
|
finishedAt: new Date().toLocaleString(),
|
||||||
duration: durString,
|
duration: durString,
|
||||||
browserId: parsedRun.browserId,
|
browserId: parsedRun.browserId,
|
||||||
@@ -182,8 +193,16 @@ router.post('/runs/run/:fileName/:runId', async (req, res) => {
|
|||||||
`../storage/runs/${parsedRun.name}_${req.params.runId}.json`,
|
`../storage/runs/${parsedRun.name}_${req.params.runId}.json`,
|
||||||
JSON.stringify(run_meta, null, 2)
|
JSON.stringify(run_meta, null, 2)
|
||||||
);
|
);
|
||||||
io.emit('run-finished', { success: true });
|
|
||||||
return res.send(true);
|
res.send(true);
|
||||||
|
|
||||||
|
googleSheetUpdateTasks[req.params.runId] = {
|
||||||
|
name: parsedRun.name,
|
||||||
|
runId: req.params.runId,
|
||||||
|
status: 'pending',
|
||||||
|
};
|
||||||
|
|
||||||
|
return;
|
||||||
} else {
|
} else {
|
||||||
throw new Error('Could not destroy browser');
|
throw new Error('Could not destroy browser');
|
||||||
}
|
}
|
||||||
@@ -291,11 +310,9 @@ router.put('/schedule/:fileName/', async (req, res) => {
|
|||||||
*/
|
*/
|
||||||
router.post('/runs/abort/:fileName/:runId', async (req, res) => {
|
router.post('/runs/abort/:fileName/:runId', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
// read the run from storage
|
|
||||||
const run = await readFile(`./../storage/runs/${req.params.fileName}_${req.params.runId}.json`)
|
const run = await readFile(`./../storage/runs/${req.params.fileName}_${req.params.runId}.json`)
|
||||||
const parsedRun = JSON.parse(run);
|
const parsedRun = JSON.parse(run);
|
||||||
|
|
||||||
//get current log
|
|
||||||
const browser = browserPool.getRemoteBrowser(parsedRun.browserId);
|
const browser = browserPool.getRemoteBrowser(parsedRun.browserId);
|
||||||
const currentLog = browser?.interpreter.debugMessages.join('/n');
|
const currentLog = browser?.interpreter.debugMessages.join('/n');
|
||||||
const serializableOutput = browser?.interpreter.serializableData.reduce((reducedObject, item, index) => {
|
const serializableOutput = browser?.interpreter.serializableData.reduce((reducedObject, item, index) => {
|
||||||
@@ -331,3 +348,28 @@ router.post('/runs/abort/:fileName/:runId', async (req, res) => {
|
|||||||
return res.send(false);
|
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();
|
||||||
Reference in New Issue
Block a user