62 lines
2.5 KiB
Python
62 lines
2.5 KiB
Python
"""Daily cron: SSH preflight on offline servers; optional Telegram offline report."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import logging
|
|
from datetime import datetime, timezone
|
|
|
|
from server.application.services.offline_daily_report_schedule import (
|
|
_cron_expr,
|
|
_enabled,
|
|
_health_check_enabled,
|
|
run_scheduled_offline_daily_report,
|
|
run_scheduled_offline_preflight,
|
|
)
|
|
from server.background.schedule_runner import _cron_match
|
|
|
|
logger = logging.getLogger("nexus.offline_daily_report_loop")
|
|
|
|
CHECK_INTERVAL_SECONDS = 60
|
|
|
|
|
|
async def offline_daily_report_loop() -> None:
|
|
"""Primary worker: SSH preflight and/or Telegram report when cron matches (Beijing)."""
|
|
while True:
|
|
try:
|
|
now = datetime.now(timezone.utc)
|
|
if _cron_match(_cron_expr(), now):
|
|
preflight_stats: dict[str, int] | None = None
|
|
if _health_check_enabled():
|
|
preflight_result = await run_scheduled_offline_preflight()
|
|
if preflight_result.get("action") == "checked":
|
|
preflight_stats = preflight_result.get("preflight")
|
|
logger.info(
|
|
"Offline SSH preflight: checked=%s online=%s offline=%s",
|
|
preflight_stats.get("checked") if preflight_stats else 0,
|
|
preflight_stats.get("online") if preflight_stats else 0,
|
|
preflight_stats.get("offline") if preflight_stats else 0,
|
|
)
|
|
elif preflight_result.get("reason") not in ("already_ran_today",):
|
|
logger.debug("Offline SSH preflight: %s", preflight_result)
|
|
|
|
if _enabled():
|
|
result = await run_scheduled_offline_daily_report(
|
|
preflight=preflight_stats,
|
|
)
|
|
if result.get("action") == "sent":
|
|
logger.info(
|
|
"Offline daily report: monitored=%s offline=%s",
|
|
result.get("monitored"),
|
|
result.get("offline"),
|
|
)
|
|
elif result.get("reason") not in ("already_ran_today",):
|
|
logger.debug("Offline daily report: %s", result)
|
|
|
|
await asyncio.sleep(CHECK_INTERVAL_SECONDS)
|
|
except asyncio.CancelledError:
|
|
raise
|
|
except Exception:
|
|
logger.exception("Offline daily report loop error")
|
|
await asyncio.sleep(CHECK_INTERVAL_SECONDS)
|