# -*- coding: utf-8 -*- """Background indexer worker — cicli separati Paperless / Gitea / Apps.""" import logging import os import time logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(name)s %(message)s") LOGGER = logging.getLogger("loogle_mcp.worker") from app.db import init_db # noqa: E402 from app.knowledge import apps_indexer, gitea_indexer, indexer # noqa: E402 def _run_gitea() -> None: if os.environ.get("GITEA_INDEX_ENABLED", "yes").strip().lower() in ("0", "false", "no", "off"): LOGGER.info("Indicizzazione Gitea disabilitata (GITEA_INDEX_ENABLED)") return gitea_result = gitea_indexer.index_all() stats = gitea_indexer.index_stats() LOGGER.info("Indicizzazione Gitea completata: %s stats=%s", gitea_result, stats) def _run_paperless() -> None: result = indexer.index_all(max_pages=10) LOGGER.info("Indicizzazione Paperless completata: %s", result) def _run_apps() -> None: apps_result = apps_indexer.index_all() LOGGER.info("Indicizzazione Apps (P7) completata: %s", apps_result) def main() -> None: init_db() interval = int(os.environ.get("INDEXER_INTERVAL_MINUTES", "30")) * 60 LOGGER.info("Indexer avviato, intervallo %ds", interval) from app.knowledge import thermal # noqa: WPS433 status = thermal.read_status() if status: LOGGER.info( "Thermal probe OK: temp=%.1f°C load1=%.2f nproc=%s source=%s (hard=%.0f°C soft=%.0f°C cpu_target=%.0f%%)", float(status.get("cpu_temp_c") or 0), float(status.get("load1") or 0), status.get("nproc"), status.get("source"), thermal.hard_temp_c(), thermal.soft_temp_c(), thermal.cpu_target_pct(), ) else: LOGGER.warning("Thermal probe non raggiungibile all'avvio — gate userà delay conservativi") # Gitea per primo: repo piccoli, non bloccato da Paperless lento try: thermal.wait_for_headroom(context="cycle:gitea-boot") _run_gitea() except Exception as exc: LOGGER.error("Indicizzazione Gitea fallita: %s", exc) while True: try: thermal.wait_for_headroom(context="cycle:gitea") _run_gitea() except Exception as exc: LOGGER.error("Indicizzazione Gitea fallita: %s", exc) try: thermal.wait_for_headroom(context="cycle:paperless") _run_paperless() except Exception as exc: LOGGER.error("Indicizzazione Paperless fallita: %s", exc) try: thermal.wait_for_headroom(context="cycle:apps") _run_apps() except Exception as exc: LOGGER.error("Indicizzazione Apps fallita: %s", exc) time.sleep(interval) if __name__ == "__main__": main()