1- import orjson
2- import importlib
1+ import cloudpickle
32from typing import Dict
43from pathlib import Path
54
65from tetra_rp import get_logger
7- from tetra_rp .core .utils .json import normalize_for_json
86from tetra_rp .core .utils .singleton import SingletonMixin
97
108from .base import BaseResource , DeployableResource
119
1210
1311log = get_logger ("resource_manager" )
1412
15-
16- RESOURCE_STATE_FILE = Path (".tetra_resources.json " )
13+ # File to persist state of resources
14+ RESOURCE_STATE_FILE = Path (".tetra_resources.pkl " )
1715
1816
1917class ResourceManager (SingletonMixin ):
@@ -25,34 +23,22 @@ def __init__(self):
2523 if not self ._resources :
2624 self ._load_resources ()
2725
28- """Load persisted resource information."""
2926 def _load_resources (self ) -> Dict [str , DeployableResource ]:
27+ """Load persisted resource information using cloudpickle."""
3028 if RESOURCE_STATE_FILE .exists ():
3129 try :
3230 with open (RESOURCE_STATE_FILE , "rb" ) as f :
33- resources_state = orjson .loads (f .read ())
34- for k , v in resources_state .items ():
35- class_name = k .split ("_" )[0 ]
36- module = importlib .import_module ("tetra_rp.core.resources" )
37- resource_class = getattr (module , class_name )
38- if resource_class :
39- # Produce the BaseResource object
40- self ._resources [k ] = resource_class (** v )
41-
31+ self ._resources = cloudpickle .load (f )
4232 log .debug (f"Loaded saved resources from { RESOURCE_STATE_FILE } " )
43-
44- except orjson . JSONDecodeError :
45- log . error ( f"Failed to load resources from { RESOURCE_STATE_FILE } " )
33+ except Exception as e :
34+ log . error ( f"Failed to load resources from { RESOURCE_STATE_FILE } : { e } " )
35+ return self . _resources
4636
4737 def _save_resources (self ) -> None :
48- """Persist state of resources to disk."""
49- resources_state = {
50- k : v .model_dump (exclude_none = True ) for k , v in self ._resources .items ()
51- }
52-
53- with open (RESOURCE_STATE_FILE , "w" ) as f :
54- f .write (orjson .dumps (normalize_for_json (resources_state )).decode ("utf-8" ))
55- log .debug (f"Saved resources in { RESOURCE_STATE_FILE } " )
38+ """Persist state of resources to disk using cloudpickle."""
39+ with open (RESOURCE_STATE_FILE , "wb" ) as f :
40+ cloudpickle .dump (self ._resources , f )
41+ log .debug (f"Saved resources in { RESOURCE_STATE_FILE } " )
5642
5743 def add_resource (self , uid : str , resource : DeployableResource ):
5844 """Add a resource to the manager."""
0 commit comments