Skip to content

Commit b2e0b3c

Browse files
committed
refactor: resource manager uses cloudpickle replacing orjson
This simplifies resource state caching and reuses the efficient cloudpickle library.
1 parent 44fe60e commit b2e0b3c

4 files changed

Lines changed: 372 additions & 361 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,5 +173,5 @@ cython_debug/
173173

174174
# PyPI configuration file
175175
.pypirc
176-
.tetra_resources.json
176+
.tetra_resources.pkl
177177
.DS_Store

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ dependencies = [
2121
"cloudpickle>=3.1.1",
2222
"runpod>=1.7.9",
2323
"python-dotenv>=1.0.0",
24-
"orjson>=3.10.16"
2524
]
2625

2726
[dependency-groups]

src/tetra_rp/core/resources/resource_manager.py

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
1-
import orjson
2-
import importlib
1+
import cloudpickle
32
from typing import Dict
43
from pathlib import Path
54

65
from tetra_rp import get_logger
7-
from tetra_rp.core.utils.json import normalize_for_json
86
from tetra_rp.core.utils.singleton import SingletonMixin
97

108
from .base import BaseResource, DeployableResource
119

1210

1311
log = 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

1917
class 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

Comments
 (0)