Spaces:
Running
Running
| """Bootstrap P0: shim LangChain, katalog WYSZUKIWARKA, routery — minimalna ingerencja w server.py.""" | |
| from __future__ import annotations | |
| import logging | |
| import core.langchain_compat # noqa: F401 | |
| logger = logging.getLogger(__name__) | |
| def init_patches() -> None: | |
| from core.search.grant_search_catalog import apply_search_patches | |
| from core.match.apply_phase1_patches import apply_match_phase1_patches | |
| apply_search_patches() | |
| apply_match_phase1_patches() | |
| async def ensure_catalog_on_empty_db() -> None: | |
| import os | |
| from core.subscription.db import grant_count_with_retry, init_models, refresh_engine_pool | |
| init_models() | |
| from core.grants.sync_service import sync_wyszukiwarka_catalog, sync_grants_to_db | |
| try: | |
| count = grant_count_with_retry() | |
| force = os.environ.get("FORCE_WYSZUKIWARKA_REIMPORT", "").lower() in ("1", "true", "yes") | |
| if count == 0 or force: | |
| logger.info("[Bootstrap] Import WYSZUKIWARKA (count=%s, force=%s)...", count, force) | |
| await sync_wyszukiwarka_catalog() | |
| refresh_engine_pool() | |
| count = grant_count_with_retry() | |
| if count == 0: | |
| logger.info("[Bootstrap] WYSZUKIWARKA niedostępna — live scrape...") | |
| await sync_grants_to_db() | |
| refresh_engine_pool() | |
| count = grant_count_with_retry() | |
| logger.info("[Bootstrap] Baza naborów: %s rekordów.", count) | |
| except Exception as e: | |
| logger.error("[Bootstrap] Błąd inicjalizacji katalogu: %s", e) | |