Poll loop now deletes DB rows for players no longer returned by the Yodeck API, and Zabbix sync deletes the corresponding hosts from the Yodeck Players group. Both actions are reflected in the activity log. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
45 lines
1.4 KiB
Python
45 lines
1.4 KiB
Python
import logging
|
|
from apscheduler.schedulers.background import BackgroundScheduler
|
|
from app import database as db
|
|
from app import yodeck as yd
|
|
from app.zabbix import sync_to_zabbix
|
|
from app.config import YODECK_POLL_INTERVAL_MINUTES
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
def poll_yodeck():
|
|
log.info("Polling Yodeck API …")
|
|
try:
|
|
players = yd.get_all_screens()
|
|
for p in players:
|
|
db.upsert_player(p)
|
|
current_ids = [p['id'] for p in players]
|
|
removed = db.delete_players_not_in(current_ids)
|
|
msg = f"Fetched {len(players)} players from Yodeck API"
|
|
if removed:
|
|
msg += f", removed {removed} deleted player(s)"
|
|
db.add_log('yodeck_fetch', msg, {'count': len(players), 'removed': removed})
|
|
log.info(msg)
|
|
# Sync host list to Zabbix after a successful fetch
|
|
sync_to_zabbix(players, db.add_log)
|
|
except Exception as exc:
|
|
msg = f"Yodeck poll failed: {exc}"
|
|
db.add_log('error', msg)
|
|
log.error(msg)
|
|
|
|
|
|
def start_scheduler():
|
|
scheduler = BackgroundScheduler(daemon=True)
|
|
scheduler.add_job(
|
|
poll_yodeck,
|
|
'interval',
|
|
minutes=YODECK_POLL_INTERVAL_MINUTES,
|
|
id='yodeck_poll',
|
|
)
|
|
scheduler.start()
|
|
log.info("Scheduler started — Yodeck poll every %d minutes", YODECK_POLL_INTERVAL_MINUTES)
|
|
# Run an initial poll immediately so the DB is populated at startup
|
|
poll_yodeck()
|
|
return scheduler
|