diff options
Diffstat (limited to 'webmentions_ssg/tasks/scanner.py')
| -rw-r--r-- | webmentions_ssg/tasks/scanner.py | 24 |
1 files changed, 19 insertions, 5 deletions
diff --git a/webmentions_ssg/tasks/scanner.py b/webmentions_ssg/tasks/scanner.py index 03f4b8d..298fed7 100644 --- a/webmentions_ssg/tasks/scanner.py +++ b/webmentions_ssg/tasks/scanner.py @@ -18,6 +18,7 @@ import sqlalchemy as sa import xxhash from bs4 import BeautifulSoup, Tag from flask import current_app +from huey.exceptions import RetryTask, TaskLockedException from sqlalchemy.orm import selectinload from .. import DATABASE as db @@ -402,7 +403,6 @@ def update_source( return source -@huey.lock_task("scan-webmention-sources") def scan_sources() -> None: """ Scan generated source documents and queue pending Webmentions. @@ -415,6 +415,15 @@ def scan_sources() -> None: :raises RuntimeError: If the configured source directory or base URL is invalid. """ + # Resolve the lock at execution time. The global Huey extension can be + # reinitialized by Flask app factories (notably in the test suite), so an + # import-time lock decorator may point at an obsolete storage backend. + with huey.lock_task("scan-webmention-sources"): + scan_sources_task() + + +def scan_sources_task() -> None: + """Scan the sources while the caller holds the shared scan lock.""" directory = current_app.config.get("SOURCE_DIRECTORY") if directory is None: @@ -560,7 +569,12 @@ def scan_sources() -> None: @huey.task() def manual_scan_sources() -> None: - """ - Run a source scan as a Huey task. - """ - return scan_sources() + """Run a source scan, deferring it while another scan holds the lock.""" + try: + scan_sources() + except TaskLockedException as exc: + # Unlike a periodic scan, a requested manual scan must not be discarded + # merely because another scan is running. RetryTask schedules another + # attempt without consuming a finite retry budget. + current_app.logger.info("Source scan already running; deferring manual scan") + raise RetryTask(delay=30) from exc |
