diff options
| author | Dennis Fink | 2026-10-30 13:52:27 +0100 |
|---|---|---|
| committer | Dennis Fink | 2026-10-30 13:52:27 +0100 |
| commit | 1a9e7a49240533bb7b7f7b8fd0527f12d2a029bf (patch) | |
| tree | 3c8ce2455b4d9b101753b3134eb00e900de2bcaa | |
| parent | 33567a493a18c3a8e17e3715874d49943da494a6 (diff) | |
| download | webmentions-ssg-1a9e7a49240533bb7b7f7b8fd0527f12d2a029bf.tar.gz webmentions-ssg-1a9e7a49240533bb7b7f7b8fd0527f12d2a029bf.zip | |
fix(sender): retry manual scans on lock contention
Ensure requested rescans are not silently dropped when a periodic scan
already holds the scanner lock.
Defer the manual task and retry it after the active scan finishes while
still propagating unrelated scan failures.
| -rw-r--r-- | tests/tasks/test_scanner.py | 44 | ||||
| -rw-r--r-- | webmentions_ssg/tasks/scanner.py | 24 |
2 files changed, 63 insertions, 5 deletions
diff --git a/tests/tasks/test_scanner.py b/tests/tasks/test_scanner.py index 6ace77f..21a13c6 100644 --- a/tests/tasks/test_scanner.py +++ b/tests/tasks/test_scanner.py @@ -13,6 +13,7 @@ import pytest import sqlalchemy as sa from bs4 import BeautifulSoup from flask import Flask +from huey.exceptions import RetryTask from pytest import MonkeyPatch from webmentions_ssg import DATABASE as db @@ -36,6 +37,49 @@ def run_scan(app: Flask, scanner_module: ModuleType) -> None: scanner_module.scan_sources() +def test_manual_scan_retries_when_periodic_scan_holds_lock( + app: Flask, scanner_module: ModuleType +) -> None: + with app.app_context(): + # Import-time decorators previously captured an old backend here, + # making this test pass or fail depending on module import order. + previous_backend = scanner_module.huey.huey + scanner_module.huey.init_app(app) + assert scanner_module.huey.huey is not previous_backend + + with scanner_module.huey.lock_task("scan-webmention-sources"): + with pytest.raises(RetryTask) as exc_info: + scanner_module.manual_scan_sources.call_local() + + assert exc_info.value.delay == 30 + + +def test_manual_scan_runs_without_lock_contention( + app: Flask, scanner_module: ModuleType, tmp_path: Path +) -> None: + configure_scanner(app, tmp_path) + write_post(tmp_path, "manual", "Hello!") + + with app.app_context(): + scanner_module.manual_scan_sources.call_local() + source = db.session.scalar( + sa.select(Source).where(Source.path == "manual/index.html") + ) + assert source is not None + assert source.url == f"{BASE_URL}manual/" + + +def test_manual_scan_propagates_unrelated_errors( + scanner_module: ModuleType, monkeypatch: MonkeyPatch +) -> None: + monkeypatch.setattr( + scanner_module, "scan_sources", Mock(side_effect=RuntimeError("scan failed")) + ) + + with pytest.raises(RuntimeError, match="scan failed"): + scanner_module.manual_scan_sources.call_local() + + def parse_html(html: str) -> BeautifulSoup: return BeautifulSoup(html, "html5lib") 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 |
