Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/cashet/_client_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,16 @@ def resolve_task_config(
timeout_seconds = (
_timeout if _timeout is not None else getattr(raw_func, "_cashet_timeout", None)
)
timeout = timedelta(seconds=timeout_seconds) if timeout_seconds is not None else None
ttl_seconds = _ttl if _ttl is not None else getattr(raw_func, "_cashet_ttl", None)
ttl = timedelta(seconds=ttl_seconds) if ttl_seconds is not None else None
timeout = duration_from_seconds(timeout_seconds)
ttl = duration_from_seconds(ttl_seconds)
return raw_func, cache, tags, retries, force, timeout, ttl


def duration_from_seconds(seconds: int | float | None) -> timedelta | None:
return timedelta(seconds=seconds) if seconds is not None else None


def set_task_metadata(
func: Callable[..., Any],
task_name: str,
Expand Down
11 changes: 4 additions & 7 deletions src/cashet/_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
import json
import logging
import tarfile
from datetime import datetime, timedelta
from datetime import datetime
from io import BytesIO
from pathlib import Path
from typing import Any

from cashet._client_base import duration_from_seconds
from cashet.models import Commit, ObjectRef, StorageTier, TaskDef, TaskStatus
from cashet.protocols import AsyncStore

Expand Down Expand Up @@ -59,12 +60,8 @@ def _task_def_to_dict(task_def: TaskDef) -> dict[str, Any]:


def _dict_to_task_def(data: dict[str, Any]) -> TaskDef:
timeout: timedelta | None = None
if "timeout" in data and data["timeout"] is not None:
timeout = timedelta(seconds=data["timeout"])
ttl: timedelta | None = None
if "ttl" in data and data["ttl"] is not None:
ttl = timedelta(seconds=data["ttl"])
timeout = duration_from_seconds(data.get("timeout"))
ttl = duration_from_seconds(data.get("ttl"))
return TaskDef(
func_hash=data["func_hash"],
func_name=data["func_name"],
Expand Down
18 changes: 18 additions & 0 deletions tests/test_export.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import tarfile
from datetime import timedelta
from io import BytesIO
from pathlib import Path

Expand Down Expand Up @@ -76,6 +77,23 @@ def test_import_skips_existing(self, client: Client, tmp_path: Path) -> None:
count = client.import_archive(archive)
assert count == 0

def test_export_import_preserves_task_timing_options(
self, client: Client, tmp_path: Path
) -> None:
ref = client.submit(add, 5, 6, _timeout=1.5, _ttl=900)
assert ref.load() == 11

archive = tmp_path / "export.tar.gz"
client.export(archive)

client2 = Client(store_dir=tmp_path / ".cashet2")
assert client2.import_archive(archive) == 1

imported = client2.show(ref.commit_hash)
assert imported is not None
assert imported.task_def.timeout == timedelta(seconds=1.5)
assert imported.task_def.ttl == timedelta(seconds=900)

def test_export_empty_store(self, client: Client, tmp_path: Path) -> None:
archive = tmp_path / "empty.tar.gz"
client.export(archive)
Expand Down
Loading