From 1dbefdf6b7939f9a937433c89e51959bfda5b764 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Fri, 2 Oct 2020 03:43:40 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9D=87=EF=B8=8F=20Raise=20an=20error=20if=20?= =?UTF-8?q?the=20Baron=20thread=20was=20already=20started?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- royalnet/baron/__init__.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/royalnet/baron/__init__.py b/royalnet/baron/__init__.py index 2ec75273..d841fdd4 100644 --- a/royalnet/baron/__init__.py +++ b/royalnet/baron/__init__.py @@ -5,6 +5,8 @@ import redis __all__ = ( "Baron", + "BaronError", + "BaronAlreadyStartedError", "BaronListenerThread", ) @@ -16,14 +18,25 @@ class Baron: redis_args: Mapping[str, Any],): self.publisher: redis.Redis = redis.Redis(**redis_args) self.listen_thread: BaronListenerThread = BaronListenerThread(publisher=self.publisher) - self.was_started = False + self.is_started = False def listener(self) -> redis.client.PubSub: return self.listen_thread.listener def start(self): + """Start the listen thread of the Baron module.""" + if self.is_started: + raise BaronAlreadyStartedError("This Baron module was already started somewhere else.") self.listen_thread.start() - self.was_started = True + self.is_started = True + + +class BaronError(Exception): + """An error of the Baron module.""" + + +class BaronAlreadyStartedError(Exception): + """This Baron module was already started somewhere else.""" class BaronListenerThread(threading.Thread):