chore: lint
This commit is contained in:
@@ -153,7 +153,7 @@ router.get("/robots/:id/runs/:runId", requireAPIKey, async (req: Request, res: R
|
|||||||
},
|
},
|
||||||
raw: true
|
raw: true
|
||||||
});
|
});
|
||||||
|
|
||||||
const response = {
|
const response = {
|
||||||
statusCode: 200,
|
statusCode: 200,
|
||||||
messageCode: "success",
|
messageCode: "success",
|
||||||
@@ -173,192 +173,192 @@ router.get("/robots/:id/runs/:runId", requireAPIKey, async (req: Request, res: R
|
|||||||
|
|
||||||
async function readyForRunHandler(browserId: string, id: string) {
|
async function readyForRunHandler(browserId: string, id: string) {
|
||||||
try {
|
try {
|
||||||
const interpretation = await executeRun(id);
|
const interpretation = await executeRun(id);
|
||||||
|
|
||||||
if (interpretation) {
|
|
||||||
logger.log('info', `Interpretation of ${id} succeeded`);
|
|
||||||
} else {
|
|
||||||
logger.log('error', `Interpretation of ${id} failed`);
|
|
||||||
await destroyRemoteBrowser(browserId);
|
|
||||||
}
|
|
||||||
|
|
||||||
resetRecordingState(browserId, id);
|
|
||||||
|
|
||||||
} catch (error: any) {
|
|
||||||
logger.error(`Error during readyForRunHandler: ${error.message}`);
|
|
||||||
await destroyRemoteBrowser(browserId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function resetRecordingState(browserId: string, id: string) {
|
if (interpretation) {
|
||||||
|
logger.log('info', `Interpretation of ${id} succeeded`);
|
||||||
|
} else {
|
||||||
|
logger.log('error', `Interpretation of ${id} failed`);
|
||||||
|
await destroyRemoteBrowser(browserId);
|
||||||
|
}
|
||||||
|
|
||||||
|
resetRecordingState(browserId, id);
|
||||||
|
|
||||||
|
} catch (error: any) {
|
||||||
|
logger.error(`Error during readyForRunHandler: ${error.message}`);
|
||||||
|
await destroyRemoteBrowser(browserId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function resetRecordingState(browserId: string, id: string) {
|
||||||
browserId = '';
|
browserId = '';
|
||||||
id = '';
|
id = '';
|
||||||
}
|
}
|
||||||
|
|
||||||
async function executeRun(id: string) {
|
async function executeRun(id: string) {
|
||||||
try {
|
try {
|
||||||
const run = await Run.findOne({ where: { runId: id } });
|
const run = await Run.findOne({ where: { runId: id } });
|
||||||
if (!run) {
|
if (!run) {
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
error: 'Run not found'
|
error: 'Run not found'
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
const plainRun = run.toJSON();
|
||||||
const plainRun = run.toJSON();
|
|
||||||
|
const recording = await Robot.findOne({ where: { 'recording_meta.id': plainRun.robotMetaId }, raw: true });
|
||||||
const recording = await Robot.findOne({ where: { 'recording_meta.id': plainRun.robotMetaId }, raw: true });
|
if (!recording) {
|
||||||
if (!recording) {
|
return {
|
||||||
return {
|
success: false,
|
||||||
success: false,
|
error: 'Recording not found'
|
||||||
error: 'Recording not found'
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
plainRun.status = 'running';
|
||||||
plainRun.status = 'running';
|
|
||||||
|
const browser = browserPool.getRemoteBrowser(plainRun.browserId);
|
||||||
const browser = browserPool.getRemoteBrowser(plainRun.browserId);
|
if (!browser) {
|
||||||
if (!browser) {
|
throw new Error('Could not access browser');
|
||||||
throw new Error('Could not access browser');
|
}
|
||||||
}
|
|
||||||
|
const currentPage = await browser.getCurrentPage();
|
||||||
const currentPage = await browser.getCurrentPage();
|
if (!currentPage) {
|
||||||
if (!currentPage) {
|
throw new Error('Could not create a new page');
|
||||||
throw new Error('Could not create a new page');
|
}
|
||||||
}
|
|
||||||
|
const interpretationInfo = await browser.interpreter.InterpretRecording(
|
||||||
const interpretationInfo = await browser.interpreter.InterpretRecording(
|
recording.recording, currentPage, plainRun.interpreterSettings);
|
||||||
recording.recording, currentPage, plainRun.interpreterSettings);
|
|
||||||
|
await destroyRemoteBrowser(plainRun.browserId);
|
||||||
await destroyRemoteBrowser(plainRun.browserId);
|
|
||||||
|
await run.update({
|
||||||
await run.update({
|
...run,
|
||||||
...run,
|
status: 'success',
|
||||||
status: 'success',
|
finishedAt: new Date().toLocaleString(),
|
||||||
finishedAt: new Date().toLocaleString(),
|
browserId: plainRun.browserId,
|
||||||
browserId: plainRun.browserId,
|
log: interpretationInfo.log.join('\n'),
|
||||||
log: interpretationInfo.log.join('\n'),
|
serializableOutput: interpretationInfo.serializableOutput,
|
||||||
serializableOutput: interpretationInfo.serializableOutput,
|
binaryOutput: interpretationInfo.binaryOutput,
|
||||||
binaryOutput: interpretationInfo.binaryOutput,
|
});
|
||||||
});
|
return true;
|
||||||
return true;
|
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
logger.log('info', `Error while running a recording with id: ${id} - ${error.message}`);
|
logger.log('info', `Error while running a recording with id: ${id} - ${error.message}`);
|
||||||
console.log(error.message);
|
console.log(error.message);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function createWorkflowAndStoreMetadata(id: string, userId: string) {
|
async function createWorkflowAndStoreMetadata(id: string, userId: string) {
|
||||||
if (!id) {
|
if (!id) {
|
||||||
id = uuid();
|
id = uuid();
|
||||||
}
|
}
|
||||||
|
|
||||||
const recording = await Robot.findOne({
|
const recording = await Robot.findOne({
|
||||||
where: {
|
where: {
|
||||||
'recording_meta.id': id
|
'recording_meta.id': id
|
||||||
},
|
},
|
||||||
raw: true
|
raw: true
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!recording || !recording.recording_meta || !recording.recording_meta.id) {
|
if (!recording || !recording.recording_meta || !recording.recording_meta.id) {
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
error: 'Recording not found'
|
error: 'Recording not found'
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const proxyConfig = await getDecryptedProxyConfig(userId);
|
const proxyConfig = await getDecryptedProxyConfig(userId);
|
||||||
let proxyOptions: any = {};
|
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,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function handleRunRecording(id: string, userId: string) {
|
if (proxyConfig.proxy_url) {
|
||||||
try {
|
proxyOptions = {
|
||||||
const result = await createWorkflowAndStoreMetadata(id, userId);
|
server: proxyConfig.proxy_url,
|
||||||
const { browserId, runId: newRunId } = result;
|
...(proxyConfig.proxy_username && proxyConfig.proxy_password && {
|
||||||
|
username: proxyConfig.proxy_username,
|
||||||
if (!browserId || !newRunId || !userId) {
|
password: proxyConfig.proxy_password,
|
||||||
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);
|
|
||||||
});
|
|
||||||
|
|
||||||
} catch (error: any) {
|
|
||||||
logger.error('Error running recording:', error);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
try {
|
||||||
function cleanupSocketListeners(socket: Socket, browserId: string, id: string) {
|
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,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function handleRunRecording(id: string, userId: string) {
|
||||||
|
try {
|
||||||
|
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);
|
||||||
|
});
|
||||||
|
|
||||||
|
} catch (error: any) {
|
||||||
|
logger.error('Error running recording:', error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function cleanupSocketListeners(socket: Socket, browserId: string, id: string) {
|
||||||
socket.off('ready-for-run', () => readyForRunHandler(browserId, id));
|
socket.off('ready-for-run', () => readyForRunHandler(browserId, id));
|
||||||
logger.log('info', `Cleaned up listeners for browserId: ${browserId}, runId: ${id}`);
|
logger.log('info', `Cleaned up listeners for browserId: ${browserId}, runId: ${id}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
router.post("/robots/:id/runs", requireAPIKey, async (req: Request, res: Response) => {
|
router.post("/robots/:id/runs", requireAPIKey, async (req: Request, res: Response) => {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
|
|
||||||
const response = {
|
const response = {
|
||||||
statusCode: 200,
|
statusCode: 200,
|
||||||
|
|||||||
Reference in New Issue
Block a user