Update app.py
Browse files
app.py
CHANGED
|
@@ -214,6 +214,21 @@ def get_task_status(api_base_url: str, api_key: str, task_id: str) -> Dict[str,
|
|
| 214 |
return response.json()
|
| 215 |
|
| 216 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 217 |
def cancel_remote_task(api_base_url: str, api_key: str, task_state: Dict[str, Any]) -> Tuple[str, Dict[str, Any]]:
|
| 218 |
task_id = (task_state or {}).get("task_id")
|
| 219 |
if not task_id:
|
|
@@ -266,6 +281,10 @@ def run_remote_task(
|
|
| 266 |
except Exception as exc:
|
| 267 |
raise gr.Error(f"Failed to query task status: {exc}") from exc
|
| 268 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 269 |
image_url = task.get("display_image_url") or task.get("latest_annotated_screenshot_url") or task.get("latest_screenshot_url")
|
| 270 |
if image_url and image_url != last_image_url:
|
| 271 |
try:
|
|
|
|
| 214 |
return response.json()
|
| 215 |
|
| 216 |
|
| 217 |
+
def send_heartbeat(api_base_url: str, api_key: str, task_id: str) -> None:
|
| 218 |
+
"""Notify the ECS server that the frontend is still alive.
|
| 219 |
+
|
| 220 |
+
Swallows all errors so a transient network blip never kills the UI loop.
|
| 221 |
+
"""
|
| 222 |
+
try:
|
| 223 |
+
get_http_session().post(
|
| 224 |
+
f"{normalize_base_url(api_base_url)}/tasks/{task_id}/heartbeat",
|
| 225 |
+
headers=build_headers(api_key),
|
| 226 |
+
timeout=5,
|
| 227 |
+
)
|
| 228 |
+
except Exception:
|
| 229 |
+
pass
|
| 230 |
+
|
| 231 |
+
|
| 232 |
def cancel_remote_task(api_base_url: str, api_key: str, task_state: Dict[str, Any]) -> Tuple[str, Dict[str, Any]]:
|
| 233 |
task_id = (task_state or {}).get("task_id")
|
| 234 |
if not task_id:
|
|
|
|
| 281 |
except Exception as exc:
|
| 282 |
raise gr.Error(f"Failed to query task status: {exc}") from exc
|
| 283 |
|
| 284 |
+
# Keep the ECS heartbeat watchdog satisfied so it doesn't auto-cancel
|
| 285 |
+
# this task if it thinks the frontend has disconnected.
|
| 286 |
+
send_heartbeat(api_base_url, api_key, task_id)
|
| 287 |
+
|
| 288 |
image_url = task.get("display_image_url") or task.get("latest_annotated_screenshot_url") or task.get("latest_screenshot_url")
|
| 289 |
if image_url and image_url != last_image_url:
|
| 290 |
try:
|