diff options
Diffstat (limited to 'webmentions_ssg')
| -rw-r--r-- | webmentions_ssg/tasks/sender.py | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/webmentions_ssg/tasks/sender.py b/webmentions_ssg/tasks/sender.py index 4aa59ee..a4a8367 100644 --- a/webmentions_ssg/tasks/sender.py +++ b/webmentions_ssg/tasks/sender.py @@ -13,6 +13,7 @@ import httpx import sqlalchemy as sa from bs4 import BeautifulSoup from flask import current_app +from huey.exceptions import TaskLockedException from .. import APP_NAME, VERSION from .. import DATABASE as db @@ -308,6 +309,20 @@ def send_webmention(webmention_uuid: uuid.UUID) -> None: :param webmention_uuid: Identifier of the sent Webmention to process. """ + # Huey locks have static names when used as decorators. Acquire one keyed by + # UUID inside the task so different Webmentions can still send in parallel. + # The pending/retry checks must happen *under* this lock. + try: + with huey.lock_task(f"send-webmention-{webmention_uuid}"): + send_webmention_task(webmention_uuid) + except TaskLockedException: + # Another worker is already sending this Webmention. The active worker + # owns its result; a later scan will requeue it if it remains pending. + return + + +def send_webmention_task(webmention_uuid: uuid.UUID) -> None: + """Process a Webmention while holding its per-UUID task lock.""" webmention = db.session.get(SentWebmention, webmention_uuid) if webmention is None: |
