Log scheduled refresh lifecycle to stdout
This commit is contained in:
+31
-2
@@ -178,6 +178,9 @@ class WatchlistRefreshJobs:
|
||||
def _copy_job(self, job):
|
||||
return json.loads(json.dumps(job))
|
||||
|
||||
def _stdout_log(self, message):
|
||||
print(f"[watchlist-refresh] {message}", flush=True)
|
||||
|
||||
def _find_locked(self, job_id):
|
||||
for job in self.jobs:
|
||||
if job["id"] == job_id:
|
||||
@@ -192,10 +195,15 @@ class WatchlistRefreshJobs:
|
||||
active = bool(job and job["status"] in {"pending", "running"})
|
||||
return {"job": self._copy_job(job) if job else None, "running": active}
|
||||
|
||||
def start(self):
|
||||
def start(self, source="manual"):
|
||||
source_name = str(source or "manual").strip().lower() or "manual"
|
||||
with self.lock:
|
||||
job = next((candidate for candidate in self.jobs if candidate["status"] in {"pending", "running"}), None)
|
||||
if job is not None:
|
||||
if source_name == "scheduled":
|
||||
self._stdout_log(
|
||||
f"Scheduled refresh skipped because another refresh is already {job['status']} (job={job['id']})."
|
||||
)
|
||||
return {
|
||||
"message": "Watchlist refresh already queued." if job["status"] == "pending" else "Watchlist refresh already running.",
|
||||
"job": self._copy_job(job) if job else None,
|
||||
@@ -205,6 +213,7 @@ class WatchlistRefreshJobs:
|
||||
job = {
|
||||
"id": os.urandom(16).hex(),
|
||||
"status": "pending",
|
||||
"source": source_name,
|
||||
"created_at": now,
|
||||
"updated_at": now,
|
||||
"started_at": None,
|
||||
@@ -220,6 +229,8 @@ class WatchlistRefreshJobs:
|
||||
self._save_job_locked(job)
|
||||
self.pending.append(job["id"])
|
||||
self.wakeup.set()
|
||||
if source_name == "scheduled":
|
||||
self._stdout_log(f"Scheduled refresh queued (job={job['id']}).")
|
||||
return {"message": "Watchlist refresh started.", "job": self._copy_job(job), "created": True}
|
||||
|
||||
def _next_pending(self):
|
||||
@@ -243,6 +254,7 @@ class WatchlistRefreshJobs:
|
||||
|
||||
def _run_job(self, job):
|
||||
interrupted = False
|
||||
source_name = str(job.get("source") or "manual").strip().lower() or "manual"
|
||||
try:
|
||||
show_ids = self.store.all_show_ids()
|
||||
started_at = now_iso()
|
||||
@@ -256,6 +268,8 @@ class WatchlistRefreshJobs:
|
||||
job["current_title"] = None
|
||||
job["message"] = f"Refreshing 0/{len(show_ids)} watchlist entries."
|
||||
self._save_job_locked(job)
|
||||
if source_name == "scheduled":
|
||||
self._stdout_log(f"Scheduled refresh started (job={job['id']}, total={len(show_ids)}).")
|
||||
|
||||
def refresh_one(show_id):
|
||||
title = show_id
|
||||
@@ -320,6 +334,19 @@ class WatchlistRefreshJobs:
|
||||
else:
|
||||
job["message"] = f"Refreshed {job['completed']} watchlist entries."
|
||||
self._save_job_locked(job)
|
||||
if source_name == "scheduled":
|
||||
if interrupted:
|
||||
self._stdout_log(
|
||||
f"Scheduled refresh interrupted (job={job['id']}, completed={job['completed']}/{job['total']})."
|
||||
)
|
||||
elif job["errors"]:
|
||||
self._stdout_log(
|
||||
f"Scheduled refresh finished with errors (job={job['id']}, completed={job['completed']}/{job['total']}, errors={job['errors']})."
|
||||
)
|
||||
else:
|
||||
self._stdout_log(
|
||||
f"Scheduled refresh finished successfully (job={job['id']}, completed={job['completed']}/{job['total']})."
|
||||
)
|
||||
except Exception as exc:
|
||||
finished_at = now_iso()
|
||||
with self.lock:
|
||||
@@ -329,6 +356,8 @@ class WatchlistRefreshJobs:
|
||||
job["current_title"] = None
|
||||
job["message"] = f"Watchlist refresh failed: {exc}"
|
||||
self._save_job_locked(job)
|
||||
if source_name == "scheduled":
|
||||
self._stdout_log(f"Scheduled refresh failed (job={job['id']}): {exc}")
|
||||
finally:
|
||||
with self.lock:
|
||||
self.current_executor = None
|
||||
@@ -397,7 +426,7 @@ class WatchlistAutoRefreshScheduler:
|
||||
return remaining
|
||||
self.last_attempt_monotonic = now
|
||||
try:
|
||||
result = self.refresh_start_fn()
|
||||
result = self.refresh_start_fn(source="scheduled")
|
||||
debug_log("watchlist.auto_refresh.tick", result=result)
|
||||
except Exception as exc:
|
||||
debug_log("watchlist.auto_refresh.error", error=exc)
|
||||
|
||||
Reference in New Issue
Block a user