From 75fc21607fd4fc8cb0c8c56470aeeaa7fdd0bc65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Du=C5=A1an=20Jolovi=C4=87?= Date: Sat, 9 May 2026 16:45:14 +0200 Subject: [PATCH] Share server submit option parsing --- src/cashet/_client_base.py | 14 +++++++++++++- src/cashet/server.py | 15 +++------------ tests/test_server.py | 9 +++++++++ 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/cashet/_client_base.py b/src/cashet/_client_base.py index e63b5f2..a70ab17 100644 --- a/src/cashet/_client_base.py +++ b/src/cashet/_client_base.py @@ -1,7 +1,7 @@ from __future__ import annotations import os -from collections.abc import Callable +from collections.abc import Callable, Mapping from datetime import timedelta from pathlib import Path from typing import Any @@ -55,6 +55,18 @@ def resolve_task_config( return raw_func, cache, tags, retries, force, timeout, ttl +def resolve_submit_options( + data: Mapping[str, Any], +) -> tuple[bool, dict[str, str], int, bool, Any, Any]: + cache = data.get("cache", True) + tags = data.get("tags", {}) + retries = data.get("retries", 0) + force = data.get("force", False) + timeout = data.get("timeout") + ttl = data.get("ttl") + return cache, tags, retries, force, timeout, ttl + + def set_task_metadata( func: Callable[..., Any], task_name: str, diff --git a/src/cashet/server.py b/src/cashet/server.py index 1f35634..5467db4 100644 --- a/src/cashet/server.py +++ b/src/cashet/server.py @@ -17,6 +17,7 @@ from starlette.responses import JSONResponse from starlette.routing import Route +from cashet._client_base import resolve_submit_options from cashet.async_client import AsyncClient from cashet.client import Client @@ -198,12 +199,7 @@ async def _async_submit(request: Request) -> JSONResponse: if error is not None: return error - cache = data.get("cache", True) - tags = data.get("tags", {}) - retries = data.get("retries", 0) - force = data.get("force", False) - timeout = data.get("timeout") - ttl = data.get("ttl") + cache, tags, retries, force, timeout, ttl = resolve_submit_options(data) start = time.perf_counter() try: @@ -404,12 +400,7 @@ async def _submit(request: Request) -> JSONResponse: if error is not None: return error - cache = data.get("cache", True) - tags = data.get("tags", {}) - retries = data.get("retries", 0) - force = data.get("force", False) - timeout = data.get("timeout") - ttl = data.get("ttl") + cache, tags, retries, force, timeout, ttl = resolve_submit_options(data) start = time.perf_counter() diff --git a/tests/test_server.py b/tests/test_server.py index e527bff..e07d303 100644 --- a/tests/test_server.py +++ b/tests/test_server.py @@ -148,6 +148,15 @@ def test_submit_internal_error_is_generic(self, tmp_path: Path) -> None: assert response.json()["error"] == "Internal server error" assert "secret internal path" not in response.text + def test_submit_cache_false_creates_new_commit(self, server_client: TestClient) -> None: + payload = {"task": "add", "args": [3, 4], "cache": False} + first = server_client.post("/submit", json=payload) + second = server_client.post("/submit", json=payload) + + assert first.status_code == 200 + assert second.status_code == 200 + assert first.json()["commit_hash"] != second.json()["commit_hash"] + class TestAsyncServerSubmit: async def test_async_submit_registered_task_with_sqlite(self, tmp_path: Path) -> None: