aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--tests/test_models.py42
-rw-r--r--webmentions_ssg/models.py13
-rw-r--r--webmentions_ssg/tasks/scanner.py5
-rw-r--r--webmentions_ssg/tasks/sender.py20
4 files changed, 59 insertions, 21 deletions
diff --git a/tests/test_models.py b/tests/test_models.py
index b1eed94..7ec7df7 100644
--- a/tests/test_models.py
+++ b/tests/test_models.py
@@ -77,6 +77,48 @@ def test_sent_webmention_pending(
assert webmention.pending is expected
+def test_sent_webmention_pending_sql_matches_python(app: Flask) -> None:
+ cases = [(1, None, True), (1, 0, True), (2, 1, True), (1, 1, False), (1, 2, False)]
+
+ with app.app_context():
+ now = datetime.now(UTC)
+ source = Source(
+ path="pending/index.html",
+ url="https://site.example/pending/",
+ content_hash="0" * 32,
+ last_seen_at=now,
+ revised_at=now,
+ )
+ for i, (desired, processed, _) in enumerate(cases):
+ source.sent_webmentions.append(
+ SentWebmention(
+ target=f"https://example.com/post/{i}",
+ desired_revision=desired,
+ processed_revision=processed,
+ )
+ )
+ db.session.add(source)
+ db.session.commit()
+
+ pending_ids = set(
+ db.session.scalars(
+ sa.select(SentWebmention.uuid).where(SentWebmention.pending)
+ )
+ )
+ for webmention, (_, _, expected) in zip(
+ source.sent_webmentions, cases, strict=True
+ ):
+ assert webmention.pending is expected
+ assert (webmention.uuid in pending_ids) is expected
+
+ result = db.session.execute(
+ sa.update(SentWebmention)
+ .where(SentWebmention.pending)
+ .values(attempt_count=1)
+ )
+ assert result.rowcount == 3
+
+
def test_uuid7_to_datetime() -> None:
identifier = uuid.uuid7()
timestamp = uuid7_to_datetime(identifier)
diff --git a/webmentions_ssg/models.py b/webmentions_ssg/models.py
index 37e5f0e..ae2f076 100644
--- a/webmentions_ssg/models.py
+++ b/webmentions_ssg/models.py
@@ -13,6 +13,7 @@ from hashlib import sha256
from typing import Never
from uuid import UUID, uuid7
+import sqlalchemy as sa
from flask_login import UserMixin
from sqlalchemy import (
Boolean,
@@ -26,6 +27,7 @@ from sqlalchemy import (
UniqueConstraint,
Uuid,
)
+from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.orm import Mapped, mapped_column, relationship, validates
from werkzeug.security import check_password_hash, generate_password_hash
@@ -316,7 +318,7 @@ class SentWebmention(Base):
return self
- @property
+ @hybrid_property
def pending(self) -> bool:
"""Return whether the Webmention has an unprocessed source revision."""
return (
@@ -324,6 +326,15 @@ class SentWebmention(Base):
or self.processed_revision < self.desired_revision
)
+ @pending.inplace.expression
+ @classmethod
+ def _pending_expression(cls) -> sa.ColumnElement[bool]:
+ """Express pending-revision status in database queries."""
+ return sa.or_(
+ cls.processed_revision.is_(None),
+ cls.processed_revision < cls.desired_revision,
+ )
+
@property
def created_at(self) -> datetime:
"""Return the creation time encoded in the Webmention UUID."""
diff --git a/webmentions_ssg/tasks/scanner.py b/webmentions_ssg/tasks/scanner.py
index 263b8a5..f3665ee 100644
--- a/webmentions_ssg/tasks/scanner.py
+++ b/webmentions_ssg/tasks/scanner.py
@@ -226,10 +226,7 @@ def scan_sources_task() -> None:
for identifier in db.session.scalars(
sa.select(SentWebmention.uuid)
.where(
- sa.or_(
- SentWebmention.processed_revision.is_(None),
- SentWebmention.processed_revision < SentWebmention.desired_revision,
- ),
+ SentWebmention.pending,
sa.or_(
SentWebmention.next_retry_at.is_(None),
SentWebmention.next_retry_at <= scan_time,
diff --git a/webmentions_ssg/tasks/sender.py b/webmentions_ssg/tasks/sender.py
index cd6c64a..804b6f6 100644
--- a/webmentions_ssg/tasks/sender.py
+++ b/webmentions_ssg/tasks/sender.py
@@ -43,10 +43,7 @@ def attempt_is_current(webmention: SentWebmention, revision: int) -> bool:
"""
db.session.refresh(webmention)
- return webmention.desired_revision == revision and (
- webmention.processed_revision is None
- or webmention.processed_revision < revision
- )
+ return webmention.desired_revision == revision and webmention.pending
def retry_delay(attempt: int) -> timedelta:
@@ -89,10 +86,7 @@ def send_webmention_task(webmention_uuid: uuid.UUID) -> None:
)
return
- if (
- webmention.processed_revision is not None
- and webmention.processed_revision >= webmention.desired_revision
- ):
+ if not webmention.pending:
return
revision = webmention.desired_revision
@@ -116,10 +110,7 @@ def send_webmention_task(webmention_uuid: uuid.UUID) -> None:
SentWebmention.uuid == webmention_uuid,
SentWebmention.desired_revision == revision,
SentWebmention.attempt_count >= max_attempts,
- sa.or_(
- SentWebmention.processed_revision.is_(None),
- SentWebmention.processed_revision < revision,
- ),
+ SentWebmention.pending,
sa.or_(
SentWebmention.next_retry_at.is_(None),
SentWebmention.next_retry_at <= now,
@@ -150,10 +141,7 @@ def send_webmention_task(webmention_uuid: uuid.UUID) -> None:
SentWebmention.uuid == webmention_uuid,
SentWebmention.desired_revision == revision,
SentWebmention.attempt_count == attempt - 1,
- sa.or_(
- SentWebmention.processed_revision.is_(None),
- SentWebmention.processed_revision < revision,
- ),
+ SentWebmention.pending,
sa.or_(
SentWebmention.next_retry_at.is_(None),
SentWebmention.next_retry_at <= now,