feat: use robot & run model in runWorkflow
This commit is contained in:
@@ -8,19 +8,40 @@ import logger from '../../logger';
|
|||||||
import { browserPool } from "../../server";
|
import { browserPool } from "../../server";
|
||||||
import { googleSheetUpdateTasks, processGoogleSheetUpdates } from "../integrations/gsheet";
|
import { googleSheetUpdateTasks, processGoogleSheetUpdates } from "../integrations/gsheet";
|
||||||
import { getRecordingByFileName } from "../../routes/storage";
|
import { getRecordingByFileName } from "../../routes/storage";
|
||||||
|
import Robot from "../../models/Robot";
|
||||||
|
import Run from "../../models/Run";
|
||||||
|
import { getDecryptedProxyConfig } from "../../routes/proxy";
|
||||||
|
|
||||||
async function runWorkflow(fileName: string, runId: string) {
|
async function runWorkflow(id: string) {
|
||||||
if (!runId) {
|
if (!id) {
|
||||||
runId = uuid();
|
id = uuid();
|
||||||
}
|
}
|
||||||
|
|
||||||
const recording = await getRecordingByFileName(fileName);
|
const recording = await Robot.findOne({
|
||||||
|
where: {
|
||||||
|
'recording_meta.id': id
|
||||||
|
},
|
||||||
|
raw: true
|
||||||
|
});
|
||||||
|
|
||||||
if (!recording || !recording.recording_meta || !recording.recording_meta.id) {
|
if (!recording || !recording.recording_meta || !recording.recording_meta.id) {
|
||||||
logger.log('info', `Recording with name: ${fileName} not found`);
|
return {
|
||||||
return {
|
success: false,
|
||||||
success: false,
|
error: 'Recording not found'
|
||||||
error: `Recording with name: ${fileName} not found`,
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// req.user.id will not be available here :)
|
||||||
|
const proxyConfig = await getDecryptedProxyConfig(req.user.id);
|
||||||
|
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,
|
||||||
|
}),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -29,34 +50,32 @@ async function runWorkflow(fileName: string, runId: string) {
|
|||||||
browser: chromium,
|
browser: chromium,
|
||||||
launchOptions: { headless: true }
|
launchOptions: { headless: true }
|
||||||
});
|
});
|
||||||
const run_meta = {
|
|
||||||
|
const run = await Run.create({
|
||||||
status: 'Scheduled',
|
status: 'Scheduled',
|
||||||
name: fileName,
|
name: recording.recording_meta.name,
|
||||||
recordingId: recording.recording_meta.id,
|
robotId: recording.id,
|
||||||
|
robotMetaId: recording.recording_meta.id,
|
||||||
startedAt: new Date().toLocaleString(),
|
startedAt: new Date().toLocaleString(),
|
||||||
finishedAt: '',
|
finishedAt: '',
|
||||||
browserId: browserId,
|
browserId: id,
|
||||||
interpreterSettings: { maxConcurrency: 1, maxRepeats: 1, debug: true },
|
interpreterSettings: { maxConcurrency: 1, maxRepeats: 1, debug: true },
|
||||||
log: '',
|
log: '',
|
||||||
runId: runId,
|
runId: id,
|
||||||
};
|
serializableOutput: {},
|
||||||
|
binaryOutput: {},
|
||||||
|
});
|
||||||
|
|
||||||
fs.mkdirSync('../storage/runs', { recursive: true });
|
const plainRun = run.toJSON();
|
||||||
await saveFile(
|
|
||||||
`../storage/runs/${fileName}_${runId}.json`,
|
|
||||||
JSON.stringify(run_meta, null, 2)
|
|
||||||
);
|
|
||||||
|
|
||||||
logger.log('debug', `Scheduled run with name: ${fileName}_${runId}.json`);
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
browserId,
|
browserId,
|
||||||
runId
|
runId: plainRun.runId,
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
const { message } = e as Error;
|
const { message } = e as Error;
|
||||||
logger.log('info', `Error while scheduling a run with name: ${fileName}_${runId}.json`);
|
logger.log('info', `Error while scheduling a run with id: ${id}`);
|
||||||
console.log(message);
|
console.log(message);
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
|
|||||||
Reference in New Issue
Block a user