aboutsummaryrefslogtreecommitdiff
path: root/webmentions_ssg/tasks/receiver.py
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--webmentions_ssg/tasks/receiver.py148
1 files changed, 84 insertions, 64 deletions
diff --git a/webmentions_ssg/tasks/receiver.py b/webmentions_ssg/tasks/receiver.py
index 1df363f..31fb64c 100644
--- a/webmentions_ssg/tasks/receiver.py
+++ b/webmentions_ssg/tasks/receiver.py
@@ -5,6 +5,7 @@
"""Fetch and verify incoming Webmentions with transactional state changes."""
import uuid
+from dataclasses import dataclass
from datetime import UTC, datetime, timedelta
from http import HTTPStatus
@@ -123,105 +124,124 @@ def source_mentions_target(source_url: str, target_url: str) -> bool:
)
-@huey.task(retries=2, retry_delay=50)
-def verify_webmention(webmention_uuid: uuid.UUID) -> None:
- """Verify a received webmention and persist the result.
+@dataclass(frozen=True)
+class VerificationClaim:
+ """Source and lease version for one atomically claimed verification."""
- The transition into ``verifying`` is an atomic claim. Its ``updated_at``
- timestamp acts as a lease version: if a reaper has reclaimed the row while
- the fetch was in progress, the old worker cannot overwrite the new result.
- Temporary fetch failures are persisted and re-raised for Huey's retries.
+ identifier: uuid.UUID
+ source: str
+ target: str
+ started_at: datetime
- :param webmention_uuid: Identifier of the received webmention to verify.
- :raises TemporaryFetchError: If fetching the source fails temporarily.
- :raises httpx.RequestError: If an HTTP request error occurs while fetching
- the source.
- """
+
+def claim_eligible_row(webmention_uuid: uuid.UUID) -> VerificationClaim | None:
+ """Claim an eligible row, returning its source, target, and lease version."""
webmention = db.session.get(ReceivedWebmention, webmention_uuid)
if webmention is None:
current_app.logger.warning(
"Cannot verify unknown ReceivedWebmention %s", webmention_uuid
)
- return
+ return None
if webmention.status not in {ReceivedStatus.RECEIVED, ReceivedStatus.FAILED}:
- return
+ return None
if (
webmention.status == ReceivedStatus.FAILED
and webmention.failure_reason == RECOVERY_EXHAUSTED_REASON
):
- return
+ return None
- previous_status = webmention.status
- previous_updated_at = webmention.updated_at
- source, target = webmention.source, webmention.target
- started_at = datetime.now(UTC)
-
- claimed = db.session.execute(
+ claim = VerificationClaim(
+ identifier=webmention_uuid,
+ source=webmention.source,
+ target=webmention.target,
+ started_at=datetime.now(UTC),
+ )
+ changed = db.session.execute(
sa.update(ReceivedWebmention)
.where(
ReceivedWebmention.uuid == webmention_uuid,
- ReceivedWebmention.status == previous_status,
- ReceivedWebmention.updated_at == previous_updated_at,
+ ReceivedWebmention.status == webmention.status,
+ ReceivedWebmention.updated_at == webmention.updated_at,
)
.values(
- status=ReceivedStatus.VERIFYING, failure_reason=None, updated_at=started_at
+ status=ReceivedStatus.VERIFYING,
+ failure_reason=None,
+ updated_at=claim.started_at,
)
)
- if rows_affected(claimed) != 1:
+ if rows_affected(changed) != 1:
db.session.rollback()
- return
+ return None
db.session.commit()
- status = None
- failure_reason = None
+ return claim
+
+def check_source(claim: VerificationClaim) -> tuple[ReceivedStatus, str | None]:
+ """Map verification results and permanent errors to a final status/reason."""
try:
- mentions_target = source_mentions_target(source, target)
+ mentions_target = source_mentions_target(claim.source, claim.target)
except SourceGoneError as exc:
- status = ReceivedStatus.DELETED
- failure_reason = str(exc)
+ return ReceivedStatus.DELETED, str(exc)
except VerificationError as exc:
- status = ReceivedStatus.FAILED
- failure_reason = str(exc)
- except (TemporaryFetchError, httpx.RequestError) as exc:
- status = ReceivedStatus.FAILED
- failure_reason = str(exc) or "Source could not be fetched"
- raise
+ return ReceivedStatus.FAILED, str(exc)
+ except TemporaryFetchError, httpx.RequestError:
+ raise # These errors must be persisted and then retried by Huey.
except Exception as exc:
- # InvalidURL is not a RequestError; other unexpected source errors must
- # also leave the row in a terminal state, without an endless retry loop.
+ # InvalidURL is not a RequestError; unexpected source errors are terminal.
current_app.logger.exception(
- "Unexpected error verifying ReceivedWebmention %s", webmention_uuid
+ "Unexpected error verifying ReceivedWebmention %s", claim.identifier
)
+ return ReceivedStatus.FAILED, str(exc) or type(exc).__name__
- status = ReceivedStatus.FAILED
- failure_reason = str(exc) or type(exc).__name__
- else:
- if mentions_target:
- status = ReceivedStatus.VERIFIED
- else:
- status = ReceivedStatus.DELETED
- failure_reason = "Source does not mention target"
- finally:
- if status is not None:
- db.session.execute(
- sa.update(ReceivedWebmention)
- .where(
- ReceivedWebmention.uuid == webmention_uuid,
- ReceivedWebmention.status == ReceivedStatus.VERIFYING,
- ReceivedWebmention.updated_at == started_at,
- )
- .values(
- status=status,
- failure_reason=failure_reason,
- updated_at=datetime.now(UTC),
- )
- )
- db.session.commit()
+ if mentions_target:
+ return ReceivedStatus.VERIFIED, None
+ return ReceivedStatus.DELETED, "Source does not mention target"
+
+
+def release_claim(
+ claim: VerificationClaim, status: ReceivedStatus, reason: str | None
+) -> bool:
+ """Persist an outcome only if this worker still owns the verification lease."""
+ changed = db.session.execute(
+ sa.update(ReceivedWebmention)
+ .where(
+ ReceivedWebmention.uuid == claim.identifier,
+ ReceivedWebmention.status == ReceivedStatus.VERIFYING,
+ ReceivedWebmention.updated_at == claim.started_at,
+ )
+ .values(status=status, failure_reason=reason, updated_at=datetime.now(UTC))
+ )
+ db.session.commit()
+ return rows_affected(changed) == 1
+
+
+@huey.task(retries=2, retry_delay=50)
+def verify_webmention(webmention_uuid: uuid.UUID) -> None:
+ """Claim and verify a received Webmention, preserving its lease on release.
+
+ Temporary fetch failures are recorded and re-raised for Huey's retries.
+
+ :param webmention_uuid: Identifier of the received webmention to verify.
+ :raises TemporaryFetchError: If fetching the source fails temporarily.
+ :raises httpx.RequestError: If an HTTP request fails while fetching.
+ """
+ if (claim := claim_eligible_row(webmention_uuid)) is None:
+ return
+
+ try:
+ status, reason = check_source(claim)
+ except (TemporaryFetchError, httpx.RequestError) as exc:
+ release_claim(
+ claim, ReceivedStatus.FAILED, str(exc) or "Source could not be fetched"
+ )
+ raise
+
+ release_claim(claim, status, reason)
def recover_stale_webmentions() -> int: