myYouTube/backend/app/services/scheduler.py

35 lines
997 B
Python
Raw Permalink Normal View History

import logging
from apscheduler.schedulers.background import BackgroundScheduler
from app.config import settings
from app.db import SessionLocal
from app.services import sync
logger = logging.getLogger(__name__)
def _run_subscriptions_sync() -> None:
db = SessionLocal()
try:
sync.sync_subscriptions(db)
except sync.SyncInProgress:
logger.info("Scheduled subscriptions sync skipped: already running")
except Exception:
logger.exception("Scheduled subscriptions sync failed")
finally:
db.close()
def create_scheduler() -> BackgroundScheduler:
# Videos sync is not scheduled anymore: it runs on user activity via
# services.sync_trigger.maybe_trigger_videos_sync (see require_session).
scheduler = BackgroundScheduler(timezone="UTC")
scheduler.add_job(
_run_subscriptions_sync,
"interval",
hours=settings.subscriptions_sync_interval_hours,
id="subscriptions_sync",
)
return scheduler