51 lines
1.3 KiB
Python
51 lines
1.3 KiB
Python
|
|
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 _run_videos_sync() -> None:
|
||
|
|
db = SessionLocal()
|
||
|
|
try:
|
||
|
|
sync.sync_videos(db)
|
||
|
|
except sync.SyncInProgress:
|
||
|
|
logger.info("Scheduled videos sync skipped: already running")
|
||
|
|
except Exception:
|
||
|
|
logger.exception("Scheduled videos sync failed")
|
||
|
|
finally:
|
||
|
|
db.close()
|
||
|
|
|
||
|
|
|
||
|
|
def create_scheduler() -> BackgroundScheduler:
|
||
|
|
scheduler = BackgroundScheduler(timezone="UTC")
|
||
|
|
scheduler.add_job(
|
||
|
|
_run_subscriptions_sync,
|
||
|
|
"interval",
|
||
|
|
hours=settings.subscriptions_sync_interval_hours,
|
||
|
|
id="subscriptions_sync",
|
||
|
|
)
|
||
|
|
scheduler.add_job(
|
||
|
|
_run_videos_sync,
|
||
|
|
"interval",
|
||
|
|
minutes=settings.videos_sync_interval_minutes,
|
||
|
|
id="videos_sync",
|
||
|
|
)
|
||
|
|
return scheduler
|