From 669a12bf50e6c9b492448627cf718aac4e3112f4 Mon Sep 17 00:00:00 2001 From: karishmas6 Date: Sat, 12 Oct 2024 21:38:50 +0530 Subject: [PATCH] fix: create runId --- server/src/api/record.ts | 146 +++++++++++++++++++-------------------- 1 file changed, 72 insertions(+), 74 deletions(-) diff --git a/server/src/api/record.ts b/server/src/api/record.ts index b4df4bf2..a463ca31 100644 --- a/server/src/api/record.ts +++ b/server/src/api/record.ts @@ -171,80 +171,6 @@ router.get("/robots/:id/runs/:runId", requireAPIKey, async (req: Request, res: R } }); -async function createWorkflowAndStoreMetadata(id: string, userId: string) { - if (!id) { - id = uuid(); - } - - const recording = await Robot.findOne({ - where: { - 'recording_meta.id': id - }, - raw: true - }); - - if (!recording || !recording.recording_meta || !recording.recording_meta.id) { - return { - success: false, - error: 'Recording not found' - }; - } - - const proxyConfig = await getDecryptedProxyConfig(userId); - let proxyOptions: any = {}; - - if (proxyConfig.proxy_url) { - proxyOptions = { - server: proxyConfig.proxy_url, - ...(proxyConfig.proxy_username && proxyConfig.proxy_password && { - username: proxyConfig.proxy_username, - password: proxyConfig.proxy_password, - }), - }; - } - - try { - const browserId = createRemoteBrowserForRun({ - browser: chromium, - launchOptions: { - headless: true, - proxy: proxyOptions.server ? proxyOptions : undefined, - } - }); - - const run = await Run.create({ - status: 'Running', - name: recording.recording_meta.name, - robotId: recording.id, - robotMetaId: recording.recording_meta.id, - startedAt: new Date().toLocaleString(), - finishedAt: '', - browserId: id, - interpreterSettings: { maxConcurrency: 1, maxRepeats: 1, debug: true }, - log: '', - runId: id, - serializableOutput: {}, - binaryOutput: {}, - }); - - const plainRun = run.toJSON(); - - return { - browserId, - runId: plainRun.runId, - } - - } catch (e) { - const { message } = e as Error; - logger.log('info', `Error while scheduling a run with id: ${id}`); - console.log(message); - return { - success: false, - error: message, - }; - } -} - async function readyForRunHandler(browserId: string, id: string) { try { const interpretation = await executeRun(id); @@ -323,6 +249,78 @@ async function executeRun(id: string) { } } +async function createWorkflowAndStoreMetadata(id: string, userId: string) { + const recording = await Robot.findOne({ + where: { + 'recording_meta.id': id + }, + raw: true + }); + + if (!recording || !recording.recording_meta || !recording.recording_meta.id) { + return { + success: false, + error: 'Recording not found' + }; + } + + const proxyConfig = await getDecryptedProxyConfig(userId); + let proxyOptions: any = {}; + + if (proxyConfig.proxy_url) { + proxyOptions = { + server: proxyConfig.proxy_url, + ...(proxyConfig.proxy_username && proxyConfig.proxy_password && { + username: proxyConfig.proxy_username, + password: proxyConfig.proxy_password, + }), + }; + } + + try { + const browserId = createRemoteBrowserForRun({ + browser: chromium, + launchOptions: { + headless: true, + proxy: proxyOptions.server ? proxyOptions : undefined, + } + }); + + const runId = uuid(); + + const run = await Run.create({ + status: 'Running', + name: recording.recording_meta.name, + robotId: recording.id, + robotMetaId: recording.recording_meta.id, + startedAt: new Date().toLocaleString(), + finishedAt: '', + browserId: id, + interpreterSettings: { maxConcurrency: 1, maxRepeats: 1, debug: true }, + log: '', + runId, + serializableOutput: {}, + binaryOutput: {}, + }); + + const plainRun = run.toJSON(); + + return { + browserId, + runId: plainRun.runId, + } + + } catch (e) { + const { message } = e as Error; + logger.log('info', `Error while scheduling a run with id: ${id}`); + console.log(message); + return { + success: false, + error: message, + }; + } +} + export async function handleRunRecording(id: string, userId: string) { try { const result = await createWorkflowAndStoreMetadata(id, userId);