Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pass organization id to launch browser #1000

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions skyvern/webeye/browser_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ async def check_and_fix_state(
proxy_location: ProxyLocation | None = None,
task_id: str | None = None,
workflow_run_id: str | None = None,
organization_id: str | None = None,
) -> None:
if self.pw is None:
LOG.info("Starting playwright")
Expand All @@ -225,6 +226,7 @@ async def check_and_fix_state(
proxy_location=proxy_location,
task_id=task_id,
workflow_run_id=workflow_run_id,
organization_id=organization_id,
)
self.browser_context = browser_context
self.browser_artifacts = browser_artifacts
Expand Down Expand Up @@ -312,29 +314,42 @@ async def get_or_create_page(
proxy_location: ProxyLocation | None = None,
task_id: str | None = None,
workflow_run_id: str | None = None,
organization_id: str | None = None,
) -> Page:
page = await self.get_working_page()
if page is not None:
return page

try:
await self.check_and_fix_state(
url=url, proxy_location=proxy_location, task_id=task_id, workflow_run_id=workflow_run_id
url=url,
proxy_location=proxy_location,
task_id=task_id,
workflow_run_id=workflow_run_id,
organization_id=organization_id,
)
except Exception as e:
error_message = str(e)
if "net::ERR" not in error_message:
raise e
await self.close_current_open_page()
await self.check_and_fix_state(
url=url, proxy_location=proxy_location, task_id=task_id, workflow_run_id=workflow_run_id
url=url,
proxy_location=proxy_location,
task_id=task_id,
workflow_run_id=workflow_run_id,
organization_id=organization_id,
)
await self.__assert_page()

if not await BrowserContextFactory.validate_browser_context(await self.get_working_page()):
await self.close_current_open_page()
await self.check_and_fix_state(
url=url, proxy_location=proxy_location, task_id=task_id, workflow_run_id=workflow_run_id
url=url,
proxy_location=proxy_location,
task_id=task_id,
workflow_run_id=workflow_run_id,
organization_id=organization_id,
)
await self.__assert_page()

Expand Down
23 changes: 19 additions & 4 deletions skyvern/webeye/browser_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ async def _create_browser_state(
url: str | None = None,
task_id: str | None = None,
workflow_run_id: str | None = None,
organization_id: str | None = None,
) -> BrowserState:
pw = await async_playwright().start()
(
Expand All @@ -42,6 +43,7 @@ async def _create_browser_state(
url=url,
task_id=task_id,
workflow_run_id=workflow_run_id,
organization_id=organization_id,
)
return BrowserState(
pw=pw,
Expand All @@ -64,11 +66,18 @@ async def get_or_create_for_task(self, task: Task) -> BrowserState:
return self.pages[task.task_id]

LOG.info("Creating browser state for task", task_id=task.task_id)
browser_state = await self._create_browser_state(task.proxy_location, task.url, task.task_id)
browser_state = await self._create_browser_state(
proxy_location=task.proxy_location,
url=task.url,
task_id=task.task_id,
organization_id=task.organization_id,
)

# The URL here is only used when creating a new page, and not when using an existing page.
# This will make sure browser_state.page is not None.
await browser_state.get_or_create_page(url=task.url, proxy_location=task.proxy_location, task_id=task.task_id)
await browser_state.get_or_create_page(
url=task.url, proxy_location=task.proxy_location, task_id=task.task_id, organization_id=task.organization_id
)

self.pages[task.task_id] = browser_state
if task.workflow_run_id:
Expand All @@ -83,13 +92,19 @@ async def get_or_create_for_workflow_run(self, workflow_run: WorkflowRun, url: s
workflow_run_id=workflow_run.workflow_run_id,
)
browser_state = await self._create_browser_state(
workflow_run.proxy_location, url=url, workflow_run_id=workflow_run.workflow_run_id
workflow_run.proxy_location,
url=url,
workflow_run_id=workflow_run.workflow_run_id,
organization_id=workflow_run.organization_id,
)

# The URL here is only used when creating a new page, and not when using an existing page.
# This will make sure browser_state.page is not None.
await browser_state.get_or_create_page(
url=url, proxy_location=workflow_run.proxy_location, workflow_run_id=workflow_run.workflow_run_id
url=url,
proxy_location=workflow_run.proxy_location,
workflow_run_id=workflow_run.workflow_run_id,
organization_id=workflow_run.organization_id,
)

self.pages[workflow_run.workflow_run_id] = browser_state
Expand Down
Loading