2024-09-26 22:59:37 +05:30
|
|
|
import { readFile, readFiles } from "../workflow-management/storage";
|
2024-09-26 22:39:39 +05:30
|
|
|
import { Router, Request, Response } from 'express';
|
2024-10-12 15:48:41 +05:30
|
|
|
import { chromium } from "playwright";
|
2024-09-26 22:41:46 +05:30
|
|
|
import { requireAPIKey } from "../middlewares/api";
|
2024-10-10 05:47:42 +05:30
|
|
|
import Robot from "../models/Robot";
|
2024-10-10 06:13:17 +05:30
|
|
|
import Run from "../models/Run";
|
2024-10-03 17:18:35 +05:30
|
|
|
const router = Router();
|
2024-10-12 15:48:41 +05:30
|
|
|
import { getDecryptedProxyConfig } from "../routes/proxy";
|
|
|
|
|
import { uuid } from "uuidv4";
|
|
|
|
|
import { createRemoteBrowserForRun, destroyRemoteBrowser } from "../browser-management/controller";
|
|
|
|
|
import logger from "../logger";
|
2024-10-12 15:51:03 +05:30
|
|
|
import { browserPool } from "../server";
|
2024-10-12 15:55:00 +05:30
|
|
|
import { io, Socket } from "socket.io-client";
|
2024-10-15 22:17:16 +05:30
|
|
|
import { BinaryOutputService } from "../storage/mino";
|
2024-10-24 22:26:12 +05:30
|
|
|
import { AuthenticatedRequest } from "../routes/record"
|
2024-10-28 04:20:46 +05:30
|
|
|
import captureServerAnalytics from "../utils/analytics";
|
2024-09-26 22:39:39 +05:30
|
|
|
|
|
|
|
|
const formatRecording = (recordingData: any) => {
|
|
|
|
|
const recordingMeta = recordingData.recording_meta;
|
|
|
|
|
const workflow = recordingData.recording.workflow || [];
|
|
|
|
|
const firstWorkflowStep = workflow[0]?.where?.url || '';
|
2024-09-26 22:39:57 +05:30
|
|
|
|
2024-09-26 22:39:39 +05:30
|
|
|
const inputParameters = [
|
2024-09-26 22:39:57 +05:30
|
|
|
{
|
|
|
|
|
type: "string",
|
|
|
|
|
name: "originUrl",
|
|
|
|
|
label: "Origin URL",
|
|
|
|
|
required: true,
|
|
|
|
|
defaultValue: firstWorkflowStep,
|
|
|
|
|
},
|
2024-09-26 22:39:39 +05:30
|
|
|
];
|
2024-09-26 22:39:57 +05:30
|
|
|
|
2024-09-26 22:39:39 +05:30
|
|
|
return {
|
2024-09-26 22:39:57 +05:30
|
|
|
id: recordingMeta.id,
|
|
|
|
|
name: recordingMeta.name,
|
2024-10-08 20:50:17 +05:30
|
|
|
createdAt: new Date(recordingMeta.createdAt).getTime(),
|
2024-09-26 22:39:57 +05:30
|
|
|
inputParameters,
|
2024-09-26 22:39:39 +05:30
|
|
|
};
|
2024-09-26 22:39:57 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2024-10-03 17:18:35 +05:30
|
|
|
router.get("/robots", requireAPIKey, async (req: Request, res: Response) => {
|
2024-09-26 22:39:39 +05:30
|
|
|
try {
|
2024-10-10 06:03:56 +05:30
|
|
|
const robots = await Robot.findAll({ raw: true });
|
2024-10-10 05:57:49 +05:30
|
|
|
const formattedRecordings = robots.map(formatRecording);
|
2024-09-26 22:39:57 +05:30
|
|
|
|
|
|
|
|
const response = {
|
|
|
|
|
statusCode: 200,
|
|
|
|
|
messageCode: "success",
|
|
|
|
|
robots: {
|
|
|
|
|
totalCount: formattedRecordings.length,
|
|
|
|
|
items: formattedRecordings,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
res.status(200).json(response);
|
2024-09-26 22:39:39 +05:30
|
|
|
} catch (error) {
|
2024-09-26 22:39:57 +05:30
|
|
|
console.error("Error fetching recordings:", error);
|
|
|
|
|
res.status(500).json({
|
|
|
|
|
statusCode: 500,
|
|
|
|
|
messageCode: "error",
|
|
|
|
|
message: "Failed to retrieve recordings",
|
|
|
|
|
});
|
2024-09-26 22:39:39 +05:30
|
|
|
}
|
2024-09-26 22:39:57 +05:30
|
|
|
});
|
2024-09-26 22:56:44 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
const formatRecordingById = (recordingData: any) => {
|
|
|
|
|
const recordingMeta = recordingData.recording_meta;
|
|
|
|
|
const workflow = recordingData.recording.workflow || [];
|
|
|
|
|
const firstWorkflowStep = workflow[0]?.where?.url || '';
|
2024-09-26 22:57:07 +05:30
|
|
|
|
2024-09-26 22:56:44 +05:30
|
|
|
const inputParameters = [
|
2024-09-26 22:57:07 +05:30
|
|
|
{
|
|
|
|
|
type: "string",
|
|
|
|
|
name: "originUrl",
|
|
|
|
|
label: "Origin URL",
|
|
|
|
|
required: true,
|
|
|
|
|
defaultValue: firstWorkflowStep,
|
|
|
|
|
},
|
2024-09-26 22:56:44 +05:30
|
|
|
];
|
2024-09-26 22:57:07 +05:30
|
|
|
|
2024-09-26 22:56:44 +05:30
|
|
|
return {
|
2024-09-26 22:57:07 +05:30
|
|
|
id: recordingMeta.id,
|
|
|
|
|
name: recordingMeta.name,
|
2024-10-08 20:50:17 +05:30
|
|
|
createdAt: new Date(recordingMeta.createdAt).getTime(),
|
2024-09-26 22:57:07 +05:30
|
|
|
inputParameters,
|
2024-09-26 22:56:44 +05:30
|
|
|
};
|
2024-09-26 22:57:07 +05:30
|
|
|
};
|
2024-09-26 22:56:44 +05:30
|
|
|
|
2024-10-10 06:08:11 +05:30
|
|
|
router.get("/robots/:id", requireAPIKey, async (req: Request, res: Response) => {
|
2024-09-26 22:56:44 +05:30
|
|
|
try {
|
2024-10-10 06:08:11 +05:30
|
|
|
const robot = await Robot.findOne({
|
|
|
|
|
where: {
|
2024-10-10 06:08:26 +05:30
|
|
|
'recording_meta.id': req.params.id
|
2024-10-10 06:08:11 +05:30
|
|
|
},
|
|
|
|
|
raw: true
|
2024-10-10 06:08:26 +05:30
|
|
|
});
|
2024-09-26 22:57:07 +05:30
|
|
|
|
2024-10-10 06:08:11 +05:30
|
|
|
const formattedRecording = formatRecordingById(robot);
|
2024-09-26 22:57:07 +05:30
|
|
|
|
|
|
|
|
const response = {
|
|
|
|
|
statusCode: 200,
|
|
|
|
|
messageCode: "success",
|
|
|
|
|
robot: formattedRecording,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
res.status(200).json(response);
|
2024-09-26 22:56:44 +05:30
|
|
|
} catch (error) {
|
2024-09-26 22:57:07 +05:30
|
|
|
console.error("Error fetching recording:", error);
|
|
|
|
|
res.status(404).json({
|
|
|
|
|
statusCode: 404,
|
|
|
|
|
messageCode: "not_found",
|
|
|
|
|
message: `Recording with name "${req.params.fileName}" not found.`,
|
|
|
|
|
});
|
2024-09-26 22:56:44 +05:30
|
|
|
}
|
2024-09-26 22:57:07 +05:30
|
|
|
});
|
2024-10-03 17:18:35 +05:30
|
|
|
|
2024-10-10 06:13:17 +05:30
|
|
|
router.get("/robots/:id/runs", requireAPIKey, async (req: Request, res: Response) => {
|
|
|
|
|
try {
|
|
|
|
|
const runs = await Run.findAll({
|
|
|
|
|
where: {
|
2024-10-10 06:18:42 +05:30
|
|
|
robotMetaId: req.params.id
|
2024-10-10 06:13:17 +05:30
|
|
|
},
|
|
|
|
|
raw: true
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const response = {
|
|
|
|
|
statusCode: 200,
|
|
|
|
|
messageCode: "success",
|
|
|
|
|
runs: {
|
|
|
|
|
totalCount: runs.length,
|
|
|
|
|
items: runs,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
res.status(200).json(response);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Error fetching runs:", error);
|
|
|
|
|
res.status(500).json({
|
|
|
|
|
statusCode: 500,
|
|
|
|
|
messageCode: "error",
|
|
|
|
|
message: "Failed to retrieve runs",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2024-10-10 06:15:07 +05:30
|
|
|
router.get("/robots/:id/runs/:runId", requireAPIKey, async (req: Request, res: Response) => {
|
|
|
|
|
try {
|
|
|
|
|
const run = await Run.findOne({
|
|
|
|
|
where: {
|
2024-10-10 06:22:53 +05:30
|
|
|
runId: req.params.runId,
|
|
|
|
|
robotMetaId: req.params.id,
|
2024-10-10 06:15:07 +05:30
|
|
|
},
|
|
|
|
|
raw: true
|
|
|
|
|
});
|
2024-10-12 15:55:43 +05:30
|
|
|
|
2024-10-10 06:15:07 +05:30
|
|
|
const response = {
|
|
|
|
|
statusCode: 200,
|
|
|
|
|
messageCode: "success",
|
|
|
|
|
run: run,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
res.status(200).json(response);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Error fetching run:", error);
|
|
|
|
|
res.status(404).json({
|
|
|
|
|
statusCode: 404,
|
|
|
|
|
messageCode: "not_found",
|
2024-10-10 06:24:04 +05:30
|
|
|
message: `Run with id "${req.params.runId}" for robot with id "${req.params.id}" not found.`,
|
2024-10-10 06:15:07 +05:30
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2024-10-12 21:55:46 +05:30
|
|
|
async function createWorkflowAndStoreMetadata(id: string, userId: string) {
|
|
|
|
|
try {
|
2024-10-12 22:20:06 +05:30
|
|
|
const recording = await Robot.findOne({
|
|
|
|
|
where: {
|
|
|
|
|
'recording_meta.id': id
|
|
|
|
|
},
|
|
|
|
|
raw: true
|
|
|
|
|
});
|
2024-10-12 21:55:46 +05:30
|
|
|
|
2024-10-12 22:20:06 +05:30
|
|
|
if (!recording || !recording.recording_meta || !recording.recording_meta.id) {
|
|
|
|
|
return {
|
|
|
|
|
success: false,
|
|
|
|
|
error: 'Recording not found'
|
|
|
|
|
};
|
|
|
|
|
}
|
2024-10-12 21:55:46 +05:30
|
|
|
|
2024-10-12 22:20:06 +05:30
|
|
|
const proxyConfig = await getDecryptedProxyConfig(userId);
|
|
|
|
|
let proxyOptions: any = {};
|
2024-10-12 21:55:46 +05:30
|
|
|
|
2024-10-12 22:20:06 +05:30
|
|
|
if (proxyConfig.proxy_url) {
|
|
|
|
|
proxyOptions = {
|
|
|
|
|
server: proxyConfig.proxy_url,
|
|
|
|
|
...(proxyConfig.proxy_username && proxyConfig.proxy_password && {
|
|
|
|
|
username: proxyConfig.proxy_username,
|
|
|
|
|
password: proxyConfig.proxy_password,
|
|
|
|
|
}),
|
|
|
|
|
};
|
|
|
|
|
}
|
2024-10-12 21:55:46 +05:30
|
|
|
|
|
|
|
|
const browserId = createRemoteBrowserForRun({
|
|
|
|
|
browser: chromium,
|
|
|
|
|
launchOptions: {
|
|
|
|
|
headless: true,
|
|
|
|
|
proxy: proxyOptions.server ? proxyOptions : undefined,
|
|
|
|
|
}
|
2024-10-27 17:54:41 +05:30
|
|
|
}, userId);
|
2024-10-12 21:55:46 +05:30
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
interpreterSettings: { maxConcurrency: 1, maxRepeats: 1, debug: true },
|
|
|
|
|
log: '',
|
|
|
|
|
runId,
|
2024-10-21 19:09:02 +05:30
|
|
|
runByAPI: true,
|
2024-10-12 21:55:46 +05:30
|
|
|
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,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-12 15:52:26 +05:30
|
|
|
async function readyForRunHandler(browserId: string, id: string) {
|
|
|
|
|
try {
|
2024-10-12 22:12:15 +05:30
|
|
|
const result = await executeRun(id);
|
2024-10-12 15:55:43 +05:30
|
|
|
|
2024-10-12 22:12:15 +05:30
|
|
|
if (result && result.success) {
|
2024-10-12 15:55:43 +05:30
|
|
|
logger.log('info', `Interpretation of ${id} succeeded`);
|
2024-10-12 22:19:38 +05:30
|
|
|
resetRecordingState(browserId, id);
|
2024-10-12 22:12:15 +05:30
|
|
|
return result.interpretationInfo;
|
2024-10-12 15:55:43 +05:30
|
|
|
} else {
|
|
|
|
|
logger.log('error', `Interpretation of ${id} failed`);
|
|
|
|
|
await destroyRemoteBrowser(browserId);
|
2024-10-12 22:19:38 +05:30
|
|
|
resetRecordingState(browserId, id);
|
2024-10-12 22:12:15 +05:30
|
|
|
return null;
|
2024-10-12 15:55:43 +05:30
|
|
|
}
|
|
|
|
|
|
2024-10-12 15:52:26 +05:30
|
|
|
} catch (error: any) {
|
2024-10-12 15:55:43 +05:30
|
|
|
logger.error(`Error during readyForRunHandler: ${error.message}`);
|
|
|
|
|
await destroyRemoteBrowser(browserId);
|
2024-10-12 22:12:15 +05:30
|
|
|
return null;
|
2024-10-12 15:52:26 +05:30
|
|
|
}
|
2024-10-12 15:55:43 +05:30
|
|
|
}
|
2024-10-12 15:52:26 +05:30
|
|
|
|
2024-10-12 22:12:15 +05:30
|
|
|
|
2024-10-12 15:55:43 +05:30
|
|
|
function resetRecordingState(browserId: string, id: string) {
|
2024-10-12 15:53:13 +05:30
|
|
|
browserId = '';
|
|
|
|
|
id = '';
|
2024-10-12 15:55:43 +05:30
|
|
|
}
|
2024-10-12 15:53:13 +05:30
|
|
|
|
2024-10-12 15:50:32 +05:30
|
|
|
async function executeRun(id: string) {
|
|
|
|
|
try {
|
2024-10-12 15:55:43 +05:30
|
|
|
const run = await Run.findOne({ where: { runId: id } });
|
|
|
|
|
if (!run) {
|
|
|
|
|
return {
|
|
|
|
|
success: false,
|
|
|
|
|
error: 'Run not found'
|
2024-10-12 22:11:02 +05:30
|
|
|
};
|
2024-10-12 15:50:32 +05:30
|
|
|
}
|
2024-10-12 15:55:43 +05:30
|
|
|
|
|
|
|
|
const plainRun = run.toJSON();
|
|
|
|
|
|
|
|
|
|
const recording = await Robot.findOne({ where: { 'recording_meta.id': plainRun.robotMetaId }, raw: true });
|
|
|
|
|
if (!recording) {
|
|
|
|
|
return {
|
|
|
|
|
success: false,
|
|
|
|
|
error: 'Recording not found'
|
2024-10-12 22:11:02 +05:30
|
|
|
};
|
2024-10-12 15:55:43 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
plainRun.status = 'running';
|
|
|
|
|
|
|
|
|
|
const browser = browserPool.getRemoteBrowser(plainRun.browserId);
|
|
|
|
|
if (!browser) {
|
|
|
|
|
throw new Error('Could not access browser');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const currentPage = await browser.getCurrentPage();
|
|
|
|
|
if (!currentPage) {
|
|
|
|
|
throw new Error('Could not create a new page');
|
2024-10-12 15:50:32 +05:30
|
|
|
}
|
2024-10-12 15:55:43 +05:30
|
|
|
|
|
|
|
|
const interpretationInfo = await browser.interpreter.InterpretRecording(
|
2024-10-12 22:11:02 +05:30
|
|
|
recording.recording, currentPage, plainRun.interpreterSettings
|
|
|
|
|
);
|
2024-10-12 15:55:43 +05:30
|
|
|
|
2024-10-15 22:17:16 +05:30
|
|
|
const binaryOutputService = new BinaryOutputService('maxun-run-screenshots');
|
|
|
|
|
const uploadedBinaryOutput = await binaryOutputService.uploadAndStoreBinaryOutput(run, interpretationInfo.binaryOutput);
|
|
|
|
|
|
2024-10-12 15:55:43 +05:30
|
|
|
await destroyRemoteBrowser(plainRun.browserId);
|
|
|
|
|
|
2024-10-12 22:16:23 +05:30
|
|
|
const updatedRun = await run.update({
|
2024-10-12 15:55:43 +05:30
|
|
|
...run,
|
|
|
|
|
status: 'success',
|
|
|
|
|
finishedAt: new Date().toLocaleString(),
|
|
|
|
|
browserId: plainRun.browserId,
|
|
|
|
|
log: interpretationInfo.log.join('\n'),
|
|
|
|
|
serializableOutput: interpretationInfo.serializableOutput,
|
2024-10-15 22:17:47 +05:30
|
|
|
binaryOutput: uploadedBinaryOutput,
|
2024-10-12 15:55:43 +05:30
|
|
|
});
|
2024-10-12 22:11:02 +05:30
|
|
|
|
2024-10-28 04:49:54 +05:30
|
|
|
let totalRowsExtracted = 0;
|
|
|
|
|
updatedRun.serializableOutput['item-0'].forEach((item: any) => {
|
|
|
|
|
totalRowsExtracted += Object.keys(item).length;
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2024-10-28 04:20:46 +05:30
|
|
|
captureServerAnalytics.capture({
|
|
|
|
|
distinctId: id,
|
|
|
|
|
event: 'maxun-oss-run-created-api',
|
|
|
|
|
properties: {
|
2024-10-28 04:21:05 +05:30
|
|
|
runId: id,
|
|
|
|
|
created_at: new Date().toISOString(),
|
|
|
|
|
status: 'success',
|
2024-10-28 04:49:54 +05:30
|
|
|
extractedItemsCount: updatedRun.serializableOutput['item-0'].length,
|
|
|
|
|
extractedRowsCount: totalRowsExtracted,
|
|
|
|
|
extractedScreenshotsCount: updatedRun.binaryOutput['item-0'].length,
|
2024-10-28 04:20:46 +05:30
|
|
|
}
|
2024-10-28 04:21:05 +05:30
|
|
|
})
|
2024-10-28 04:20:46 +05:30
|
|
|
|
2024-10-12 22:11:02 +05:30
|
|
|
return {
|
|
|
|
|
success: true,
|
2024-10-12 22:16:23 +05:30
|
|
|
interpretationInfo: updatedRun.toJSON()
|
2024-10-12 22:11:02 +05:30
|
|
|
};
|
|
|
|
|
|
2024-10-12 15:50:32 +05:30
|
|
|
} catch (error: any) {
|
2024-10-12 15:55:43 +05:30
|
|
|
logger.log('info', `Error while running a recording with id: ${id} - ${error.message}`);
|
2024-10-28 04:20:46 +05:30
|
|
|
const run = await Run.findOne({ where: { runId: id } });
|
|
|
|
|
if (run) {
|
2024-10-28 04:21:05 +05:30
|
|
|
await run.update({
|
|
|
|
|
status: 'failed',
|
|
|
|
|
finishedAt: new Date().toLocaleString(),
|
|
|
|
|
});
|
2024-10-28 04:20:46 +05:30
|
|
|
}
|
|
|
|
|
captureServerAnalytics.capture({
|
|
|
|
|
distinctId: id,
|
|
|
|
|
event: 'maxun-oss-run-created-api',
|
|
|
|
|
properties: {
|
2024-10-28 04:21:05 +05:30
|
|
|
runId: id,
|
|
|
|
|
created_at: new Date().toISOString(),
|
|
|
|
|
status: 'failed',
|
2024-10-28 04:20:46 +05:30
|
|
|
}
|
2024-10-28 04:21:05 +05:30
|
|
|
});
|
2024-10-12 22:11:02 +05:30
|
|
|
return {
|
|
|
|
|
success: false,
|
|
|
|
|
error: error.message,
|
|
|
|
|
};
|
2024-10-12 15:50:32 +05:30
|
|
|
}
|
2024-10-12 15:55:43 +05:30
|
|
|
}
|
2024-10-12 15:50:32 +05:30
|
|
|
|
2024-10-12 15:55:43 +05:30
|
|
|
export async function handleRunRecording(id: string, userId: string) {
|
2024-10-12 15:54:14 +05:30
|
|
|
try {
|
2024-10-12 15:55:43 +05:30
|
|
|
const result = await createWorkflowAndStoreMetadata(id, userId);
|
|
|
|
|
const { browserId, runId: newRunId } = result;
|
|
|
|
|
|
|
|
|
|
if (!browserId || !newRunId || !userId) {
|
|
|
|
|
throw new Error('browserId or runId or userId is undefined');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const socket = io(`http://localhost:8080/${browserId}`, {
|
|
|
|
|
transports: ['websocket'],
|
|
|
|
|
rejectUnauthorized: false
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
socket.on('ready-for-run', () => readyForRunHandler(browserId, newRunId));
|
|
|
|
|
|
|
|
|
|
logger.log('info', `Running recording: ${id}`);
|
|
|
|
|
|
|
|
|
|
socket.on('disconnect', () => {
|
|
|
|
|
cleanupSocketListeners(socket, browserId, newRunId);
|
|
|
|
|
});
|
|
|
|
|
|
2024-10-12 22:43:10 +05:30
|
|
|
// Return the runId immediately, so the client knows the run is started
|
|
|
|
|
return newRunId;
|
|
|
|
|
|
2024-10-12 15:54:14 +05:30
|
|
|
} catch (error: any) {
|
2024-10-12 15:55:43 +05:30
|
|
|
logger.error('Error running recording:', error);
|
2024-10-12 15:54:14 +05:30
|
|
|
}
|
2024-10-12 15:55:43 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function cleanupSocketListeners(socket: Socket, browserId: string, id: string) {
|
2024-10-12 15:54:14 +05:30
|
|
|
socket.off('ready-for-run', () => readyForRunHandler(browserId, id));
|
|
|
|
|
logger.log('info', `Cleaned up listeners for browserId: ${browserId}, runId: ${id}`);
|
2024-10-12 15:55:43 +05:30
|
|
|
}
|
2024-10-12 15:54:14 +05:30
|
|
|
|
2024-10-12 22:43:10 +05:30
|
|
|
async function waitForRunCompletion(runId: string, interval: number = 2000) {
|
|
|
|
|
while (true) {
|
|
|
|
|
const run = await Run.findOne({ where: { runId }, raw: true });
|
|
|
|
|
if (!run) throw new Error('Run not found');
|
|
|
|
|
|
|
|
|
|
if (run.status === 'success') {
|
|
|
|
|
return run;
|
2024-10-28 04:20:46 +05:30
|
|
|
} else if (run.status === 'failed') {
|
2024-10-12 22:43:10 +05:30
|
|
|
throw new Error('Run failed');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Wait for the next polling interval
|
|
|
|
|
await new Promise(resolve => setTimeout(resolve, interval));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-24 22:26:12 +05:30
|
|
|
router.post("/robots/:id/runs", requireAPIKey, async (req: AuthenticatedRequest, res: Response) => {
|
2024-10-12 15:48:41 +05:30
|
|
|
try {
|
2024-10-24 22:26:12 +05:30
|
|
|
if (!req.user) {
|
|
|
|
|
return res.status(401).json({ ok: false, error: 'Unauthorized' });
|
|
|
|
|
}
|
2024-10-12 22:43:10 +05:30
|
|
|
const runId = await handleRunRecording(req.params.id, req.user.dataValues.id);
|
|
|
|
|
console.log(`Result`, runId);
|
|
|
|
|
|
|
|
|
|
if (!runId) {
|
|
|
|
|
throw new Error('Run ID is undefined');
|
|
|
|
|
}
|
|
|
|
|
const completedRun = await waitForRunCompletion(runId);
|
2024-10-12 15:48:41 +05:30
|
|
|
|
|
|
|
|
const response = {
|
|
|
|
|
statusCode: 200,
|
|
|
|
|
messageCode: "success",
|
2024-10-12 22:43:10 +05:30
|
|
|
run: completedRun,
|
2024-10-12 15:48:41 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
|
|
res.status(200).json(response);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Error running robot:", error);
|
|
|
|
|
res.status(500).json({
|
|
|
|
|
statusCode: 500,
|
|
|
|
|
messageCode: "error",
|
|
|
|
|
message: "Failed to run robot",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2024-10-12 22:14:33 +05:30
|
|
|
|
2024-10-03 17:18:35 +05:30
|
|
|
export default router;
|