From 0cc9ac0e94f9f36f2e18351389987077adf0fa23 Mon Sep 17 00:00:00 2001 From: David Lougheed Date: Thu, 12 Jan 2023 11:47:05 -0500 Subject: [PATCH] feat: add logger to event bus, with optional ability to pass another logger in --- bento_lib/events/_event_bus.py | 18 +++++++++++++++--- bento_lib/package.cfg | 2 +- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/bento_lib/events/_event_bus.py b/bento_lib/events/_event_bus.py index 9c24851..9af159f 100644 --- a/bento_lib/events/_event_bus.py +++ b/bento_lib/events/_event_bus.py @@ -1,5 +1,6 @@ import json import jsonschema +import logging import redis from typing import Callable, Dict, Optional, Union @@ -38,14 +39,19 @@ def __init__(self, allow_fake: bool = False, **kwargs): self._rc: Optional[redis.Redis] = None + logger = kwargs.pop("logger", None) or logging.getLogger(__name__) + self._logger: logging.Logger = logger + + connection_data: dict = kwargs or default_connection_data + try: - self._rc = self._get_redis(**(kwargs or default_connection_data)) + self._rc = self._get_redis(**connection_data) self._rc.get("") # Dummy request to check connection except redis.exceptions.ConnectionError as e: self._rc = None if not allow_fake: raise e - # TODO: Log otherwise (as info) + logger.warning(f"Starting event bus in 'fake' mode (tried connection data: {connection_data})") self._ps: Optional[redis.PubSub] = None @@ -91,6 +97,8 @@ def start_event_loop(self) -> None: if self._event_thread is not None: return + self._logger.debug("Starting EventBus event loop") + self._ps = self._rc.pubsub() self._ps.psubscribe(**self._ps_handlers) self._event_thread = self._ps.run_in_thread(sleep_time=0.001, daemon=True) @@ -103,6 +111,8 @@ def stop_event_loop(self) -> None: if self._event_thread is None: return + self._logger.debug("Stopping EventBus event loop") + self._event_thread.stop() self._event_thread = None self._ps = None @@ -179,7 +189,9 @@ def _publish_event( if self._rc is None: return False - self._rc.publish(channel, self._make_event(event_type, event_data, attrs)) + event: str = self._make_event(event_type, event_data, attrs) + self._logger.debug(f"Publishing event: {event}") + self._rc.publish(channel, event) return True def publish_service_event(self, service_artifact: str, event_type: str, event_data: Serializable) -> bool: diff --git a/bento_lib/package.cfg b/bento_lib/package.cfg index 67f4362..75fb19a 100644 --- a/bento_lib/package.cfg +++ b/bento_lib/package.cfg @@ -1,5 +1,5 @@ [package] name = bento_lib -version = 5.1.1 +version = 5.2.0 authors = David Lougheed, Paul Pillot author_emails = david.lougheed@mail.mcgill.ca, paul.pillot@computationalgenomics.ca