(id: string, userId: string)
| 730 | }; |
| 731 | |
| 732 | async function executeRun(id: string, userId: string) { |
| 733 | let browser: any = null; |
| 734 | |
| 735 | try { |
| 736 | const run = await Run.findOne({ where: { runId: id } }); |
| 737 | if (!run) { |
| 738 | return { |
| 739 | success: false, |
| 740 | error: 'Run not found' |
| 741 | }; |
| 742 | } |
| 743 | |
| 744 | const plainRun = run.toJSON(); |
| 745 | |
| 746 | if (run.status === 'aborted' || run.status === 'aborting') { |
| 747 | logger.log('info', `API Run ${id} has status ${run.status}, skipping execution`); |
| 748 | return { success: true }; |
| 749 | } |
| 750 | |
| 751 | if (run.status === 'queued') { |
| 752 | logger.log('info', `API Run ${id} has status 'queued', skipping stale execution - will be handled by recovery`); |
| 753 | return { success: true }; |
| 754 | } |
| 755 | |
| 756 | const retryCount = plainRun.retryCount || 0; |
| 757 | if (retryCount >= 3) { |
| 758 | logger.log('warn', `API Run ${id} has exceeded max retries (${retryCount}/3), marking as failed`); |
| 759 | await run.update({ |
| 760 | status: 'failed', |
| 761 | finishedAt: new Date().toLocaleString(), |
| 762 | log: `Max retries exceeded (${retryCount}/3) - Run permanently failed` |
| 763 | }); |
| 764 | return { success: false, error: 'Max retries exceeded' }; |
| 765 | } |
| 766 | const requestedFormats = run.interpreterSettings.formats; |
| 767 | const promptInstructionsOverride = run.interpreterSettings.promptInstructions; |
| 768 | |
| 769 | const recording = await Robot.findOne({ where: { 'recording_meta.id': plainRun.robotMetaId }, raw: true }); |
| 770 | if (!recording) { |
| 771 | return { |
| 772 | success: false, |
| 773 | error: 'Recording not found' |
| 774 | }; |
| 775 | } |
| 776 | |
| 777 | browser = browserPool.getRemoteBrowser(plainRun.browserId); |
| 778 | if (!browser) { |
| 779 | throw new Error('Could not access browser'); |
| 780 | } |
| 781 | |
| 782 | let currentPage = await browser.getCurrentPage(); |
| 783 | if (!currentPage) { |
| 784 | throw new Error('Could not create a new page'); |
| 785 | } |
| 786 | |
| 787 | const robotType = (recording.recording_meta as any)?.type || (recording.recording_meta as any)?.robotType || 'extract'; |
| 788 | |
| 789 | if (robotType === 'scrape') { |
no test coverage detected