aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2024-01-09 04:42:20 +0300
committerFrederick Muriuki Muriithi2024-01-09 05:00:04 +0300
commit93c77842315d304abbfc27d78d98b7d42da32a61 (patch)
tree08a7a4e1f95fcdb93bd7bffd4a2d38356b686198
parent0e8f06477acda15b6ff924bd5c599d58d541bb4c (diff)
downloadgn-uploader-93c77842315d304abbfc27d78d98b7d42da32a61.tar.gz
scripts: Log to redis
Add a logger that will log output to a redis list. This will be useful to retrieve the output(s) of the running script to help display some form of progress to the user.
-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)