Add Discord webhook refresh notifications

This commit is contained in:
Dymas
2026-05-21 18:56:49 +02:00
parent a95b4c343f
commit 9648b4f6e4
9 changed files with 638 additions and 54 deletions
+75
View File
@@ -26,6 +26,7 @@ from app_support import (
load_json,
now_iso,
printable_command,
send_discord_webhook_event,
strip_control,
)
@@ -262,6 +263,8 @@ class WatchlistRefreshJobs:
def _run_job(self, job):
interrupted = False
source_name = str(job.get("source") or "manual").strip().lower() or "manual"
new_episode_items = []
refresh_error_items = []
try:
show_ids = self.store.all_show_ids()
delay_seconds = self._refresh_delay_seconds()
@@ -295,6 +298,20 @@ class WatchlistRefreshJobs:
title = item.get("title") or title
errored = item.get("status") == "error"
error_message = str(item.get("status_message") or "").strip() if errored else ""
if item.get("had_new_episodes"):
new_episode_items.append(
{
"show_id": item.get("show_id"),
"title": title,
"sub_count": item.get("sub_count"),
"dub_count": item.get("dub_count"),
"sub_delta": item.get("sub_delta"),
"dub_delta": item.get("dub_delta"),
"expected_count": item.get("expected_count"),
"airing_status": item.get("airing_status"),
"thumbnail_path": item.get("thumbnail_path"),
}
)
except Exception as exc:
errored = True
error_message = str(exc)
@@ -306,6 +323,7 @@ class WatchlistRefreshJobs:
job["updated_at"] = now_iso()
if errored and error_message:
job["last_error"] = error_message
refresh_error_items.append({"show_id": show_id, "title": title, "error": error_message})
if skipped:
job["message"] = f"Refreshing {index}/{len(show_ids)}: removed entry"
else:
@@ -344,6 +362,42 @@ class WatchlistRefreshJobs:
self._stdout_log(
f"Scheduled refresh finished successfully (job={job['id']}, completed={job['completed']}/{job['total']})."
)
config = self.config_getter() or {}
if interrupted:
send_discord_webhook_event(
config,
"watchlist_refresh_interrupted",
{
"source": source_name,
"completed": job.get("completed"),
"total": job.get("total"),
"message": job.get("message"),
},
)
else:
if new_episode_items:
send_discord_webhook_event(
config,
"watchlist_refresh_new_episodes",
{
"source": source_name,
"job_id": job.get("id"),
"items": new_episode_items,
},
)
if job.get("errors"):
send_discord_webhook_event(
config,
"watchlist_refresh_failed",
{
"source": source_name,
"completed": job.get("completed"),
"total": job.get("total"),
"errors": job.get("errors"),
"items": refresh_error_items,
"error": job.get("last_error"),
},
)
except Exception as exc:
finished_at = now_iso()
with self.lock:
@@ -352,9 +406,22 @@ class WatchlistRefreshJobs:
job["updated_at"] = finished_at
job["current_title"] = None
job["message"] = f"Watchlist refresh failed: {exc}"
job["last_error"] = str(exc)
self._save_job_locked(job)
if source_name == "scheduled":
self._stdout_log(f"Scheduled refresh failed (job={job['id']}): {exc}")
send_discord_webhook_event(
self.config_getter() or {},
"watchlist_refresh_failed",
{
"source": source_name,
"completed": job.get("completed"),
"total": job.get("total"),
"errors": max(1, int(job.get("errors") or 0)),
"items": refresh_error_items,
"error": str(exc),
},
)
finally:
with self.lock:
self.current_executor = None
@@ -427,6 +494,14 @@ class WatchlistAutoRefreshScheduler:
debug_log("watchlist.auto_refresh.tick", result=result)
except Exception as exc:
debug_log("watchlist.auto_refresh.error", error=exc)
send_discord_webhook_event(
self.config_getter() or {},
"runtime_error",
{
"source": "watchlist.auto_refresh",
"error": str(exc),
},
)
return interval_seconds
def _run(self):