Skip to content
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
43 changes: 4 additions & 39 deletions stagehand/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,15 @@ async def init(self):
timeout=self.timeout_settings
)

# Check server health
await self._check_server_health()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why? if the URL is wrong or the server is not available, the python client should notify the user, no?


# Create session if we don't have one
if not self.session_id:
await self._create_session()
self._log(f"Created new session: {self.session_id}", level=3)

###
# TODO: throw log for unauthorized (401) key not whitelisted
###

# Start Playwright and connect to remote
self._log("Starting Playwright...", level=3)
self._playwright = await async_playwright().start()
Expand Down Expand Up @@ -237,42 +238,6 @@ async def close(self):

self._closed = True

async def _check_server_health(self, timeout: int = 10):
"""
Ping /healthcheck to verify the server is available.
Uses exponential backoff for retries.
"""
start = time.time()
attempt = 0
while True:
try:
client = self.httpx_client or httpx.AsyncClient(
timeout=self.timeout_settings
)
async with client:
headers = {
"x-bb-api-key": self.browserbase_api_key,
}
resp = await client.get(
f"{self.server_url}/healthcheck", headers=headers
)
if resp.status_code == 200:
data = resp.json()
if data.get("status") == "ok":
self._log("Healthcheck passed. Server is running.", level=3)
return
except Exception as e:
self._log(f"Healthcheck error: {str(e)}", level=3)

if time.time() - start > timeout:
raise TimeoutError(f"Server not responding after {timeout} seconds.")

wait_time = min(
2**attempt * 0.5, 5.0
) # Exponential backoff, capped at 5 seconds
await asyncio.sleep(wait_time)
attempt += 1

async def _create_session(self):
"""
Create a new session by calling /sessions/start on the server.
Expand Down
36 changes: 4 additions & 32 deletions stagehand/sync/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,15 @@ def init(self):
if not self._client:
self._client = requests.Session()

# Check server health
self._check_server_health()

# Create session if we don't have one
if not self.session_id:
self._create_session()
self._log(f"Created new session: {self.session_id}", level=3)

###
# TODO: throw log for unauthorized (401) key not whitelisted
###

# Start Playwright and connect to remote
self._log("Starting Playwright...", level=3)
self._playwright = sync_playwright().start()
Expand Down Expand Up @@ -152,35 +153,6 @@ def close(self):

self._closed = True

def _check_server_health(self, timeout: int = 10):
"""
Check server health synchronously with exponential backoff.
"""
start = time.time()
attempt = 0
while True:
try:
headers = {
"x-bb-api-key": self.browserbase_api_key,
}
resp = self._client.get(
f"{self.server_url}/healthcheck", headers=headers
)
if resp.status_code == 200:
data = resp.json()
if data.get("status") == "ok":
self._log("Healthcheck passed. Server is running.", level=3)
return
except Exception as e:
self._log(f"Healthcheck error: {str(e)}", level=3)

if time.time() - start > timeout:
raise TimeoutError(f"Server not responding after {timeout} seconds.")

wait_time = min(2**attempt * 0.5, 5.0)
time.sleep(wait_time)
attempt += 1

def _create_session(self):
"""
Create a new session synchronously.
Expand Down