Add scheduled watchlist refresh support

This commit is contained in:
Dymas
2026-05-17 20:38:16 +02:00
parent 47480efa30
commit 6168acb272
8 changed files with 285 additions and 16 deletions
+43 -8
View File
@@ -57,7 +57,7 @@ from app_support import (
write_json,
)
from http_handler import HandlerContext, build_handler_class, server_host, server_port
from queue_jobs import DownloadQueue, WatchlistRefreshJobs
from queue_jobs import DownloadQueue, WatchlistAutoRefreshScheduler, WatchlistRefreshJobs
from title_matching import (
base_title_variants,
normalize_title_key,
@@ -82,6 +82,7 @@ CONFIG = None
WATCHLIST = None
DOWNLOAD_QUEUE = None
WATCHLIST_REFRESH = None
WATCHLIST_AUTO_REFRESH = None
def graph_request(query, variables, timeout=25):
@@ -1780,10 +1781,14 @@ def get_config_snapshot(fallback=None):
def save_runtime_config(config):
normalized = normalize_config(config)
scheduler = None
with RUNTIME_LOCK:
global CONFIG
CONFIG = dict(normalized)
write_json(CONFIG_PATH, CONFIG)
scheduler = WATCHLIST_AUTO_REFRESH
if scheduler is not None:
scheduler.notify_config_changed()
return dict(CONFIG)
@@ -1793,6 +1798,7 @@ def runtime_state():
"watchlist": WATCHLIST,
"download_queue": DOWNLOAD_QUEUE,
"watchlist_refresh": WATCHLIST_REFRESH,
"watchlist_auto_refresh": WATCHLIST_AUTO_REFRESH,
}
@@ -1809,35 +1815,55 @@ def build_runtime(start_workers=None):
start_worker=worker_state,
)
watchlist_refresh = WatchlistRefreshJobs(watchlist, start_worker=worker_state)
watchlist_auto_refresh = WatchlistAutoRefreshScheduler(
config_getter=lambda: get_config_snapshot(config),
refresh_start_fn=watchlist_refresh.start,
start_worker=worker_state,
)
return {
"config": config,
"watchlist": watchlist,
"download_queue": download_queue,
"watchlist_refresh": watchlist_refresh,
"watchlist_auto_refresh": watchlist_auto_refresh,
}
def initialize_runtime(start_workers=None):
global CONFIG, WATCHLIST, DOWNLOAD_QUEUE, WATCHLIST_REFRESH
global CONFIG, WATCHLIST, DOWNLOAD_QUEUE, WATCHLIST_REFRESH, WATCHLIST_AUTO_REFRESH
with RUNTIME_LOCK:
workers_allowed = background_workers_enabled()
if CONFIG is not None and WATCHLIST is not None and DOWNLOAD_QUEUE is not None and WATCHLIST_REFRESH is not None:
if (
CONFIG is not None
and WATCHLIST is not None
and DOWNLOAD_QUEUE is not None
and WATCHLIST_REFRESH is not None
and WATCHLIST_AUTO_REFRESH is not None
):
if start_workers and workers_allowed:
WATCHLIST.worker_enabled = True
WATCHLIST.ensure_worker()
DOWNLOAD_QUEUE.ensure_worker()
WATCHLIST_REFRESH.ensure_worker()
WATCHLIST_AUTO_REFRESH.ensure_worker()
return runtime_state()
runtime = build_runtime(start_workers=start_workers)
CONFIG = runtime["config"]
WATCHLIST = runtime["watchlist"]
DOWNLOAD_QUEUE = runtime["download_queue"]
WATCHLIST_REFRESH = runtime["watchlist_refresh"]
WATCHLIST_AUTO_REFRESH = runtime["watchlist_auto_refresh"]
return runtime
def ensure_runtime(start_workers=False):
if CONFIG is None or WATCHLIST is None or DOWNLOAD_QUEUE is None or WATCHLIST_REFRESH is None:
if (
CONFIG is None
or WATCHLIST is None
or DOWNLOAD_QUEUE is None
or WATCHLIST_REFRESH is None
or WATCHLIST_AUTO_REFRESH is None
):
return initialize_runtime(start_workers=start_workers)
if start_workers:
return initialize_runtime(start_workers=True)
@@ -1847,10 +1873,16 @@ def ensure_runtime(start_workers=False):
def shutdown_runtime(wait=False, cancel_active_downloads=None):
services = None
with RUNTIME_LOCK:
if CONFIG is None and WATCHLIST is None and DOWNLOAD_QUEUE is None and WATCHLIST_REFRESH is None:
if (
CONFIG is None
and WATCHLIST is None
and DOWNLOAD_QUEUE is None
and WATCHLIST_REFRESH is None
and WATCHLIST_AUTO_REFRESH is None
):
return False
services = (WATCHLIST, DOWNLOAD_QUEUE, WATCHLIST_REFRESH)
watchlist, download_queue, watchlist_refresh = services
services = (WATCHLIST, DOWNLOAD_QUEUE, WATCHLIST_REFRESH, WATCHLIST_AUTO_REFRESH)
watchlist, download_queue, watchlist_refresh, watchlist_auto_refresh = services
should_cancel_downloads = bool(wait) if cancel_active_downloads is None else bool(cancel_active_downloads)
stopped = True
if watchlist is not None:
@@ -1859,11 +1891,13 @@ def shutdown_runtime(wait=False, cancel_active_downloads=None):
stopped = download_queue.shutdown(wait=wait, cancel_active=should_cancel_downloads) and stopped
if watchlist_refresh is not None:
stopped = watchlist_refresh.shutdown(wait=wait) and stopped
if watchlist_auto_refresh is not None:
stopped = watchlist_auto_refresh.shutdown(wait=wait) and stopped
return stopped
def reset_runtime(wait=False, cancel_active_downloads=None):
global CONFIG, WATCHLIST, DOWNLOAD_QUEUE, WATCHLIST_REFRESH
global CONFIG, WATCHLIST, DOWNLOAD_QUEUE, WATCHLIST_REFRESH, WATCHLIST_AUTO_REFRESH
stopped = shutdown_runtime(wait=wait, cancel_active_downloads=cancel_active_downloads)
if not stopped:
if wait:
@@ -1874,6 +1908,7 @@ def reset_runtime(wait=False, cancel_active_downloads=None):
WATCHLIST = None
DOWNLOAD_QUEUE = None
WATCHLIST_REFRESH = None
WATCHLIST_AUTO_REFRESH = None
return True