from django.contrib import messages from django.contrib.auth.decorators import user_passes_test from django.core.cache import cache from django.db import connection from django.db.models import Count, Min, Q from django.http import JsonResponse from django.shortcuts import get_object_or_404, redirect, render from django.utils import timezone from django.views.decorators.http import require_POST from apps.articles.models import Article from apps.feeds.models import Feed, Subscription from apps.feeds.tasks import POLL_HEARTBEAT_CACHE_KEY, enqueue_fetch, reset_backoff from .health import fetch_pipeline_status is_admin = user_passes_test(lambda u: u.is_authenticated and u.is_staff) # Beat ticks poll_due_feeds every minute; this many minutes without a tick # means beat or the worker (or the broker between them) isn't working. POLL_HEARTBEAT_MAX_AGE_SECONDS = 5 * 60 def health(request): """Unauthenticated liveness check — reachable even before first-run setup (see SetupWizardMiddleware). Used by scripts/upgrade.sh to confirm a freshly restarted deploy actually came up, and is a reasonable target for any external uptime monitor too.""" try: with connection.cursor() as cursor: cursor.execute("SELECT 1") except Exception: return JsonResponse({"status": "error"}, status=503) return JsonResponse({"status": "ok"}) def health_ready(request): """Readiness: liveness plus everything background fetching depends on (C8) — the database, the Redis cache/broker, and a recent poll_due_feeds tick, which proves beat is scheduling AND a worker is consuming. Beyond that tick, apps.core.health checks fetching is actually succeeding: queue depth, a recent successful fetch, and the share of feeds failing. /health/ above stays DB-only on purpose: scripts/upgrade.sh gates a deploy on it right after restart, before beat has had a chance to tick. Point an uptime monitor at this one instead.""" checks = {"database": False, "redis": False, "poll_heartbeat": False} detail = {} try: with connection.cursor() as cursor: cursor.execute("SELECT 1") checks["database"] = True except Exception: pass try: cache.set("health-ready-probe", "1", timeout=10) checks["redis"] = cache.get("health-ready-probe") == "1" except Exception: pass try: last_tick = cache.get(POLL_HEARTBEAT_CACHE_KEY) except Exception: last_tick = None if last_tick is not None: age = (timezone.now() - last_tick).total_seconds() detail["poll_heartbeat_age_seconds"] = int(age) checks["poll_heartbeat"] = age <= POLL_HEARTBEAT_MAX_AGE_SECONDS if checks["database"]: pipeline_checks, pipeline_detail = fetch_pipeline_status() checks.update(pipeline_checks) detail.update(pipeline_detail) ok = all(checks.values()) return JsonResponse( {"status": "ok" if ok else "degraded", "checks": checks, **detail}, status=200 if ok else 503, ) @is_admin def dashboard(request): feeds = Feed.objects.annotate(subscriber_count=Count("subscriptions")) erroring_feeds = feeds.filter( Q(last_fetch_status=Feed.STATUS_ERROR) | Q(fetch_error_count__gt=0) ).order_by("-fetch_error_count") stats = { "feed_count": feeds.count(), "active_feed_count": feeds.filter(is_active=True).count(), "error_feed_count": erroring_feeds.count(), "subscription_count": Subscription.objects.count(), "article_count": Article.objects.count(), "oldest_article_at": Article.objects.aggregate(oldest=Min("fetched_at"))["oldest"], } return render( request, "core/dashboard.html", {"stats": stats, "erroring_feeds": erroring_feeds}, ) @is_admin @require_POST def retry_feed(request, feed_id): feed = get_object_or_404(Feed, pk=feed_id) # Reset first, synchronously: as separate queued tasks the reset could # land after the fetch and undo the schedule it just set. reset_backoff(feed.id) name = feed.title or feed.feed_url if enqueue_fetch(feed.id): messages.success(request, f"Queued an immediate retry for '{name}'.") else: messages.info(request, f"'{name}' is already being fetched right now.") return redirect("core:dashboard")