aboutsummaryrefslogtreecommitdiff
path: root/webmentions_ssg/tasks/verification.py
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--webmentions_ssg/tasks/verification.py (renamed from webmentions_ssg/tasks/receiver.py)47
1 files changed, 26 insertions, 21 deletions
diff --git a/webmentions_ssg/tasks/receiver.py b/webmentions_ssg/tasks/verification.py
index 313a8e4..a614815 100644
--- a/webmentions_ssg/tasks/receiver.py
+++ b/webmentions_ssg/tasks/verification.py
@@ -15,7 +15,7 @@ from flask import current_app
from ..db_helpers import rows_affected
from ..extensions import db, huey
-from ..models import ReceivedStatus, ReceivedWebmention
+from ..models import ReceivedWebmention, ReceivedWebmentionStatus
from ..net import (
AddressResolutionError,
ResponseTooLargeError,
@@ -149,11 +149,14 @@ def claim_for_verification(webmention_uuid: uuid.UUID) -> VerificationClaim | No
)
return None
- if webmention.status not in {ReceivedStatus.RECEIVED, ReceivedStatus.FAILED}:
+ if webmention.status not in {
+ ReceivedWebmentionStatus.RECEIVED,
+ ReceivedWebmentionStatus.FAILED,
+ }:
return None
if (
- webmention.status == ReceivedStatus.FAILED
+ webmention.status == ReceivedWebmentionStatus.FAILED
and webmention.failure_reason == RECOVERY_EXHAUSTED_REASON
):
return None
@@ -172,7 +175,7 @@ def claim_for_verification(webmention_uuid: uuid.UUID) -> VerificationClaim | No
ReceivedWebmention.updated_at == webmention.updated_at,
)
.values(
- status=ReceivedStatus.VERIFYING,
+ status=ReceivedWebmentionStatus.VERIFYING,
failure_reason=None,
updated_at=claim.lease_started_at,
)
@@ -188,7 +191,7 @@ def claim_for_verification(webmention_uuid: uuid.UUID) -> VerificationClaim | No
def determine_verification_outcome(
claim: VerificationClaim,
-) -> tuple[ReceivedStatus, str | None]:
+) -> tuple[ReceivedWebmentionStatus, str | None]:
"""Map verification results and permanent errors to a final status/reason.
:param claim: Claim containing the source and target URLs to verify.
@@ -200,9 +203,9 @@ def determine_verification_outcome(
try:
mentions_target = source_mentions_target(claim.source, claim.target)
except SourceGoneError as exc:
- return ReceivedStatus.DELETED, str(exc)
+ return ReceivedWebmentionStatus.SOURCE_REMOVED, str(exc)
except VerificationError as exc:
- return ReceivedStatus.FAILED, str(exc)
+ return ReceivedWebmentionStatus.FAILED, str(exc)
except RetryableFetchError, httpx.RequestError:
raise # These errors must be persisted and then retried by Huey.
except Exception as exc:
@@ -210,28 +213,28 @@ def determine_verification_outcome(
current_app.logger.exception(
"Unexpected error verifying ReceivedWebmention %s", claim.webmention_uuid
)
- return ReceivedStatus.FAILED, str(exc) or type(exc).__name__
+ return ReceivedWebmentionStatus.FAILED, str(exc) or type(exc).__name__
if mentions_target:
- return ReceivedStatus.VERIFIED, None
- return ReceivedStatus.DELETED, "Source does not mention target"
+ return ReceivedWebmentionStatus.VERIFIED, None
+ return ReceivedWebmentionStatus.SOURCE_REMOVED, "Source does not mention target"
-def release_claim(
- claim: VerificationClaim, status: ReceivedStatus, reason: str | None
+def store_verification_outcome(
+ claim: VerificationClaim, status: ReceivedWebmentionStatus, reason: str | None
) -> bool:
"""Persist an outcome only if this worker still owns the verification lease.
:param claim: Verification lease acquired by this worker.
:param status: Final status to persist for the claimed Webmention.
- :param reason: Optional explanation for a failed or deleted Webmention.
+ :param reason: Optional explanation for a failed or source-removed Webmention.
:returns: ``True`` if the worker still owned the lease and persisted the outcome.
"""
changed = db.session.execute(
sa.update(ReceivedWebmention)
.where(
ReceivedWebmention.uuid == claim.webmention_uuid,
- ReceivedWebmention.status == ReceivedStatus.VERIFYING,
+ ReceivedWebmention.status == ReceivedWebmentionStatus.VERIFYING,
ReceivedWebmention.updated_at == claim.lease_started_at,
)
.values(status=status, failure_reason=reason, updated_at=datetime.now(UTC))
@@ -259,12 +262,14 @@ def verify_webmention(webmention_uuid: uuid.UUID) -> None:
try:
status, reason = determine_verification_outcome(claim)
except (RetryableFetchError, httpx.RequestError) as exc:
- release_claim(
- claim, ReceivedStatus.FAILED, str(exc) or "Source could not be fetched"
+ store_verification_outcome(
+ claim,
+ ReceivedWebmentionStatus.FAILED,
+ str(exc) or "Source could not be fetched",
)
raise
- release_claim(claim, status, reason)
+ store_verification_outcome(claim, status, reason)
def recover_stale_verifications() -> int:
@@ -296,7 +301,7 @@ def recover_stale_verifications() -> int:
ReceivedWebmention.recovery_attempts,
).where(
ReceivedWebmention.status.in_(
- (ReceivedStatus.VERIFYING, ReceivedStatus.RECEIVED)
+ (ReceivedWebmentionStatus.VERIFYING, ReceivedWebmentionStatus.RECEIVED)
),
ReceivedWebmention.updated_at <= cutoff,
)
@@ -319,14 +324,14 @@ def recover_stale_verifications() -> int:
.values(
**(
{
- "status": ReceivedStatus.FAILED,
+ "status": ReceivedWebmentionStatus.FAILED,
"failure_reason": RECOVERY_EXHAUSTED_REASON,
"updated_at": now,
"recovery_attempts": recovery_attempts,
}
if at_limit
- else ReceivedWebmention.verification_reset_values(
- at=now, recoveries=recovery_attempts + 1
+ else ReceivedWebmention.reopen_verification_values(
+ at=now, recovery_attempts=recovery_attempts + 1
)
)
)