From ff2a3da100c4cfbfb77affb3430dbcd18586e030 Mon Sep 17 00:00:00 2001 From: "2569718930@qq.com" <2569718930@qq.com> Date: Mon, 1 Jun 2026 00:30:25 +0800 Subject: [PATCH] Use longer timeout for scan prewarm --- tests/test_deployment_runtime_config.py | 8 ++++++++ tests/test_web_observability.py | 7 ++++--- web/scan_terminal_service.py | 18 ++++++++++++++---- web/services/scan_terminal_config.py | 6 ++++++ 4 files changed, 32 insertions(+), 7 deletions(-) diff --git a/tests/test_deployment_runtime_config.py b/tests/test_deployment_runtime_config.py index 281cae58..d42cecc1 100644 --- a/tests/test_deployment_runtime_config.py +++ b/tests/test_deployment_runtime_config.py @@ -72,7 +72,15 @@ def test_scan_terminal_backend_timeout_returns_before_next_proxy_abort(): assert 'POLYWEATHER_SCAN_TERMINAL_PROXY_TIMEOUT_MS || "18000"' in route_source assert '"POLYWEATHER_SCAN_TERMINAL_BUILD_TIMEOUT_SEC",\n 10,' in config_source + assert ( + '"POLYWEATHER_SCAN_TERMINAL_PREWARM_PAYLOAD_TIMEOUT_SEC",\n 30,' + in config_source + ) assert scan_terminal_config.SCAN_TERMINAL_BUILD_TIMEOUT_SEC <= 30 + assert ( + scan_terminal_config.SCAN_TERMINAL_PREWARM_PAYLOAD_TIMEOUT_SEC + >= scan_terminal_config.SCAN_TERMINAL_BUILD_TIMEOUT_SEC + ) def test_probability_engine_uses_enriched_multi_model_snapshot(): diff --git a/tests/test_web_observability.py b/tests/test_web_observability.py index 76746238..4b69ca4c 100644 --- a/tests/test_web_observability.py +++ b/tests/test_web_observability.py @@ -2301,8 +2301,8 @@ def test_scan_terminal_timeout_does_not_replace_better_cached_snapshot(monkeypat def test_scan_terminal_prewarm_builds_default_terminal_payload(monkeypatch): calls = [] - def _fake_build(filters, *, force_refresh=False): - calls.append((dict(filters), force_refresh)) + def _fake_build(filters, *, force_refresh=False, timeout_sec=None): + calls.append((dict(filters), force_refresh, timeout_sec)) return {"rows": []} monkeypatch.setattr( @@ -2312,8 +2312,9 @@ def test_scan_terminal_prewarm_builds_default_terminal_payload(monkeypatch): ) assert scan_terminal_service._warm_scan_terminal_payloads() == 1 - filters, force_refresh = calls[0] + filters, force_refresh, timeout_sec = calls[0] assert force_refresh is False + assert timeout_sec == scan_terminal_service.SCAN_TERMINAL_PREWARM_PAYLOAD_TIMEOUT_SEC assert filters["scan_mode"] == "tradable" assert filters["min_price"] == 0.05 assert filters["max_price"] == 0.95 diff --git a/web/scan_terminal_service.py b/web/scan_terminal_service.py index e713e826..471693fc 100644 --- a/web/scan_terminal_service.py +++ b/web/scan_terminal_service.py @@ -16,6 +16,7 @@ from web.services.scan_terminal_config import ( SCAN_TERMINAL_BUILD_TIMEOUT_SEC, SCAN_TERMINAL_MAX_WORKERS, SCAN_TERMINAL_PAYLOAD_TTL_SEC, + SCAN_TERMINAL_PREWARM_PAYLOAD_TIMEOUT_SEC, ) from src.data_collection.city_registry import ALIASES from web.scan_terminal_cache import ( @@ -101,11 +102,17 @@ def _build_scan_terminal_payload_uncached( filters: Dict[str, Any], *, force_refresh: bool = False, + timeout_sec: Optional[float] = None, ) -> Dict[str, Any]: cached_entry = get_scan_terminal_cache_entry(filters) or {} try: city_names = list(CITIES.keys()) + build_timeout_sec = float( + timeout_sec + if timeout_sec is not None + else SCAN_TERMINAL_BUILD_TIMEOUT_SEC + ) timezone_offset = filters.get("timezone_offset_seconds") if timezone_offset is not None: target_tz = int(timezone_offset) @@ -145,7 +152,7 @@ def _build_scan_terminal_payload_uncached( try: completed = as_completed( future_map, - timeout=float(SCAN_TERMINAL_BUILD_TIMEOUT_SEC), + timeout=build_timeout_sec, ) for future in completed: city_name = future_map[future] @@ -160,8 +167,7 @@ def _build_scan_terminal_payload_uncached( except FutureTimeoutError: timed_out = True timeout_message = ( - f"scan terminal build timed out after " - f"{SCAN_TERMINAL_BUILD_TIMEOUT_SEC}s" + f"scan terminal build timed out after {build_timeout_sec:g}s" ) failed_reasons.append(timeout_message) for future, city_name in future_map.items(): @@ -361,7 +367,11 @@ def _warm_scan_terminal_payloads() -> int: ok = 0 for filters in _scan_terminal_prewarm_filters(): try: - _build_scan_terminal_payload_uncached(filters, force_refresh=False) + _build_scan_terminal_payload_uncached( + filters, + force_refresh=False, + timeout_sec=SCAN_TERMINAL_PREWARM_PAYLOAD_TIMEOUT_SEC, + ) ok += 1 except Exception as exc: logger.warning("scan terminal payload pre-warm failed: {}", exc) diff --git a/web/services/scan_terminal_config.py b/web/services/scan_terminal_config.py index 7dc0f46f..9a7ac97d 100644 --- a/web/services/scan_terminal_config.py +++ b/web/services/scan_terminal_config.py @@ -35,6 +35,12 @@ SCAN_TERMINAL_BUILD_TIMEOUT_SEC = _env_int( min_value=8, max_value=30, ) +SCAN_TERMINAL_PREWARM_PAYLOAD_TIMEOUT_SEC = _env_int( + "POLYWEATHER_SCAN_TERMINAL_PREWARM_PAYLOAD_TIMEOUT_SEC", + 30, + min_value=10, + max_value=120, +) SCAN_TERMINAL_MAX_WORKERS = _env_int( "POLYWEATHER_SCAN_TERMINAL_MAX_WORKERS", 8,