From 93c77842315d304abbfc27d78d98b7d42da32a61 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Tue, 9 Jan 2024 04:42:20 +0300 Subject: 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. --- scripts/redis_logger.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 scripts/redis_logger.py 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) -- cgit v1.2.3