|
| 1 | +"""Scheduled periodic scans of installed HACS components.""" |
| 2 | + |
| 3 | +import asyncio |
| 4 | +import logging |
| 5 | +from datetime import datetime |
| 6 | + |
| 7 | +log = logging.getLogger(__name__) |
| 8 | + |
| 9 | +_task: asyncio.Task | None = None |
| 10 | +_interval_hours: float = 0 |
| 11 | +_enabled: bool = False |
| 12 | + |
| 13 | + |
| 14 | +async def _scheduled_loop(): |
| 15 | + """Run periodic scan of installed HACS components.""" |
| 16 | + from app.scanner.hacs_list import fetch_installed_hacs, repo_to_url |
| 17 | + from app.scanner.pipeline import run_scan |
| 18 | + from app import storage |
| 19 | + from app.report.mqtt import publish_status |
| 20 | + |
| 21 | + while True: |
| 22 | + await asyncio.sleep(_interval_hours * 3600) |
| 23 | + if not _enabled: |
| 24 | + continue |
| 25 | + |
| 26 | + log.info("Scheduled scan starting (interval=%gh)", _interval_hours) |
| 27 | + publish_status("scheduled_scan") |
| 28 | + |
| 29 | + try: |
| 30 | + installed = await fetch_installed_hacs() |
| 31 | + if not installed: |
| 32 | + log.warning("Scheduled scan: no HACS components found") |
| 33 | + continue |
| 34 | + |
| 35 | + scanned = 0 |
| 36 | + for comp in installed: |
| 37 | + url = repo_to_url(comp.get("full_name", "")) |
| 38 | + if not url: |
| 39 | + continue |
| 40 | + name = comp.get("name", comp.get("full_name", "")) |
| 41 | + job_id = f"sched:{name}" |
| 42 | + storage.create_job(job_id, name, url, batch_id="scheduled") |
| 43 | + try: |
| 44 | + await run_scan(url, name) |
| 45 | + storage.complete_job(job_id) |
| 46 | + scanned += 1 |
| 47 | + except Exception as e: |
| 48 | + storage.fail_job(job_id, str(e)) |
| 49 | + log.warning("Scheduled scan failed for %s: %s", name, e) |
| 50 | + |
| 51 | + log.info("Scheduled scan done: %d/%d components scanned", scanned, len(installed)) |
| 52 | + publish_status("idle") |
| 53 | + storage.cleanup_repo_cache() |
| 54 | + |
| 55 | + except Exception as e: |
| 56 | + log.exception("Scheduled scan loop error: %s", e) |
| 57 | + publish_status("idle") |
| 58 | + |
| 59 | + |
| 60 | +def start(interval_hours: float = 24): |
| 61 | + """Start the scheduled scan loop.""" |
| 62 | + global _task, _interval_hours, _enabled |
| 63 | + if interval_hours <= 0: |
| 64 | + log.info("Scheduled scans disabled (interval=0)") |
| 65 | + return |
| 66 | + |
| 67 | + _interval_hours = interval_hours |
| 68 | + _enabled = True |
| 69 | + |
| 70 | + if _task and not _task.done(): |
| 71 | + log.info("Scheduler already running, updating interval to %gh", interval_hours) |
| 72 | + return |
| 73 | + |
| 74 | + _task = asyncio.create_task(_scheduled_loop()) |
| 75 | + log.info("Scheduled scans enabled: every %gh", interval_hours) |
| 76 | + |
| 77 | + |
| 78 | +def stop(): |
| 79 | + """Stop the scheduled scan loop.""" |
| 80 | + global _task, _enabled |
| 81 | + _enabled = False |
| 82 | + if _task and not _task.done(): |
| 83 | + _task.cancel() |
| 84 | + _task = None |
| 85 | + log.info("Scheduled scans disabled") |
| 86 | + |
| 87 | + |
| 88 | +def status() -> dict: |
| 89 | + """Return scheduler status.""" |
| 90 | + return { |
| 91 | + "enabled": _enabled, |
| 92 | + "interval_hours": _interval_hours, |
| 93 | + "running": _task is not None and not _task.done() if _task else False, |
| 94 | + } |
0 commit comments