feat: pass browserId, remove try catch block

This commit is contained in:
karishmas6
2024-10-12 21:55:46 +05:30
parent 669a12bf50
commit a111976959

View File

@@ -171,6 +171,78 @@ router.get("/robots/:id/runs/:runId", requireAPIKey, async (req: Request, res: R
}
});
async function createWorkflowAndStoreMetadata(id: string, userId: string) {
try {
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,
}),
};
}
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,
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,
};
}
}
async function readyForRunHandler(browserId: string, id: string) {
try {
const interpretation = await executeRun(id);
@@ -249,78 +321,6 @@ 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);