From 94edf03cf895e783354f2005cd0b138e97060cf5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torma=20Krist=C3=B3f?= Date: Mon, 30 Mar 2020 17:10:48 +0200 Subject: [PATCH 1/2] introduce type hinting to consumerlocator --- app.py | 3 ++- consumerlocator.py | 21 +++++++-------------- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/app.py b/app.py index e818b33..81c318b 100644 --- a/app.py +++ b/app.py @@ -25,11 +25,12 @@ if __name__ == "__main__": LOGGER.info("Producer started") generateduuid = str(uuid) LOGGER.debug(f"My uuid is {generateduuid}") - consumerlocator = ConsumerLocator() + consumerlocator = ConsumerLocator(communicator=Communicator()) communicator = Communicator(consumerlocator=consumerlocator,uuid=uuid) messagesender = MessageSender(communicator=communicator) while True: + LOGGER.info("Sending message to consumer") messagesender.sendmessage() time.sleep(random.random()) diff --git a/consumerlocator.py b/consumerlocator.py index 535857a..82faeda 100644 --- a/consumerlocator.py +++ b/consumerlocator.py @@ -1,7 +1,7 @@ #!/usr/bin/env python import datetime -import communicator +from communicator import Communicator import os """ @@ -20,22 +20,15 @@ class ConsumerLocator: Manages the list of consumers. """ - def __init__(self): + def __init__(self, communicator: Communicator): """ Initialize class. """ - os.environ["KnownConsumer"] = "10.69.42.2" # TODO remove self.consumerlist = [{"Host": os.environ["KnownConsumer"], "State": True, "LastOk": datetime.datetime.now()}] self.currentconsumer = self.consumerlist[0] + self.communicator = communicator - def initcommunicator(self, comm: communicator.Communicator): - """ - Initialize the reference to the communicator - :param comm: is the communicator - """ - self.communicator = comm - - def learnconsumerlist(self): + def learnconsumerlist(self) -> None: """" Learns the list of consumers. """ @@ -53,7 +46,7 @@ class ConsumerLocator: self.updateconsumerlist() - def updateconsumerlist(self): + def updateconsumerlist(self) -> None: """ Updates the consumer list based on their availability. """ @@ -93,14 +86,14 @@ class ConsumerLocator: else: return None - def getcurrentconsumer(self): + def getcurrentconsumer(self) -> str: """ Returns the currently selected consumer. :return: the current consumer """ return self.currentconsumer["Host"] - def checkcurrentconsumer(self): + def checkcurrentconsumer(self) -> bool: """ Check the consumers health. :return: True if OK, False if fail -- 2.45.2 From 77f01db969441f37f05996dd2e3b317a54f95572 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torma=20Krist=C3=B3f?= Date: Mon, 30 Mar 2020 17:15:59 +0200 Subject: [PATCH 2/2] solve circular dependency --- app.py | 7 ++++--- communicator.py | 2 +- consumerlocator.py | 4 ++-- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/app.py b/app.py index 81c318b..b808775 100644 --- a/app.py +++ b/app.py @@ -25,12 +25,13 @@ if __name__ == "__main__": LOGGER.info("Producer started") generateduuid = str(uuid) LOGGER.debug(f"My uuid is {generateduuid}") - consumerlocator = ConsumerLocator(communicator=Communicator()) - communicator = Communicator(consumerlocator=consumerlocator,uuid=uuid) + consumerlocator = ConsumerLocator(uuid=generateduuid) + communicator = Communicator(consumerlocator=consumerlocator,uuid=generateduuid) messagesender = MessageSender(communicator=communicator) while True: - + LOGGER.info(f"Updating consumer list of {generateduuid}") + consumerlocator.updateconsumer() LOGGER.info("Sending message to consumer") messagesender.sendmessage() time.sleep(random.random()) diff --git a/communicator.py b/communicator.py index 675642b..69a7511 100644 --- a/communicator.py +++ b/communicator.py @@ -21,7 +21,7 @@ class Communicator: Class handling low level communication with consumers. """ - def __init__(self, consumerlocator: ConsumerLocator, uuid): + def __init__(self, consumerlocator: ConsumerLocator, uuid: str): """ Initialize object :param consumerlocator: diff --git a/consumerlocator.py b/consumerlocator.py index 82faeda..0a9c55f 100644 --- a/consumerlocator.py +++ b/consumerlocator.py @@ -20,13 +20,13 @@ class ConsumerLocator: Manages the list of consumers. """ - def __init__(self, communicator: Communicator): + def __init__(self, uuid: str): """ Initialize class. """ self.consumerlist = [{"Host": os.environ["KnownConsumer"], "State": True, "LastOk": datetime.datetime.now()}] self.currentconsumer = self.consumerlist[0] - self.communicator = communicator + self.communicator = Communicator(consumerlocator=self,uuid=uuid) def learnconsumerlist(self) -> None: """" -- 2.45.2