aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--scripts/redis_logger.py34
1 files changed, 34 insertions, 0 deletions
diff --git a/scripts/redis_logger.py b/scripts/redis_logger.py
new file mode 100644
index 0000000..c3a71db
--- /dev/null
+++ b/scripts/redis_logger.py
@@ -0,0 +1,34 @@
+"""Utilities to log to redis for our worker scripts."""
+import uuid
+import json
+import logging
+from typing import Any
+
+from redis import Redis
+
+class RedisLogger(logging.Handler):
+ """Log out to redis for our worker scripts"""
+ def __init__(self,
+ rconn: Redis,
+ jobid: uuid.UUID,
+ level:int = logging.NOTSET,
+ messagelistname: str = "log-messages",
+ expiry: int = 86400):
+ """Initialise the handler."""
+ super().__init__(level)
+ self.redisconnection = rconn
+ self.jobid = jobid
+ self.messagelistname = messagelistname
+ self.expiry = expiry
+ rconn.hset(name=str(jobid),
+ key="log-messagelist",
+ value=self.messages_list_name())
+
+ def messages_list_name(self):
+ return f"{str(self.jobid)}:{self.messagelistname}"
+
+ def emit(self, record):
+ """Log to the messages list for a given worker."""
+ self.redisconnection.rpush(
+ self.messages_list_name(), self.format(record))
+ self.redisconnection.expire(self.messages_list_name(), self.expiry)