44
55import json
66import logging
7- from datetime import UTC , datetime
87from pathlib import Path
98
109from aws_durable_execution_sdk_python_testing .exceptions import (
1615)
1716
1817
19- class DateTimeEncoder (json .JSONEncoder ):
20- """Custom JSON encoder that handles datetime objects."""
21-
22- def default (self , obj ):
23- if isinstance (obj , datetime ):
24- return obj .timestamp ()
25- return super ().default (obj )
26-
27-
28- def datetime_object_hook (obj ):
29- """JSON object hook to convert unix timestamps back to datetime objects."""
30- if isinstance (obj , dict ):
31- for key , value in obj .items ():
32- if isinstance (value , int | float ) and key .endswith (
33- ("_timestamp" , "_time" , "Timestamp" , "Time" )
34- ):
35- try : # noqa: SIM105
36- obj [key ] = datetime .fromtimestamp (value , tz = UTC )
37- except (ValueError , OSError ):
38- # Leave as number if not a valid timestamp
39- pass
40- return obj
41-
42-
4318class FileSystemExecutionStore (BaseExecutionStore ):
4419 """File system-based execution store for persistence."""
4520
@@ -69,10 +44,10 @@ def _get_file_path(self, execution_arn: str) -> Path:
6944 def save (self , execution : Execution ) -> None :
7045 """Save execution to file system."""
7146 file_path = self ._get_file_path (execution .durable_execution_arn )
72- data = execution .to_dict ()
47+ data = execution .to_json_dict ()
7348
7449 with open (file_path , "w" , encoding = "utf-8" ) as f :
75- json .dump (data , f , indent = 2 , cls = DateTimeEncoder )
50+ json .dump (data , f , indent = 2 )
7651
7752 def load (self , execution_arn : str ) -> Execution :
7853 """Load execution from file system."""
@@ -82,9 +57,9 @@ def load(self, execution_arn: str) -> Execution:
8257 raise ResourceNotFoundException (msg )
8358
8459 with open (file_path , encoding = "utf-8" ) as f :
85- data = json .load (f , object_hook = datetime_object_hook )
60+ data = json .load (f )
8661
87- return Execution .from_dict (data )
62+ return Execution .from_json_dict (data )
8863
8964 def update (self , execution : Execution ) -> None :
9065 """Update execution in file system (same as save)."""
@@ -96,8 +71,8 @@ def list_all(self) -> list[Execution]:
9671 for file_path in self ._storage_dir .glob ("*.json" ):
9772 try :
9873 with open (file_path , encoding = "utf-8" ) as f :
99- data = json .load (f , object_hook = datetime_object_hook )
100- executions .append (Execution .from_dict (data ))
74+ data = json .load (f )
75+ executions .append (Execution .from_json_dict (data ))
10176 except (json .JSONDecodeError , KeyError , OSError ) as e :
10277 logging .warning ("Skipping corrupted file %s: %s" , file_path , e )
10378 continue
0 commit comments