chore(home): mirror from kalinamall (9883e6a) with papatramp URLs
This commit is contained in:
@@ -0,0 +1 @@
|
||||
"""Фоновые задачи SAC (retention и др.)."""
|
||||
@@ -0,0 +1,45 @@
|
||||
"""CLI: python -m app.jobs.daily_report [--force]"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import logging
|
||||
import sys
|
||||
|
||||
from app.database import SessionLocal
|
||||
from app.services.daily_report import run_daily_reports
|
||||
|
||||
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
|
||||
logger = logging.getLogger("sac.daily_report")
|
||||
|
||||
|
||||
def main(argv: list[str] | None = None) -> int:
|
||||
parser = argparse.ArgumentParser(description="Generate SAC daily reports from DB events")
|
||||
parser.add_argument(
|
||||
"--force",
|
||||
action="store_true",
|
||||
help="Ignore SAC_DAILY_REPORT_HOUR (for manual run / tests)",
|
||||
)
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
db = SessionLocal()
|
||||
try:
|
||||
results = run_daily_reports(db, force=args.force)
|
||||
created = [r for r in results if r.created]
|
||||
logger.info("created %s reports", len(created))
|
||||
for r in created:
|
||||
logger.info(" %s %s event_id=%s", r.hostname, r.report_type, r.event_id)
|
||||
skipped = [r for r in results if not r.created and r.skipped_reason]
|
||||
for r in skipped:
|
||||
logger.info(" skip %s: %s", r.hostname, r.skipped_reason)
|
||||
return 0
|
||||
except Exception:
|
||||
db.rollback()
|
||||
logger.exception("daily report job failed")
|
||||
return 1
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
@@ -0,0 +1,60 @@
|
||||
"""Фоновый asyncio-цикл сканирования stale heartbeat (в процессе sac-api)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
|
||||
from app.config import get_settings
|
||||
from app.database import SessionLocal
|
||||
from app.services.host_silence_scan import dispatch_host_silence_notifications, run_host_silence_scan
|
||||
|
||||
logger = logging.getLogger("sac.host_silence_scan")
|
||||
|
||||
|
||||
async def host_silence_scan_loop(stop_event: asyncio.Event) -> None:
|
||||
settings = get_settings()
|
||||
interval_sec = max(60, int(settings.sac_host_silence_scan_interval_minutes) * 60)
|
||||
logger.info(
|
||||
"host_silence background scan enabled (interval=%s min, stale=%s min)",
|
||||
settings.sac_host_silence_scan_interval_minutes,
|
||||
settings.sac_heartbeat_stale_minutes,
|
||||
)
|
||||
while not stop_event.is_set():
|
||||
try:
|
||||
await asyncio.to_thread(_run_scan_once)
|
||||
except Exception:
|
||||
logger.exception("host_silence background scan error")
|
||||
try:
|
||||
await asyncio.wait_for(stop_event.wait(), timeout=interval_sec)
|
||||
except asyncio.TimeoutError:
|
||||
continue
|
||||
|
||||
|
||||
def _run_scan_once() -> None:
|
||||
db = SessionLocal()
|
||||
try:
|
||||
results = run_host_silence_scan(db)
|
||||
notified = dispatch_host_silence_notifications(db, results)
|
||||
from app.services.agent_update import process_agent_update_fallbacks
|
||||
|
||||
fallback_results = process_agent_update_fallbacks(db)
|
||||
db.commit()
|
||||
if results:
|
||||
logger.info(
|
||||
"host_silence background: stale=%s created=%s notified=%s",
|
||||
len(results),
|
||||
sum(1 for r in results if r.created),
|
||||
notified,
|
||||
)
|
||||
if fallback_results:
|
||||
logger.info(
|
||||
"agent_update fallback: processed=%s ok=%s",
|
||||
len(fallback_results),
|
||||
sum(1 for r in fallback_results if r.get("ok")),
|
||||
)
|
||||
except Exception:
|
||||
db.rollback()
|
||||
raise
|
||||
finally:
|
||||
db.close()
|
||||
@@ -0,0 +1,38 @@
|
||||
"""CLI: python -m app.jobs.host_silence_scan"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import sys
|
||||
|
||||
from app.database import SessionLocal
|
||||
from app.services.host_silence_scan import dispatch_host_silence_notifications, run_host_silence_scan
|
||||
|
||||
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
|
||||
logger = logging.getLogger("sac.host_silence_scan")
|
||||
|
||||
|
||||
def main() -> int:
|
||||
db = SessionLocal()
|
||||
try:
|
||||
results = run_host_silence_scan(db)
|
||||
notified = dispatch_host_silence_notifications(db, results)
|
||||
db.commit()
|
||||
created = sum(1 for r in results if r.created)
|
||||
logger.info(
|
||||
"host_silence scan done stale_hosts=%s created=%s notified=%s",
|
||||
len(results),
|
||||
created,
|
||||
notified,
|
||||
)
|
||||
return 0
|
||||
except Exception:
|
||||
db.rollback()
|
||||
logger.exception("host_silence scan failed")
|
||||
return 1
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
@@ -0,0 +1,36 @@
|
||||
"""CLI: python -m app.jobs.retention"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import sys
|
||||
|
||||
from app.config import get_settings
|
||||
from app.database import SessionLocal
|
||||
from app.services.retention import purge_old_data
|
||||
|
||||
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
|
||||
logger = logging.getLogger("sac.retention")
|
||||
|
||||
|
||||
def main() -> int:
|
||||
settings = get_settings()
|
||||
db = SessionLocal()
|
||||
try:
|
||||
stats = purge_old_data(db, settings)
|
||||
logger.info(
|
||||
"retention done events_deleted=%s problems_deleted=%s",
|
||||
stats["events_deleted"],
|
||||
stats["problems_deleted"],
|
||||
)
|
||||
return 0
|
||||
except Exception:
|
||||
db.rollback()
|
||||
logger.exception("retention failed")
|
||||
return 1
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Reference in New Issue
Block a user