bec/bec_lib/util_scripts/channel_monitor.py
Ivan Usov 06f2d781ae refactor: rename module BECMessage -> messages
This should help to avoid confusion between BECMessage module and
BECMessage class located in the same module
2023-11-10 10:28:53 +01:00

39 lines
979 B
Python

import argparse
import json
import threading
from bec_lib import messages
from bec_lib.redis_connector import RedisConnector
from bec_lib.service_config import ServiceConfig
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument(
"--config",
default="",
help="path to the config file",
)
parser.add_argument(
"--channel",
default="",
help="channel name",
)
clargs = parser.parse_args()
config_path = clargs.config
topic = clargs.channel
config = ServiceConfig(config_path)
def channel_callback(msg, **kwargs):
msg = messages.MessageReader.loads(msg.value)
out = {"msg_type": msg.msg_type, "content": msg.content, "metadata": msg.metadata}
print(json.dumps(out, indent=4, default=lambda o: "<not serializable object>"))
connector = RedisConnector(config.redis)
consumer = connector.consumer(topics=topic, cb=channel_callback)
consumer.start()
event = threading.Event()
event.wait()