Private
Public Access
Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0975a7634b | ||
|
|
fd494a9c08 | ||
|
|
f8cd1eb33e | ||
|
|
73dbee9400 |
No files matched your search
@@ -54,6 +54,18 @@ ruff check .
|
|||||||
black --check .
|
black --check .
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Testing against PostgreSQL
|
||||||
|
|
||||||
|
The suite runs on SQLite by default; the Postgres-only tests (search backend)
|
||||||
|
are skipped there. To run everything against a real Postgres:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker run -d --name evection-pg-test -e POSTGRES_PASSWORD=testpw -e POSTGRES_DB=evection \
|
||||||
|
-p 127.0.0.1:55432:5432 postgres:16
|
||||||
|
DATABASE_URL=postgres://postgres:testpw@127.0.0.1:55432/evection pytest
|
||||||
|
docker rm -f evection-pg-test
|
||||||
|
```
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
[GNU AGPL-3.0-or-later](LICENSE) — if you run a modified version of Evection as
|
[GNU AGPL-3.0-or-later](LICENSE) — if you run a modified version of Evection as
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
# Generated by Django 5.2.18 on 2026-11-01 16:28
|
||||||
|
|
||||||
|
import django.db.models.deletion
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('articles', '0004_starred_saved_as_system_tags'),
|
||||||
|
('feeds', '0006_backfill_feed_next_fetch_at'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.CreateModel(
|
||||||
|
name='PurgedArticle',
|
||||||
|
fields=[
|
||||||
|
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||||
|
('guid', models.CharField(max_length=1000)),
|
||||||
|
('purged_at', models.DateTimeField(auto_now_add=True)),
|
||||||
|
('feed', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='purged_articles', to='feeds.feed')),
|
||||||
|
],
|
||||||
|
options={
|
||||||
|
'unique_together': {('feed', 'guid')},
|
||||||
|
},
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -32,6 +32,23 @@ class Article(models.Model):
|
|||||||
return self.title or self.guid
|
return self.title or self.guid
|
||||||
|
|
||||||
|
|
||||||
|
class PurgedArticle(models.Model):
|
||||||
|
"""Tombstone left by the retention purge (apps.articles.tasks.
|
||||||
|
purge_old_articles) for every article it deletes: a feed that still
|
||||||
|
lists an entry after we purged it would otherwise be re-inserted as a
|
||||||
|
brand-new, unread article on its next poll. Ingestion skips any guid
|
||||||
|
that has a tombstone here. Only the (feed, guid) pair is kept — a
|
||||||
|
small fraction of the article it replaces.
|
||||||
|
"""
|
||||||
|
|
||||||
|
feed = models.ForeignKey(Feed, on_delete=models.CASCADE, related_name="purged_articles")
|
||||||
|
guid = models.CharField(max_length=1000)
|
||||||
|
purged_at = models.DateTimeField(auto_now_add=True)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
unique_together = [("feed", "guid")]
|
||||||
|
|
||||||
|
|
||||||
class UserArticleState(models.Model):
|
class UserArticleState(models.Model):
|
||||||
"""Per-user read/hide state for an Article.
|
"""Per-user read/hide state for an Article.
|
||||||
|
|
||||||
|
|||||||
+13
-3
@@ -1,6 +1,7 @@
|
|||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
|
|
||||||
from celery import shared_task
|
from celery import shared_task
|
||||||
|
from django.db import transaction
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
|
||||||
from apps.core.models import SiteSettings
|
from apps.core.models import SiteSettings
|
||||||
@@ -8,7 +9,7 @@ from apps.feeds.models import Subscription
|
|||||||
from apps.rules.engine import evaluate_rules_for_article
|
from apps.rules.engine import evaluate_rules_for_article
|
||||||
from apps.rules.models import FilterRule
|
from apps.rules.models import FilterRule
|
||||||
|
|
||||||
from .models import Article, ArticleTag, SubscriptionTag
|
from .models import Article, ArticleTag, PurgedArticle, SubscriptionTag
|
||||||
|
|
||||||
|
|
||||||
@shared_task
|
@shared_task
|
||||||
@@ -75,6 +76,10 @@ def purge_old_articles():
|
|||||||
ArticleTag rows go with their article via ON DELETE CASCADE, and the
|
ArticleTag rows go with their article via ON DELETE CASCADE, and the
|
||||||
SQLite FTS triggers drop the search entries. 0 days means keep
|
SQLite FTS triggers drop the search entries. 0 days means keep
|
||||||
everything forever (the default).
|
everything forever (the default).
|
||||||
|
|
||||||
|
Each deleted article leaves a PurgedArticle tombstone first, so a feed
|
||||||
|
that still lists the entry can't bring it back as a new, unread
|
||||||
|
article on its next poll (see apps.feeds.services.fetcher).
|
||||||
"""
|
"""
|
||||||
retention_days = SiteSettings.load().retention_days
|
retention_days = SiteSettings.load().retention_days
|
||||||
if retention_days <= 0:
|
if retention_days <= 0:
|
||||||
@@ -87,9 +92,14 @@ def purge_old_articles():
|
|||||||
chunk = list(
|
chunk = list(
|
||||||
Article.objects.filter(fetched_at__lt=cutoff)
|
Article.objects.filter(fetched_at__lt=cutoff)
|
||||||
.exclude(pk__in=keep)
|
.exclude(pk__in=keep)
|
||||||
.values_list("pk", flat=True)[:PURGE_CHUNK_SIZE]
|
.values_list("pk", "feed_id", "guid")[:PURGE_CHUNK_SIZE]
|
||||||
)
|
)
|
||||||
if not chunk:
|
if not chunk:
|
||||||
return deleted_total
|
return deleted_total
|
||||||
Article.objects.filter(pk__in=chunk).delete()
|
with transaction.atomic():
|
||||||
|
PurgedArticle.objects.bulk_create(
|
||||||
|
[PurgedArticle(feed_id=feed_id, guid=guid) for _pk, feed_id, guid in chunk],
|
||||||
|
ignore_conflicts=True,
|
||||||
|
)
|
||||||
|
Article.objects.filter(pk__in=[pk for pk, _feed_id, _guid in chunk]).delete()
|
||||||
deleted_total += len(chunk)
|
deleted_total += len(chunk)
|
||||||
@@ -72,3 +72,20 @@ def test_purge_works_through_more_than_one_chunk(feed, monkeypatch):
|
|||||||
|
|
||||||
assert purge_old_articles() == 5
|
assert purge_old_articles() == 5
|
||||||
assert Article.objects.count() == 0
|
assert Article.objects.count() == 0
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.django_db
|
||||||
|
def test_purge_leaves_a_tombstone_per_deleted_article(feed):
|
||||||
|
from .models import PurgedArticle
|
||||||
|
|
||||||
|
SiteSettings.objects.create(retention_days=30)
|
||||||
|
_article(feed, "gone-1", age_days=60)
|
||||||
|
_article(feed, "gone-2", age_days=60)
|
||||||
|
_article(feed, "kept", age_days=1)
|
||||||
|
|
||||||
|
purge_old_articles()
|
||||||
|
|
||||||
|
assert set(PurgedArticle.objects.filter(feed=feed).values_list("guid", flat=True)) == {
|
||||||
|
"gone-1",
|
||||||
|
"gone-2",
|
||||||
|
}
|
||||||
@@ -0,0 +1,83 @@
|
|||||||
|
from datetime import timedelta
|
||||||
|
|
||||||
|
import redis
|
||||||
|
from django.conf import settings
|
||||||
|
from django.db.models import Max, Min
|
||||||
|
from django.utils import timezone
|
||||||
|
|
||||||
|
from apps.feeds.models import Feed
|
||||||
|
|
||||||
|
from .models import SiteSettings
|
||||||
|
|
||||||
|
# More than this many tasks waiting means workers aren't keeping up (or
|
||||||
|
# aren't running) — fetches are piling up faster than they complete.
|
||||||
|
MAX_QUEUE_DEPTH = 1000
|
||||||
|
|
||||||
|
# Past this share of active feeds failing, it's far more likely something
|
||||||
|
# on our side (DNS, outbound network, a bad deploy) than every publisher
|
||||||
|
# breaking at once. Needs a handful of feeds first so one broken feed out
|
||||||
|
# of two doesn't read as an outage.
|
||||||
|
MAX_FAILING_RATIO = 0.5
|
||||||
|
MIN_FEEDS_FOR_RATIO = 4
|
||||||
|
|
||||||
|
MIN_SUCCESS_WINDOW_MINUTES = 30
|
||||||
|
|
||||||
|
|
||||||
|
def queue_depth() -> int | None:
|
||||||
|
"""Tasks waiting in Celery's default queue, or None if the broker
|
||||||
|
can't be reached. Celery's Redis transport keeps each queue as a Redis
|
||||||
|
list named after the queue."""
|
||||||
|
try:
|
||||||
|
client = redis.Redis.from_url(
|
||||||
|
settings.CELERY_BROKER_URL, socket_connect_timeout=2, socket_timeout=2
|
||||||
|
)
|
||||||
|
return int(client.llen("celery"))
|
||||||
|
except Exception:
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def _success_window() -> timedelta:
|
||||||
|
floor = getattr(settings, "EVECTION_MIN_FETCH_INTERVAL_MINUTES", 15)
|
||||||
|
base = max(SiteSettings.load().fetch_interval_minutes, floor)
|
||||||
|
return timedelta(minutes=max(3 * base, MIN_SUCCESS_WINDOW_MINUTES))
|
||||||
|
|
||||||
|
|
||||||
|
def fetch_pipeline_status(now=None) -> tuple[dict[str, bool], dict]:
|
||||||
|
"""Checks that fetching is actually *succeeding*, not just scheduled
|
||||||
|
(the poll heartbeat only proves beat and a worker are alive): returns
|
||||||
|
({check: passed}, detail) covering queue depth, whether any feed has
|
||||||
|
been fetched successfully recently, and the share of feeds failing.
|
||||||
|
"""
|
||||||
|
now = now or timezone.now()
|
||||||
|
detail: dict = {}
|
||||||
|
|
||||||
|
depth = queue_depth()
|
||||||
|
detail["queue_depth"] = depth
|
||||||
|
checks = {"queue_depth": depth is not None and depth <= MAX_QUEUE_DEPTH}
|
||||||
|
|
||||||
|
active = Feed.objects.filter(is_active=True)
|
||||||
|
active_count = active.count()
|
||||||
|
failing = active.filter(fetch_error_count__gt=0).count()
|
||||||
|
detail["active_feeds"] = active_count
|
||||||
|
detail["failing_feeds"] = failing
|
||||||
|
|
||||||
|
window = _success_window()
|
||||||
|
last_success = active.filter(
|
||||||
|
last_fetch_status__in=[Feed.STATUS_OK, Feed.STATUS_NOT_MODIFIED]
|
||||||
|
).aggregate(latest=Max("last_fetched_at"))["latest"]
|
||||||
|
if last_success is not None:
|
||||||
|
detail["last_successful_fetch_age_seconds"] = int((now - last_success).total_seconds())
|
||||||
|
recent_success = now - last_success <= window
|
||||||
|
elif active_count == 0:
|
||||||
|
recent_success = True
|
||||||
|
else:
|
||||||
|
# Nothing has ever fetched successfully — only a problem once
|
||||||
|
# the oldest feed has had a full window to be fetched.
|
||||||
|
oldest = active.aggregate(oldest=Min("created_at"))["oldest"]
|
||||||
|
recent_success = now - oldest <= window
|
||||||
|
checks["recent_fetch_success"] = recent_success
|
||||||
|
|
||||||
|
checks["feed_failures"] = not (
|
||||||
|
active_count >= MIN_FEEDS_FOR_RATIO and failing / active_count > MAX_FAILING_RATIO
|
||||||
|
)
|
||||||
|
return checks, detail
|
||||||
+137
-4
@@ -22,8 +22,17 @@ def test_health_check_works_before_first_run_setup(client):
|
|||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def broker_queue(monkeypatch):
|
||||||
|
"""Stands in for the Redis broker's queue length (tests have no Redis):
|
||||||
|
set `broker_queue["depth"]` to a number, or None for "unreachable"."""
|
||||||
|
state = {"depth": 0}
|
||||||
|
monkeypatch.setattr("apps.core.health.queue_depth", lambda: state["depth"])
|
||||||
|
return state
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.django_db
|
@pytest.mark.django_db
|
||||||
def test_health_ready_is_degraded_until_a_poll_tick_has_happened(client):
|
def test_health_ready_is_degraded_until_a_poll_tick_has_happened(client, broker_queue):
|
||||||
response = client.get(reverse("core:health_ready"))
|
response = client.get(reverse("core:health_ready"))
|
||||||
assert response.status_code == 503
|
assert response.status_code == 503
|
||||||
body = response.json()
|
body = response.json()
|
||||||
@@ -34,22 +43,26 @@ def test_health_ready_is_degraded_until_a_poll_tick_has_happened(client):
|
|||||||
|
|
||||||
|
|
||||||
@pytest.mark.django_db
|
@pytest.mark.django_db
|
||||||
def test_health_ready_is_ok_after_poll_due_feeds_runs(client):
|
def test_health_ready_is_ok_after_poll_due_feeds_runs(client, broker_queue):
|
||||||
from apps.feeds.tasks import poll_due_feeds
|
from apps.feeds.tasks import poll_due_feeds
|
||||||
|
|
||||||
poll_due_feeds()
|
poll_due_feeds()
|
||||||
|
|
||||||
response = client.get(reverse("core:health_ready"))
|
response = client.get(reverse("core:health_ready"))
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
assert response.json()["checks"] == {
|
checks = response.json()["checks"]
|
||||||
|
assert checks == {
|
||||||
"database": True,
|
"database": True,
|
||||||
"redis": True,
|
"redis": True,
|
||||||
"poll_heartbeat": True,
|
"poll_heartbeat": True,
|
||||||
|
"queue_depth": True,
|
||||||
|
"recent_fetch_success": True,
|
||||||
|
"feed_failures": True,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.django_db
|
@pytest.mark.django_db
|
||||||
def test_health_ready_flags_a_stale_poll_heartbeat(client):
|
def test_health_ready_flags_a_stale_poll_heartbeat(client, broker_queue):
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
|
|
||||||
from django.core.cache import cache
|
from django.core.cache import cache
|
||||||
@@ -119,3 +132,123 @@ def test_retry_feed_resets_backoff_and_requeues(client, django_user_model):
|
|||||||
assert response.status_code == 302
|
assert response.status_code == 302
|
||||||
mock_reset.assert_called_once_with(feed.id)
|
mock_reset.assert_called_once_with(feed.id)
|
||||||
mock_fetch.assert_called_once_with(feed.id)
|
mock_fetch.assert_called_once_with(feed.id)
|
||||||
|
|
||||||
|
|
||||||
|
def _ready(client):
|
||||||
|
from django.core.cache import cache
|
||||||
|
from django.utils import timezone
|
||||||
|
|
||||||
|
from apps.feeds.tasks import POLL_HEARTBEAT_CACHE_KEY
|
||||||
|
|
||||||
|
# Record the heartbeat directly: running poll_due_feeds here would
|
||||||
|
# execute fetches eagerly (CELERY_TASK_ALWAYS_EAGER) and rewrite the
|
||||||
|
# feed states each test sets up.
|
||||||
|
cache.set(POLL_HEARTBEAT_CACHE_KEY, timezone.now())
|
||||||
|
response = client.get(reverse("core:health_ready"))
|
||||||
|
return response.status_code, response.json()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.django_db
|
||||||
|
def test_health_ready_flags_a_backed_up_queue(client, broker_queue):
|
||||||
|
from .health import MAX_QUEUE_DEPTH
|
||||||
|
|
||||||
|
broker_queue["depth"] = MAX_QUEUE_DEPTH + 1
|
||||||
|
|
||||||
|
status, body = _ready(client)
|
||||||
|
|
||||||
|
assert status == 503
|
||||||
|
assert body["checks"]["queue_depth"] is False
|
||||||
|
assert body["queue_depth"] == MAX_QUEUE_DEPTH + 1
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.django_db
|
||||||
|
def test_health_ready_flags_an_unreachable_broker_queue(client, broker_queue):
|
||||||
|
broker_queue["depth"] = None
|
||||||
|
|
||||||
|
status, body = _ready(client)
|
||||||
|
|
||||||
|
assert status == 503
|
||||||
|
assert body["checks"]["queue_depth"] is False
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.django_db
|
||||||
|
def test_health_ready_flags_no_recent_successful_fetch(client, broker_queue):
|
||||||
|
from datetime import timedelta
|
||||||
|
|
||||||
|
from django.utils import timezone
|
||||||
|
|
||||||
|
feed = Feed.objects.create(
|
||||||
|
feed_url="https://a.example.com/feed.xml",
|
||||||
|
last_fetch_status=Feed.STATUS_OK,
|
||||||
|
last_fetched_at=timezone.now() - timedelta(days=2),
|
||||||
|
)
|
||||||
|
Feed.objects.filter(pk=feed.pk).update(created_at=timezone.now() - timedelta(days=30))
|
||||||
|
|
||||||
|
status, body = _ready(client)
|
||||||
|
|
||||||
|
assert status == 503
|
||||||
|
assert body["checks"]["recent_fetch_success"] is False
|
||||||
|
assert body["last_successful_fetch_age_seconds"] > 86400
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.django_db
|
||||||
|
def test_health_ready_is_ok_with_a_recent_successful_fetch(client, broker_queue):
|
||||||
|
from django.utils import timezone
|
||||||
|
|
||||||
|
Feed.objects.create(
|
||||||
|
feed_url="https://a.example.com/feed.xml",
|
||||||
|
last_fetch_status=Feed.STATUS_NOT_MODIFIED,
|
||||||
|
last_fetched_at=timezone.now(),
|
||||||
|
)
|
||||||
|
|
||||||
|
status, body = _ready(client)
|
||||||
|
|
||||||
|
assert status == 200
|
||||||
|
assert body["active_feeds"] == 1
|
||||||
|
assert body["failing_feeds"] == 0
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.django_db
|
||||||
|
def test_health_ready_gives_a_brand_new_feed_time_before_it_counts_as_stale(client, broker_queue):
|
||||||
|
Feed.objects.create(feed_url="https://new.example.com/feed.xml")
|
||||||
|
|
||||||
|
status, body = _ready(client)
|
||||||
|
|
||||||
|
assert status == 200
|
||||||
|
assert body["checks"]["recent_fetch_success"] is True
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.django_db
|
||||||
|
def test_health_ready_flags_most_feeds_failing_but_not_a_single_broken_one(client, broker_queue):
|
||||||
|
from django.utils import timezone
|
||||||
|
|
||||||
|
now = timezone.now()
|
||||||
|
healthy = Feed.objects.create(
|
||||||
|
feed_url="https://ok.example.com/feed.xml",
|
||||||
|
last_fetch_status=Feed.STATUS_OK,
|
||||||
|
last_fetched_at=now,
|
||||||
|
)
|
||||||
|
broken = [
|
||||||
|
Feed.objects.create(
|
||||||
|
feed_url=f"https://bad{i}.example.com/feed.xml",
|
||||||
|
last_fetch_status=Feed.STATUS_ERROR,
|
||||||
|
fetch_error_count=3,
|
||||||
|
last_fetched_at=now,
|
||||||
|
)
|
||||||
|
for i in range(2)
|
||||||
|
]
|
||||||
|
|
||||||
|
status, body = _ready(client)
|
||||||
|
assert status == 200, "2 of 3 failing is below the minimum feed count for the ratio"
|
||||||
|
|
||||||
|
Feed.objects.create(
|
||||||
|
feed_url="https://bad9.example.com/feed.xml",
|
||||||
|
last_fetch_status=Feed.STATUS_ERROR,
|
||||||
|
fetch_error_count=1,
|
||||||
|
last_fetched_at=now,
|
||||||
|
)
|
||||||
|
status, body = _ready(client)
|
||||||
|
assert status == 503
|
||||||
|
assert body["checks"]["feed_failures"] is False
|
||||||
|
assert body["failing_feeds"] == 3
|
||||||
|
assert healthy.pk and broken
|
||||||
+10
-1
@@ -12,6 +12,8 @@ from apps.articles.models import Article
|
|||||||
from apps.feeds.models import Feed, Subscription
|
from apps.feeds.models import Feed, Subscription
|
||||||
from apps.feeds.tasks import POLL_HEARTBEAT_CACHE_KEY, fetch_feed, reset_feed_backoff
|
from apps.feeds.tasks import POLL_HEARTBEAT_CACHE_KEY, fetch_feed, reset_feed_backoff
|
||||||
|
|
||||||
|
from .health import fetch_pipeline_status
|
||||||
|
|
||||||
is_admin = user_passes_test(lambda u: u.is_authenticated and u.is_staff)
|
is_admin = user_passes_test(lambda u: u.is_authenticated and u.is_staff)
|
||||||
|
|
||||||
|
|
||||||
@@ -37,7 +39,9 @@ def health_ready(request):
|
|||||||
"""Readiness: liveness plus everything background fetching depends on
|
"""Readiness: liveness plus everything background fetching depends on
|
||||||
(C8) — the database, the Redis cache/broker, and a recent
|
(C8) — the database, the Redis cache/broker, and a recent
|
||||||
poll_due_feeds tick, which proves beat is scheduling AND a worker is
|
poll_due_feeds tick, which proves beat is scheduling AND a worker is
|
||||||
consuming. /health/ above stays DB-only on purpose: scripts/upgrade.sh
|
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
|
gates a deploy on it right after restart, before beat has had a
|
||||||
chance to tick. Point an uptime monitor at this one instead."""
|
chance to tick. Point an uptime monitor at this one instead."""
|
||||||
checks = {"database": False, "redis": False, "poll_heartbeat": False}
|
checks = {"database": False, "redis": False, "poll_heartbeat": False}
|
||||||
@@ -65,6 +69,11 @@ def health_ready(request):
|
|||||||
detail["poll_heartbeat_age_seconds"] = int(age)
|
detail["poll_heartbeat_age_seconds"] = int(age)
|
||||||
checks["poll_heartbeat"] = age <= POLL_HEARTBEAT_MAX_AGE_SECONDS
|
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())
|
ok = all(checks.values())
|
||||||
return JsonResponse(
|
return JsonResponse(
|
||||||
{"status": "ok" if ok else "degraded", "checks": checks, **detail},
|
{"status": "ok" if ok else "degraded", "checks": checks, **detail},
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import hashlib
|
|||||||
import logging
|
import logging
|
||||||
from calendar import timegm
|
from calendar import timegm
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from datetime import datetime
|
from datetime import datetime, timedelta
|
||||||
from datetime import timezone as dt_timezone
|
from datetime import timezone as dt_timezone
|
||||||
|
|
||||||
import feedparser
|
import feedparser
|
||||||
@@ -21,7 +21,8 @@ from django.conf import settings
|
|||||||
from django.db import IntegrityError, transaction
|
from django.db import IntegrityError, transaction
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
|
||||||
from apps.articles.models import Article
|
from apps.articles.models import Article, PurgedArticle
|
||||||
|
from apps.core.models import SiteSettings
|
||||||
from apps.feeds.models import Feed
|
from apps.feeds.models import Feed
|
||||||
|
|
||||||
from .ssrf_guard import SSRFBlocked, safe_get
|
from .ssrf_guard import SSRFBlocked, safe_get
|
||||||
@@ -263,6 +264,24 @@ def fetch_and_ingest(feed: Feed) -> FetchResult:
|
|||||||
"content_hash": _content_hash(title, link, author, summary, content_html, published_at),
|
"content_hash": _content_hash(title, link, author, summary, content_html, published_at),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Entries the retention purge already deleted (tombstoned) and, when
|
||||||
|
# retention is on, entries so old by their own publish date that the
|
||||||
|
# next purge would delete them again, are not (re-)ingested — see
|
||||||
|
# apps.articles.tasks.purge_old_articles.
|
||||||
|
retention_days = SiteSettings.load().retention_days
|
||||||
|
if retention_days > 0:
|
||||||
|
too_old_before = timezone.now() - timedelta(days=retention_days)
|
||||||
|
for guid in [
|
||||||
|
g
|
||||||
|
for g, f in parsed_entries.items()
|
||||||
|
if f["published_at"] is not None and f["published_at"] < too_old_before
|
||||||
|
]:
|
||||||
|
del parsed_entries[guid]
|
||||||
|
for guid in PurgedArticle.objects.filter(feed=feed, guid__in=parsed_entries).values_list(
|
||||||
|
"guid", flat=True
|
||||||
|
):
|
||||||
|
del parsed_entries[guid]
|
||||||
|
|
||||||
existing = {a.guid: a for a in Article.objects.filter(feed=feed, guid__in=parsed_entries)}
|
existing = {a.guid: a for a in Article.objects.filter(feed=feed, guid__in=parsed_entries)}
|
||||||
|
|
||||||
to_create = [
|
to_create = [
|
||||||
|
|||||||
@@ -338,3 +338,64 @@ def test_ingest_falls_back_to_row_by_row_when_a_concurrent_insert_wins(feed):
|
|||||||
|
|
||||||
assert len(result.new_article_ids) == 3
|
assert len(result.new_article_ids) == 3
|
||||||
assert Article.objects.filter(feed=feed).count() == 3
|
assert Article.objects.filter(feed=feed).count() == 3
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.django_db
|
||||||
|
@responses.activate
|
||||||
|
def test_purged_article_is_not_resurrected_by_a_feed_that_still_lists_it(feed):
|
||||||
|
"""#29: a feed still listing an entry the retention purge deleted must
|
||||||
|
not bring it back as a brand-new, unread article."""
|
||||||
|
from apps.articles.tasks import purge_old_articles
|
||||||
|
from apps.core.models import SiteSettings
|
||||||
|
|
||||||
|
SiteSettings.objects.create(retention_days=30)
|
||||||
|
responses.add(responses.GET, FEED_URL, body=_feed_xml(3), status=200)
|
||||||
|
fetch_and_ingest(feed)
|
||||||
|
Article.objects.filter(feed=feed, guid="guid-0").update(
|
||||||
|
fetched_at=timezone.now() - dt.timedelta(days=90)
|
||||||
|
)
|
||||||
|
assert purge_old_articles() == 1
|
||||||
|
assert not Article.objects.filter(feed=feed, guid="guid-0").exists()
|
||||||
|
|
||||||
|
result = fetch_and_ingest(feed)
|
||||||
|
|
||||||
|
assert result.new_article_ids == []
|
||||||
|
assert not Article.objects.filter(feed=feed, guid="guid-0").exists()
|
||||||
|
assert Article.objects.filter(feed=feed).count() == 2
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.django_db
|
||||||
|
@responses.activate
|
||||||
|
def test_entries_older_than_the_retention_window_are_not_ingested(feed):
|
||||||
|
"""With retention on, a feed's long archive isn't ingested only to be
|
||||||
|
purged (and tombstoned) the same night."""
|
||||||
|
from apps.core.models import SiteSettings
|
||||||
|
|
||||||
|
SiteSettings.objects.create(retention_days=30)
|
||||||
|
xml = (
|
||||||
|
'<?xml version="1.0"?><rss version="2.0"><channel><title>T</title>'
|
||||||
|
"<item><title>Ancient</title><guid>old</guid>"
|
||||||
|
"<pubDate>Mon, 01 Jan 2024 00:00:00 GMT</pubDate></item>"
|
||||||
|
"<item><title>Undated</title><guid>undated</guid></item>"
|
||||||
|
"</channel></rss>"
|
||||||
|
)
|
||||||
|
responses.add(responses.GET, FEED_URL, body=xml, status=200)
|
||||||
|
|
||||||
|
result = fetch_and_ingest(feed)
|
||||||
|
|
||||||
|
assert len(result.new_article_ids) == 1
|
||||||
|
assert Article.objects.filter(feed=feed).get().guid == "undated"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.django_db
|
||||||
|
@responses.activate
|
||||||
|
def test_old_entries_are_ingested_normally_when_retention_is_off(feed):
|
||||||
|
xml = (
|
||||||
|
'<?xml version="1.0"?><rss version="2.0"><channel><title>T</title>'
|
||||||
|
"<item><title>Ancient</title><guid>old</guid>"
|
||||||
|
"<pubDate>Mon, 01 Jan 2024 00:00:00 GMT</pubDate></item>"
|
||||||
|
"</channel></rss>"
|
||||||
|
)
|
||||||
|
responses.add(responses.GET, FEED_URL, body=xml, status=200)
|
||||||
|
|
||||||
|
assert len(fetch_and_ingest(feed).new_article_ids) == 1
|
||||||
@@ -503,3 +503,54 @@ def test_article_with_no_publish_date_shows_when_it_was_fetched(client, alice):
|
|||||||
|
|
||||||
article.refresh_from_db()
|
article.refresh_from_db()
|
||||||
assert article.published_at is None
|
assert article.published_at is None
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.django_db
|
||||||
|
def test_new_since_requires_login(client):
|
||||||
|
response = client.get(reverse("reader:new_since"))
|
||||||
|
assert response.status_code == 302
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.django_db
|
||||||
|
def test_new_since_without_a_cursor_returns_the_baseline_id(client, alice):
|
||||||
|
feed = Feed.objects.create(feed_url="https://example.com/feed.xml")
|
||||||
|
Subscription.objects.create(user=alice, feed=feed)
|
||||||
|
article = Article.objects.create(feed=feed, guid="g1", title="Hello")
|
||||||
|
|
||||||
|
client.login(username="alice", password="pw-alice-123")
|
||||||
|
body = client.get(reverse("reader:new_since")).json()
|
||||||
|
|
||||||
|
assert body == {"latest_id": article.pk, "new_count": 0}
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.django_db
|
||||||
|
def test_new_since_counts_only_new_unread_visible_articles_from_my_feeds(client, alice, bob):
|
||||||
|
mine = Feed.objects.create(feed_url="https://mine.example.com/feed.xml")
|
||||||
|
theirs = Feed.objects.create(feed_url="https://theirs.example.com/feed.xml")
|
||||||
|
Subscription.objects.create(user=alice, feed=mine)
|
||||||
|
Subscription.objects.create(user=bob, feed=theirs)
|
||||||
|
cursor = Article.objects.create(feed=mine, guid="cursor", title="Cursor").pk
|
||||||
|
|
||||||
|
new_unread = Article.objects.create(feed=mine, guid="n1", title="New unread")
|
||||||
|
already_read = Article.objects.create(feed=mine, guid="n2", title="New but read")
|
||||||
|
hidden = Article.objects.create(feed=mine, guid="n3", title="New but hidden")
|
||||||
|
Article.objects.create(feed=theirs, guid="n4", title="Someone else's feed")
|
||||||
|
UserArticleState.objects.create(user=alice, article=already_read, is_read=True)
|
||||||
|
UserArticleState.objects.create(user=alice, article=hidden, is_hidden=True)
|
||||||
|
|
||||||
|
client.login(username="alice", password="pw-alice-123")
|
||||||
|
body = client.get(reverse("reader:new_since"), {"since": cursor}).json()
|
||||||
|
|
||||||
|
assert body["new_count"] == 1
|
||||||
|
assert new_unread.pk > cursor
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.django_db
|
||||||
|
def test_authenticated_pages_load_the_tab_badge_script_with_its_endpoint(client, alice):
|
||||||
|
client.login(username="alice", password="pw-alice-123")
|
||||||
|
content = client.get(reverse("reader:stream")).content.decode()
|
||||||
|
|
||||||
|
assert "js/tab-badge" in content
|
||||||
|
assert f'data-url="{reverse("reader:new_since")}"' in content
|
||||||
|
client.logout()
|
||||||
|
assert "tab-badge" not in client.get(reverse("accounts:login")).content.decode()
|
||||||
@@ -7,6 +7,7 @@ app_name = "reader"
|
|||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path("", views.stream, name="stream"),
|
path("", views.stream, name="stream"),
|
||||||
path("check-new/", views.check_new_articles, name="check_new"),
|
path("check-new/", views.check_new_articles, name="check_new"),
|
||||||
|
path("new-since/", views.new_since, name="new_since"),
|
||||||
path("mark-all-read/", views.mark_all_read, name="mark_all_read"),
|
path("mark-all-read/", views.mark_all_read, name="mark_all_read"),
|
||||||
path("articles/<int:article_id>/", views.article_detail, name="article_detail"),
|
path("articles/<int:article_id>/", views.article_detail, name="article_detail"),
|
||||||
path(
|
path(
|
||||||
|
|||||||
+24
-1
@@ -5,7 +5,7 @@ from urllib.parse import urlencode
|
|||||||
from django.contrib.auth.decorators import login_required
|
from django.contrib.auth.decorators import login_required
|
||||||
from django.db.models import Max, Q
|
from django.db.models import Max, Q
|
||||||
from django.db.models.functions import Coalesce
|
from django.db.models.functions import Coalesce
|
||||||
from django.http import Http404
|
from django.http import Http404, JsonResponse
|
||||||
from django.shortcuts import get_object_or_404, redirect, render
|
from django.shortcuts import get_object_or_404, redirect, render
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
@@ -258,6 +258,29 @@ def check_new_articles(request):
|
|||||||
return render(request, "reader/_new_articles_banner.html", {"new_count": new_count})
|
return render(request, "reader/_new_articles_banner.html", {"new_count": new_count})
|
||||||
|
|
||||||
|
|
||||||
|
@login_required
|
||||||
|
def new_since(request):
|
||||||
|
"""Feeds the browser-tab dot (static/js/tab-badge.js) on every page:
|
||||||
|
without ?since it returns the current highest article id, which the
|
||||||
|
page keeps as its baseline; with it, how many unread, non-hidden
|
||||||
|
articles from the user's subscriptions have appeared after that id.
|
||||||
|
Deliberately not scoped to the stream's current filter/tag/feed — the
|
||||||
|
tab should light up for anything new, whichever page is open."""
|
||||||
|
latest_id = Article.objects.aggregate(Max("id"))["id__max"] or 0
|
||||||
|
try:
|
||||||
|
since_id = int(request.GET["since"])
|
||||||
|
except (KeyError, ValueError):
|
||||||
|
return JsonResponse({"latest_id": latest_id, "new_count": 0})
|
||||||
|
|
||||||
|
new_count = (
|
||||||
|
_base_queryset(request.user)
|
||||||
|
.filter(id__gt=since_id)
|
||||||
|
.exclude(user_states__user=request.user, user_states__is_read=True)
|
||||||
|
.count()
|
||||||
|
)
|
||||||
|
return JsonResponse({"latest_id": latest_id, "new_count": new_count})
|
||||||
|
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
@require_POST
|
@require_POST
|
||||||
def mark_all_read(request):
|
def mark_all_read(request):
|
||||||
|
|||||||
+22
-6
@@ -314,17 +314,33 @@ misconfigured feed or an outage at the source.
|
|||||||
|
|
||||||
`/health/` is an unauthenticated liveness check (DB connectivity only) —
|
`/health/` is an unauthenticated liveness check (DB connectivity only) —
|
||||||
`scripts/upgrade.sh` uses it to confirm a deploy actually came back up.
|
`scripts/upgrade.sh` uses it to confirm a deploy actually came back up.
|
||||||
Point an external uptime monitor at `/health/ready/` instead: it also checks
|
Point an external uptime monitor at `/health/ready/` instead. It returns 503
|
||||||
Redis and that the background poller has ticked in the last five minutes
|
with a per-check breakdown when any of these fail:
|
||||||
(proving Celery beat is scheduling *and* a worker is consuming), and returns
|
|
||||||
503 with a per-check breakdown when any of them fails — so "the site loads
|
- `database` and `redis` — reachable.
|
||||||
but nothing is being fetched" actually alerts.
|
- `poll_heartbeat` — the background poller ticked in the last five minutes
|
||||||
|
(proves Celery beat is scheduling *and* a worker is consuming).
|
||||||
|
- `queue_depth` — no more than 1000 tasks waiting in the Celery queue
|
||||||
|
(workers aren't keeping up, or aren't running).
|
||||||
|
- `recent_fetch_success` — at least one feed has been fetched successfully
|
||||||
|
within three poll intervals (minimum 30 minutes), so "scheduled but every
|
||||||
|
fetch is failing" is caught. A brand-new install gets that long before it
|
||||||
|
counts as stale.
|
||||||
|
- `feed_failures` — no more than half of your active feeds are failing, once
|
||||||
|
you have at least four (a handful of dead feeds is normal; most of them
|
||||||
|
failing at once points at your server, DNS, or outbound network).
|
||||||
|
|
||||||
|
The payload also reports `queue_depth`, `active_feeds`, `failing_feeds`, and
|
||||||
|
the age of the last successful fetch, so you can graph them.
|
||||||
|
|
||||||
**Retention.** By default every fetched article is kept forever. In the Django
|
**Retention.** By default every fetched article is kept forever. In the Django
|
||||||
admin, "Site settings" has a *Retention days* field: set it to delete articles
|
admin, "Site settings" has a *Retention days* field: set it to delete articles
|
||||||
fetched longer ago than that, nightly (03:30 UTC, via Celery beat). Articles
|
fetched longer ago than that, nightly (03:30 UTC, via Celery beat). Articles
|
||||||
anyone has starred or saved for later are always kept; read state and tags for
|
anyone has starred or saved for later are always kept; read state and tags for
|
||||||
deleted articles go with them. The dashboard shows the article count and the
|
deleted articles go with them. A small record (feed and entry id) is kept for
|
||||||
|
each deleted article so a feed that still lists it can't bring it back as new
|
||||||
|
and unread, and entries whose own publish date is already older than the window
|
||||||
|
aren't fetched in the first place. The dashboard shows the article count and the
|
||||||
age of the oldest one, so you can see what retention is doing.
|
age of the oldest one, so you can see what retention is doing.
|
||||||
|
|
||||||
**SQLite.** SQLite installs run in WAL mode with a 20-second busy timeout (set
|
**SQLite.** SQLite installs run in WAL mode with a 20-second busy timeout (set
|
||||||
|
|||||||
@@ -0,0 +1,85 @@
|
|||||||
|
// Marks the browser tab with a red dot on its favicon when new unread
|
||||||
|
// articles have arrived since this page loaded — visible from a background
|
||||||
|
// tab, unlike the in-page "new articles" banner. Runs on every
|
||||||
|
// authenticated page; a full page load resets the baseline (and the icon),
|
||||||
|
// which is exactly when the user has seen the new articles.
|
||||||
|
(function () {
|
||||||
|
var POLL_MS = 30000;
|
||||||
|
var url = document.currentScript && document.currentScript.dataset.url;
|
||||||
|
var icon = document.querySelector('link[rel="icon"]');
|
||||||
|
if (!url || !icon) return;
|
||||||
|
|
||||||
|
var originalHref = icon.href;
|
||||||
|
var baseline = null;
|
||||||
|
var badged = null; // the dotted icon's data: URL, drawn once on first need
|
||||||
|
var showing = false;
|
||||||
|
|
||||||
|
function drawDot() {
|
||||||
|
return new Promise(function (resolve) {
|
||||||
|
var image = new Image();
|
||||||
|
image.onload = function () {
|
||||||
|
var size = 64;
|
||||||
|
var canvas = document.createElement("canvas");
|
||||||
|
canvas.width = canvas.height = size;
|
||||||
|
var ctx = canvas.getContext("2d");
|
||||||
|
ctx.drawImage(image, 0, 0, size, size);
|
||||||
|
ctx.beginPath();
|
||||||
|
ctx.arc(size * 0.74, size * 0.26, size * 0.23, 0, 2 * Math.PI);
|
||||||
|
ctx.fillStyle = "#e53935";
|
||||||
|
ctx.fill();
|
||||||
|
ctx.lineWidth = size * 0.07;
|
||||||
|
ctx.strokeStyle = "#ffffff";
|
||||||
|
ctx.stroke();
|
||||||
|
resolve(canvas.toDataURL("image/png"));
|
||||||
|
};
|
||||||
|
image.onerror = function () {
|
||||||
|
resolve(null);
|
||||||
|
};
|
||||||
|
image.src = originalHref;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Replace the <link> rather than editing href: some browsers ignore an
|
||||||
|
// in-place change to an existing favicon element.
|
||||||
|
function setIcon(href) {
|
||||||
|
var fresh = icon.cloneNode();
|
||||||
|
fresh.href = href;
|
||||||
|
icon.parentNode.replaceChild(fresh, icon);
|
||||||
|
icon = fresh;
|
||||||
|
}
|
||||||
|
|
||||||
|
function showDot(on) {
|
||||||
|
if (on === showing) return Promise.resolve();
|
||||||
|
showing = on;
|
||||||
|
if (!on) {
|
||||||
|
setIcon(originalHref);
|
||||||
|
return Promise.resolve();
|
||||||
|
}
|
||||||
|
var ready = badged ? Promise.resolve(badged) : drawDot();
|
||||||
|
return ready.then(function (href) {
|
||||||
|
badged = href;
|
||||||
|
if (href && showing) setIcon(href);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function poll() {
|
||||||
|
var query = baseline === null ? "" : "?since=" + baseline;
|
||||||
|
fetch(url + query, { credentials: "same-origin", headers: { Accept: "application/json" } })
|
||||||
|
.then(function (response) {
|
||||||
|
// A redirect means the session ended (we'd be reading the login
|
||||||
|
// page) — stop polling instead of hammering it.
|
||||||
|
if (!response.ok || response.redirected) throw new Error("stop");
|
||||||
|
return response.json();
|
||||||
|
})
|
||||||
|
.then(function (data) {
|
||||||
|
if (baseline === null) baseline = data.latest_id;
|
||||||
|
else showDot(data.new_count > 0);
|
||||||
|
setTimeout(poll, POLL_MS);
|
||||||
|
})
|
||||||
|
.catch(function (error) {
|
||||||
|
if (error.message !== "stop") setTimeout(poll, POLL_MS);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
poll();
|
||||||
|
})();
|
||||||
@@ -58,6 +58,7 @@
|
|||||||
{% if user.is_authenticated %}
|
{% if user.is_authenticated %}
|
||||||
<script src="{% static 'js/theme.js' %}" defer></script>
|
<script src="{% static 'js/theme.js' %}" defer></script>
|
||||||
<script src="{% static 'js/sidebar-state.js' %}" defer></script>
|
<script src="{% static 'js/sidebar-state.js' %}" defer></script>
|
||||||
|
<script src="{% static 'js/tab-badge.js' %}" data-url="{% url 'reader:new_since' %}" defer></script>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
<script src="{% static 'js/register-sw.js' %}" defer></script>
|
<script src="{% static 'js/register-sw.js' %}" defer></script>
|
||||||
</body>
|
</body>
|
||||||
|
|||||||
Reference in new issue
Block a user