diff options
| author | Dennis Fink | 2026-10-31 20:43:33 +0100 |
|---|---|---|
| committer | Dennis Fink | 2026-10-31 20:43:33 +0100 |
| commit | 7035e9f9467684cccae2b7ff3faaa62e6a9f91aa (patch) | |
| tree | 8d14e16fa0b669353052ee51eeeb1da60fa16d78 /webmentions_ssg/__init__.py | |
| parent | 3a3284abe23e143cbb638877f47958c82afa3367 (diff) | |
| download | webmentions-ssg-7035e9f9467684cccae2b7ff3faaa62e6a9f91aa.tar.gz webmentions-ssg-7035e9f9467684cccae2b7ff3faaa62e6a9f91aa.zip | |
fix(core): harden local setup and Huey configuration
Create local development and test directories automatically so fresh
checkouts can start without pre-existing logging or instance paths.
Reject in-memory and blackhole Huey backends in production because they
cannot reliably deliver queued work across separate web and worker
processes.
Diffstat (limited to 'webmentions_ssg/__init__.py')
| -rw-r--r-- | webmentions_ssg/__init__.py | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/webmentions_ssg/__init__.py b/webmentions_ssg/__init__.py index f8fd8f1..8fed1a7 100644 --- a/webmentions_ssg/__init__.py +++ b/webmentions_ssg/__init__.py @@ -8,6 +8,7 @@ import logging import logging.handlers import os from pathlib import Path +from urllib.parse import urlsplit import coloredlogs from flask import Flask @@ -75,7 +76,12 @@ def create_app(config_class: type[DefaultConfig] | None = None) -> Flask: config_class = DevelopmentConfig if development else ProductionConfig app = Flask( - __name__, instance_path=str(Path("testing").absolute()) if development else None + __name__, + instance_path=( + str(Path("testing").absolute()) + if issubclass(config_class, DevelopmentConfig) + else None + ), ) app.config.from_object(config_class) @@ -129,6 +135,28 @@ def create_app(config_class: type[DefaultConfig] | None = None) -> Flask: "Configure RATELIMIT_STORAGE_URI with shared storage before running in production." ) + if not app.testing and not app.debug: + # HUEY_URL overrides the legacy HUEY.url option; absent either, Huey + # falls back to an in-process queue that cannot serve a separate worker. + huey_url = app.config.get( + "HUEY_URL", app.config.get("HUEY", {}).get("url", "memory://") + ) + if isinstance(huey_url, str) and urlsplit(huey_url).scheme.lower() in { + "memory", + "blackhole", + }: + raise RuntimeError( + "Configure HUEY_URL with a shared, persistent queue before " + "running in production (memory:// and blackhole:// are unsafe)." + ) + + # Local development and tests should start from a fresh checkout: their + # default log directory and development SQLite instance are generated. + if app.testing or app.debug: + Path(app.config["LOG_FILE"]).parent.mkdir(parents=True, exist_ok=True) + if app.debug: + Path(app.instance_path).mkdir(parents=True, exist_ok=True) + default_logging_formatter = logging.Formatter(app.config["LOG_FORMAT"]) log_file_handler = logging.handlers.RotatingFileHandler( |
