From 7035e9f9467684cccae2b7ff3faaa62e6a9f91aa Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Sat, 31 Oct 2026 20:43:33 +0100 Subject: 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. --- webmentions_ssg/__init__.py | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) (limited to 'webmentions_ssg/__init__.py') 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( -- cgit v1.3.1