diff options
Diffstat (limited to 'webmentions_ssg/tasks/extension.py')
| -rw-r--r-- | webmentions_ssg/tasks/extension.py | 144 |
1 files changed, 72 insertions, 72 deletions
diff --git a/webmentions_ssg/tasks/extension.py b/webmentions_ssg/tasks/extension.py index 3f172f4..a878e16 100644 --- a/webmentions_ssg/tasks/extension.py +++ b/webmentions_ssg/tasks/extension.py @@ -16,78 +16,6 @@ from huey.api import TaskWrapper from .json_serializer import TaggedJSONSerializer -def _redis_class_and_options( - parsed: SplitResult, -) -> tuple[type[BaseHuey], dict[str, Any]]: - """Select a Redis Huey flavour while retaining the underlying URL scheme. - - :param parsed: Parsed Redis backend URL, including any flavour suffix. - :returns: Huey class and constructor options for the requested Redis flavour. - :raises RuntimeError: If the Redis backend flavour is unsupported. - """ - from huey import ( - PriorityRedisExpireHuey, - PriorityRedisHuey, - RedisExpireHuey, - RedisHuey, - ) - - scheme = parsed.scheme.lower() - base_scheme, separator, flavour = scheme.partition("+") - backends = { - "": RedisHuey, - "priority": PriorityRedisHuey, - "expire": RedisExpireHuey, - "priority+expire": PriorityRedisExpireHuey, - } - if flavour not in backends or (separator and not flavour): - raise RuntimeError(f"Unsupported HUEY_URL scheme: {scheme!r}") - - fixed_url = urlunsplit(parsed._replace(scheme=base_scheme)) - return backends[flavour], {"url": fixed_url} - - -def _file_class_and_options( - url: str, scheme: str -) -> tuple[type[BaseHuey], dict[str, Any]]: - """Extract a relative or absolute SQLite/file storage path. - - :param url: Backend URL containing the storage filename or path. - :param scheme: Selected file or SQLite backend scheme. - :returns: Huey class and constructor options for the file-backed queue. - :raises RuntimeError: If the storage URL is malformed or omits the path. - """ - from huey import FileHuey, SqliteHuey - - if scheme == "sqlite": - backend, option, description, example = ( - SqliteHuey, - "filename", - "SQLite Huey", - "sqlite:///huey.db (relative) or sqlite:////var/huey.db (absolute)", - ) - missing = "SQLite Huey URL must include a database path." - else: - backend, option, description, example = ( - FileHuey, - "path", - "File Huey", - "file:///huey-queue (relative) or file:////var/huey-queue (absolute)", - ) - missing = "File Huey URL must include a directory path." - - prefix = f"{scheme}:///" - if not url.startswith(prefix): - raise RuntimeError(f"{description} URLs must look like {example}") - - # Four slashes retain the initial '/' of an absolute path; three do not. - path = url.removeprefix(prefix) - if not path: - raise RuntimeError(missing) - - return backend, {option: path} - - class _DeferredTask: """Stable public task handle which follows the extension's current backend. @@ -328,3 +256,75 @@ class FlaskHuey: return BlackHoleHuey, {} raise RuntimeError(f"Unsupported HUEY_URL scheme: {scheme!r}") + + +def _redis_class_and_options( + parsed: SplitResult, +) -> tuple[type[BaseHuey], dict[str, Any]]: + """Select a Redis Huey flavour while retaining the underlying URL scheme. + + :param parsed: Parsed Redis backend URL, including any flavour suffix. + :returns: Huey class and constructor options for the requested Redis flavour. + :raises RuntimeError: If the Redis backend flavour is unsupported. + """ + from huey import ( + PriorityRedisExpireHuey, + PriorityRedisHuey, + RedisExpireHuey, + RedisHuey, + ) + + scheme = parsed.scheme.lower() + base_scheme, separator, flavour = scheme.partition("+") + backends = { + "": RedisHuey, + "priority": PriorityRedisHuey, + "expire": RedisExpireHuey, + "priority+expire": PriorityRedisExpireHuey, + } + if flavour not in backends or (separator and not flavour): + raise RuntimeError(f"Unsupported HUEY_URL scheme: {scheme!r}") + + fixed_url = urlunsplit(parsed._replace(scheme=base_scheme)) + return backends[flavour], {"url": fixed_url} + + +def _file_class_and_options( + url: str, scheme: str +) -> tuple[type[BaseHuey], dict[str, Any]]: + """Extract a relative or absolute SQLite/file storage path. + + :param url: Backend URL containing the storage filename or path. + :param scheme: Selected file or SQLite backend scheme. + :returns: Huey class and constructor options for the file-backed queue. + :raises RuntimeError: If the storage URL is malformed or omits the path. + """ + from huey import FileHuey, SqliteHuey + + if scheme == "sqlite": + backend, option, description, example = ( + SqliteHuey, + "filename", + "SQLite Huey", + "sqlite:///huey.db (relative) or sqlite:////var/huey.db (absolute)", + ) + missing = "SQLite Huey URL must include a database path." + else: + backend, option, description, example = ( + FileHuey, + "path", + "File Huey", + "file:///huey-queue (relative) or file:////var/huey-queue (absolute)", + ) + missing = "File Huey URL must include a directory path." + + prefix = f"{scheme}:///" + if not url.startswith(prefix): + raise RuntimeError(f"{description} URLs must look like {example}") + + # Four slashes retain the initial '/' of an absolute path; three do not. + path = url.removeprefix(prefix) + if not path: + raise RuntimeError(missing) + + return backend, {option: path} |
