aboutsummaryrefslogtreecommitdiff
path: root/gn3
diff options
context:
space:
mode:
Diffstat (limited to 'gn3')
-rw-r--r--gn3/jobs.py13
-rw-r--r--gn3/json_encoders_decoders.py31
2 files changed, 39 insertions, 5 deletions
diff --git a/gn3/jobs.py b/gn3/jobs.py
index 8854a61..1af63f7 100644
--- a/gn3/jobs.py
+++ b/gn3/jobs.py
@@ -5,9 +5,10 @@ from uuid import UUID, uuid4
from datetime import datetime
from redis import Redis
-
from pymonad.either import Left, Right, Either
+from gn3 import json_encoders_decoders as jed
+
JOBS_NAMESPACE = "GN3::JOBS"
class InvalidCommand(Exception):
@@ -22,8 +23,8 @@ def job(redisconn: Redis, job_id: UUID) -> Either:
the_job = redisconn.hgetall(job_key(job_id))
if the_job:
return Right({
- **the_job,
- "search_results": json.loads(the_job["search_results"])
+ key: json.loads(value, object_hook=jed.custom_json_decoder)
+ for key, value in the_job.items()
})
return Left({
"error": "NotFound",
@@ -48,8 +49,10 @@ def create_job(redisconn: Redis, job_details: dict[str, Any]) -> UUID:
def __create__(_job_command):
job_id = job_details.get("job_id", uuid4())
redisconn.hset(job_key(job_id), mapping={
- **job_details, "job_id": job_id, "created": datetime.now(), "stdout": "",
- "stderr": "", "status": "queued"
+ key: json.dumps(value, cls=jed.CustomJSONEncoder) for key, value in {
+ **job_details, "job_id": job_id, "created": datetime.now(),
+ "status": "queued"
+ }.items()
})
return job_id
def __raise__(err):
diff --git a/gn3/json_encoders_decoders.py b/gn3/json_encoders_decoders.py
new file mode 100644
index 0000000..be15b34
--- /dev/null
+++ b/gn3/json_encoders_decoders.py
@@ -0,0 +1,31 @@
+"""Custom json encoders for various purposes."""
+import json
+from uuid import UUID
+from datetime import datetime
+
+__ENCODERS__ = {
+ UUID: lambda obj: {"__type": "UUID", "__value": str(obj)},
+ datetime: lambda obj: {"__type": "DATETIME", "__value": obj.isoformat()}
+}
+
+class CustomJSONEncoder(json.JSONEncoder):
+ """
+ A custom JSON encoder to handle cases where the default encoder fails.
+ """
+ def default(self, obj):# pylint: disable=[arguments-renamed]
+ """Return a serializable object for `obj`."""
+ if type(obj) in __ENCODERS__:
+ return __ENCODERS__[type(obj)](obj)
+ return json.JSONEncoder.default(self, obj)
+
+
+__DECODERS__ = {
+ "UUID": UUID,
+ "DATETIME": datetime.fromisoformat
+}
+
+def custom_json_decoder(obj_dict):
+ """Decode custom types"""
+ if "__type" in obj_dict:
+ return __DECODERS__[obj_dict["__type"]](obj_dict["__value"])
+ return obj_dict