feat: pass userId to browser pool methods

This commit is contained in:
amhsirak
2025-03-06 02:41:11 +05:30
parent 591318728a
commit 36e77d1666

View File

@@ -92,7 +92,7 @@ function AddGeneratedFlags(workflow: WorkflowFile) {
return copy; return copy;
}; };
async function executeRun(id: string) { async function executeRun(id: string, userId: string) {
try { try {
const run = await Run.findOne({ where: { runId: id } }); const run = await Run.findOne({ where: { runId: id } });
if (!run) { if (!run) {
@@ -114,7 +114,7 @@ async function executeRun(id: string) {
plainRun.status = 'running'; plainRun.status = 'running';
const browser = browserPool.getRemoteBrowser(plainRun.browserId); const browser = browserPool.getRemoteBrowser(userId);
if (!browser) { if (!browser) {
throw new Error('Could not access browser'); throw new Error('Could not access browser');
} }
@@ -132,7 +132,7 @@ async function executeRun(id: string) {
const binaryOutputService = new BinaryOutputService('maxun-run-screenshots'); const binaryOutputService = new BinaryOutputService('maxun-run-screenshots');
const uploadedBinaryOutput = await binaryOutputService.uploadAndStoreBinaryOutput(run, interpretationInfo.binaryOutput); const uploadedBinaryOutput = await binaryOutputService.uploadAndStoreBinaryOutput(run, interpretationInfo.binaryOutput);
await destroyRemoteBrowser(plainRun.browserId); await destroyRemoteBrowser(userId, plainRun.browserId);
await run.update({ await run.update({
...run, ...run,
@@ -207,22 +207,22 @@ async function executeRun(id: string) {
} }
} }
async function readyForRunHandler(browserId: string, id: string) { async function readyForRunHandler(browserId: string, id: string, userId: string) {
try { try {
const interpretation = await executeRun(id); const interpretation = await executeRun(id, userId);
if (interpretation) { if (interpretation) {
logger.log('info', `Interpretation of ${id} succeeded`); logger.log('info', `Interpretation of ${id} succeeded`);
} else { } else {
logger.log('error', `Interpretation of ${id} failed`); logger.log('error', `Interpretation of ${id} failed`);
await destroyRemoteBrowser(browserId); await destroyRemoteBrowser(userId, browserId);
} }
resetRecordingState(browserId, id); resetRecordingState(browserId, id);
} catch (error: any) { } catch (error: any) {
logger.error(`Error during readyForRunHandler: ${error.message}`); logger.error(`Error during readyForRunHandler: ${error.message}`);
await destroyRemoteBrowser(browserId); await destroyRemoteBrowser(userId, browserId);
} }
} }
@@ -245,12 +245,12 @@ export async function handleRunRecording(id: string, userId: string) {
rejectUnauthorized: false rejectUnauthorized: false
}); });
socket.on('ready-for-run', () => readyForRunHandler(browserId, newRunId)); socket.on('ready-for-run', () => readyForRunHandler(browserId, newRunId, userId));
logger.log('info', `Running robot: ${id}`); logger.log('info', `Running robot: ${id}`);
socket.on('disconnect', () => { socket.on('disconnect', () => {
cleanupSocketListeners(socket, browserId, newRunId); cleanupSocketListeners(socket, browserId, newRunId, userId);
}); });
} catch (error: any) { } catch (error: any) {
@@ -258,8 +258,8 @@ export async function handleRunRecording(id: string, userId: string) {
} }
} }
function cleanupSocketListeners(socket: Socket, browserId: string, id: string) { function cleanupSocketListeners(socket: Socket, browserId: string, id: string, userId: string) {
socket.off('ready-for-run', () => readyForRunHandler(browserId, id)); socket.off('ready-for-run', () => readyForRunHandler(browserId, id, userId));
logger.log('info', `Cleaned up listeners for browserId: ${browserId}, runId: ${id}`); logger.log('info', `Cleaned up listeners for browserId: ${browserId}, runId: ${id}`);
} }