generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 1
feat: implement filesystem store #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
src/aws_durable_execution_sdk_python_testing/stores/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
|
|
27 changes: 27 additions & 0 deletions
27
src/aws_durable_execution_sdk_python_testing/stores/base.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| """Base classes and protocols for execution stores.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from enum import Enum | ||
| from typing import TYPE_CHECKING, Protocol | ||
|
|
||
|
|
||
| if TYPE_CHECKING: | ||
| from aws_durable_execution_sdk_python_testing.execution import Execution | ||
|
|
||
|
|
||
| class StoreType(Enum): | ||
| """Supported execution store types.""" | ||
|
|
||
| MEMORY = "memory" | ||
| FILESYSTEM = "filesystem" | ||
|
|
||
|
|
||
| class ExecutionStore(Protocol): | ||
| """Protocol for execution storage implementations.""" | ||
|
|
||
| # ignore cover because coverage doesn't understand elipses | ||
| def save(self, execution: Execution) -> None: ... # pragma: no cover | ||
| def load(self, execution_arn: str) -> Execution: ... # pragma: no cover | ||
| def update(self, execution: Execution) -> None: ... # pragma: no cover | ||
| def list_all(self) -> list[Execution]: ... # pragma: no cover |
99 changes: 99 additions & 0 deletions
99
src/aws_durable_execution_sdk_python_testing/stores/filesystem.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| """File system-based execution store implementation.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| import logging | ||
| from datetime import UTC, datetime | ||
| from pathlib import Path | ||
|
|
||
| from aws_durable_execution_sdk_python_testing.exceptions import ( | ||
| DurableFunctionsLocalRunnerError, | ||
| ) | ||
| from aws_durable_execution_sdk_python_testing.execution import Execution | ||
|
|
||
|
|
||
| class DateTimeEncoder(json.JSONEncoder): | ||
| """Custom JSON encoder that handles datetime objects.""" | ||
|
|
||
| def default(self, obj): | ||
| if isinstance(obj, datetime): | ||
| return obj.timestamp() | ||
| return super().default(obj) | ||
|
|
||
|
|
||
| def datetime_object_hook(obj): | ||
| """JSON object hook to convert unix timestamps back to datetime objects.""" | ||
| if isinstance(obj, dict): | ||
| for key, value in obj.items(): | ||
| if isinstance(value, int | float) and key.endswith(("_timestamp", "_time")): | ||
bchampp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| try: # noqa: SIM105 | ||
| obj[key] = datetime.fromtimestamp(value, tz=UTC) | ||
| except (ValueError, OSError): | ||
| # Leave as number if not a valid timestamp | ||
| pass | ||
| return obj | ||
|
|
||
|
|
||
| class FileSystemExecutionStore: | ||
| """File system-based execution store for persistence.""" | ||
|
|
||
| def __init__(self, storage_dir: Path) -> None: | ||
| self._storage_dir = storage_dir | ||
|
|
||
| @classmethod | ||
| def create(cls, storage_dir: str | Path | None = None) -> FileSystemExecutionStore: | ||
| """Create a FileSystemExecutionStore with directory creation. | ||
|
|
||
| Args: | ||
| storage_dir: Directory path for storage. Defaults to '.durable_executions' | ||
|
|
||
| Returns: | ||
| FileSystemExecutionStore instance with created directory | ||
| """ | ||
| path = Path(storage_dir) if storage_dir else Path(".durable_executions") | ||
| path.mkdir(exist_ok=True) | ||
| return cls(storage_dir=path) | ||
|
|
||
| def _get_file_path(self, execution_arn: str) -> Path: | ||
| """Get file path for execution ARN.""" | ||
| # Use ARN as filename with .json extension, replacing unsafe characters | ||
| safe_filename = execution_arn.replace(":", "_").replace("/", "_") | ||
| return self._storage_dir / f"{safe_filename}.json" | ||
|
|
||
| def save(self, execution: Execution) -> None: | ||
| """Save execution to file system.""" | ||
| file_path = self._get_file_path(execution.durable_execution_arn) | ||
| data = execution.to_dict() | ||
|
|
||
| with open(file_path, "w", encoding="utf-8") as f: | ||
| json.dump(data, f, indent=2, cls=DateTimeEncoder) | ||
|
|
||
| def load(self, execution_arn: str) -> Execution: | ||
| """Load execution from file system.""" | ||
| file_path = self._get_file_path(execution_arn) | ||
| if not file_path.exists(): | ||
| msg = f"Execution {execution_arn} not found" | ||
| raise DurableFunctionsLocalRunnerError(msg) | ||
|
|
||
| with open(file_path, encoding="utf-8") as f: | ||
| data = json.load(f, object_hook=datetime_object_hook) | ||
|
|
||
| return Execution.from_dict(data) | ||
|
|
||
| def update(self, execution: Execution) -> None: | ||
| """Update execution in file system (same as save).""" | ||
| self.save(execution) | ||
|
|
||
| def list_all(self) -> list[Execution]: | ||
| """List all executions from file system.""" | ||
| executions = [] | ||
| for file_path in self._storage_dir.glob("*.json"): | ||
| try: | ||
| with open(file_path, encoding="utf-8") as f: | ||
| data = json.load(f, object_hook=datetime_object_hook) | ||
| executions.append(Execution.from_dict(data)) | ||
| except (json.JSONDecodeError, KeyError, OSError) as e: | ||
| logging.warning("Skipping corrupted file %s: %s", file_path, e) | ||
| continue | ||
| return executions | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.