From a2ed2ebe00c623eb183b03f8182ffd672fbf9e1e Mon Sep 17 00:00:00 2001 From: wakonig_k Date: Mon, 26 Feb 2024 20:58:46 +0100 Subject: [PATCH] fix(bec_dispatcher): handle redis connection errors more gracefully --- bec_widgets/utils/bec_dispatcher.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/bec_widgets/utils/bec_dispatcher.py b/bec_widgets/utils/bec_dispatcher.py index 3076765b..a8f0a2ba 100644 --- a/bec_widgets/utils/bec_dispatcher.py +++ b/bec_widgets/utils/bec_dispatcher.py @@ -6,9 +6,10 @@ import os from collections.abc import Callable from typing import Union +import redis from bec_lib import BECClient, ServiceConfig -from bec_lib.connector import ConnectorBase -from qtpy.QtCore import QObject, Signal as pyqtSignal +from qtpy.QtCore import QObject +from qtpy.QtCore import Signal as pyqtSignal # Adding a new pyqt signal requires a class factory, as they must be part of the class definition # and cannot be dynamically added as class attributes after the class has been defined. @@ -41,7 +42,10 @@ class _BECDispatcher(QObject): if bec_config is None and os.path.isfile("bec_config.yaml"): bec_config = "bec_config.yaml" - self.client.initialize(config=ServiceConfig(config_path=bec_config)) + try: + self.client.initialize(config=ServiceConfig(config_path=bec_config)) + except redis.exceptions.ConnectionError as e: + print(f"Failed to initialize BECClient: {e}") self._connections = {} def connect_slot( @@ -78,7 +82,10 @@ class _BECDispatcher(QObject): if set(topics).intersection(connection_key): connection.signal.emit(msg.content, msg.metadata) - self.client.connector.register(topics=topics, cb=cb) + try: + self.client.connector.register(topics=topics, cb=cb) + except redis.exceptions.ConnectionError: + print("Could not connect to Redis, skipping registration of topics.") return _Connection(cb)