From 890f225912ead569fec12e1364af38f71155c8da Mon Sep 17 00:00:00 2001 From: karishmas6 Date: Wed, 11 Sep 2024 11:53:12 +0530 Subject: [PATCH] feat: run workflow --- .../workflow-management/scheduler/index.ts | 61 ++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/server/src/workflow-management/scheduler/index.ts b/server/src/workflow-management/scheduler/index.ts index 587d9fd4..5d9584ae 100644 --- a/server/src/workflow-management/scheduler/index.ts +++ b/server/src/workflow-management/scheduler/index.ts @@ -1,5 +1,10 @@ import { Queue, Worker } from 'bullmq'; import IORedis from 'ioredis'; +import { deleteFile, readFile, readFiles, saveFile } from "../storage"; +import { createRemoteBrowserForRun, destroyRemoteBrowser } from '../../browser-management/controller'; +import logger from '../../logger'; +import { browserPool } from "../../server"; +import fs from "fs"; const connection = new IORedis(); @@ -24,4 +29,58 @@ worker.on('failed', (job: any, err) => { console.error(`Job ${job.id} failed for ${job.data.fileName}_${job.data.runId}:`, err); }); -export { workflowQueue }; \ No newline at end of file +export { workflowQueue }; + +async function runWorkflow(fileName, runId) { + try { + // read the recording from storage + const recording = await readFile(`./../storage/recordings/${fileName}.waw.json`); + const parsedRecording = JSON.parse(recording); + // read the run from storage + const run = await readFile(`./../storage/runs/${fileName}_${runId}.json`); + const parsedRun = JSON.parse(run); + + // interpret the run in active browser + const browser = browserPool.getRemoteBrowser(parsedRun.browserId); + const currentPage = browser?.getCurrentPage(); + if (browser && currentPage) { + const interpretationInfo = await browser.interpreter.InterpretRecording( + parsedRecording.recording, currentPage, parsedRun.interpreterSettings); + const duration = Math.round((new Date().getTime() - new Date(parsedRun.startedAt).getTime()) / 1000); + const durString = (() => { + if (duration < 60) { + return `${duration} s`; + } + else { + const minAndS = (duration / 60).toString().split('.'); + return `${minAndS[0]} m ${minAndS[1]} s`; + } + })(); + await destroyRemoteBrowser(parsedRun.browserId); + const run_meta = { + ...parsedRun, + status: interpretationInfo.result, + finishedAt: new Date().toLocaleString(), + duration: durString, + browserId: null, + log: interpretationInfo.log.join('\n'), + serializableOutput: interpretationInfo.serializableOutput, + binaryOutput: interpretationInfo.binaryOutput, + }; + fs.mkdirSync('../storage/runs', { recursive: true }); + await saveFile( + `../storage/runs/${parsedRun.name}_${runId}.json`, + JSON.stringify(run_meta, null, 2) + ); + return true; + } else { + throw new Error('Could not destroy browser'); + } + } catch (e) { + const { message } = e as Error; + logger.log('info', `Error while running a recording with name: ${fileName}_${runId}.json`); + return false; + } +} + +export { runWorkflow }; \ No newline at end of file