From 9783b8c20b691cdaa0ca68411f0ac77ea6ccf57a Mon Sep 17 00:00:00 2001 From: schdwlk Date: Sun, 29 Mar 2020 18:27:19 +0200 Subject: [PATCH 1/2] Fixed a lot of typos in consumerlocator and added some mocks --- app.py | 6 +++--- consumerlocator.py | 51 +++++++++++++++++++++++++--------------------- 2 files changed, 31 insertions(+), 26 deletions(-) diff --git a/app.py b/app.py index 34e9f4e..2f30c1d 100644 --- a/app.py +++ b/app.py @@ -20,14 +20,14 @@ if __name__ == "__main__": print("Producer: init") comm = communicator.Communicator() conslist = consumerlocator.ConsumerLocator() - conslist.initCommunicator(comm) + conslist.initcommunicator(comm) message = messagesender.MessageSender() conslist.learnconsumerlist() print("Producer: started sending") for i in range(30): - print("Producer: send %i message", i) - msg = message.CreateMessage(20) + msg = message.createMessage(20) + print("Producer: send ", i, "th message: ", msg) if not conslist.checkcurrentconsumer(): conslist.updateconsumer() comm.sendmessage(msg) diff --git a/consumerlocator.py b/consumerlocator.py index 540b6af..6a77f83 100644 --- a/consumerlocator.py +++ b/consumerlocator.py @@ -2,9 +2,10 @@ import datetime import communicator +import os """ -Consumer locator modul, that manages the list of consumers. +Consumer locator module, that manages the list of consumers. """ __author__ = "@dscharnitzky" @@ -23,10 +24,16 @@ class ConsumerLocator: """ Initialize class. """ - self.consumerList = [{"Host": "KnownHost", "State": True, "LastOk": datetime.datetime.now()}] - self.currentConsumer = self.consumerList[0]["Host"] + os.environ["KnownConsumer"] = "MockRemoveMe" + self.consumerlist = [{"Host": os.environ["KnownConsumer"], "State": True, "LastOk": datetime.datetime.now()}] + self.currentconsumer = self.consumerlist[0] + print(self.currentconsumer) - def initCommunicator(self, comm: 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): @@ -38,44 +45,44 @@ class ConsumerLocator: return for consumer in recievedconsumerlist: self.consumerList.append({"Host": consumer, "State": True, "LastOk": datetime.datetime.now()}) - self.updateConsumerList() + self.updateconsumerlist() def updateconsumerlist(self): """ Updates the consumer list based on their availability. """ - removeList = [] - for consumer in self.consumerList: + removelist = [] + for consumer in self.consumerlist: if not self.communicator.checkconsumer(consumer["Host"]): consumer["State"] = False if datetime.datetime.now() - consumer["LastOk"] > datetime.timedelta(hours=1): - removeList.append(consumer) + removelist.append(consumer) else: consumer["LastOk"] = datetime.datetime.now() - for rem in removeList: - self.consumerList.remove(rem) + for rem in removelist: + self.consumerlist.remove(rem) def updateconsumer(self): """ Checks all the consumers in the list and updates the current consumer if necessary. :return: the current consumer or None if there are no available customers at the moment. """ - self.updateConsumerList() + self.updateconsumerlist() - if not self.checkConsumer(): + if not self.checkcurrentconsumer(): - newCurrentConsumer = None + newcurrentconsumer = None - for consumer in self.consumerList: + for consumer in self.consumerlist: if consumer["State"]: - newCurrentConsumer = consumer + newcurrentconsumer = consumer break - self.currentConsumer = newCurrentConsumer + self.currentconsumer = newcurrentconsumer self.learnconsumerlist() - if self.currentConsumer is not None: - return self.currentConsumer["Host"] + if self.currentconsumer is not None: + return self.currentconsumer["Host"] else: return None @@ -84,14 +91,12 @@ class ConsumerLocator: Returns the currently selected consumer. :return: the current consumer """ - return self.currentConsumer["Host"] + return self.currentconsumer["Host"] def checkcurrentconsumer(self): """ Check the consumers health. :return: True if OK, False if fail """ - if self.communicator.checkconsumer(self.currentConsumer["Host"]): - return True - else: - return False + # return self.communicator.checkconsumer(self.currentconsumer["Host"]) + return True # TODO remove this line and uncomment the above -- 2.45.2 From 2b44a5bbad358d482e64ae8e16c83e09ff056d8b Mon Sep 17 00:00:00 2001 From: schdwlk Date: Sun, 29 Mar 2020 19:18:25 +0200 Subject: [PATCH 2/2] Added environment variable and fixed stuff --- app.py | 18 +++++++++++++----- consumerlocator.py | 28 ++++++++++++++++++---------- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/app.py b/app.py index 2f30c1d..c93b059 100644 --- a/app.py +++ b/app.py @@ -3,7 +3,7 @@ import sentry_sdk import communicator import consumerlocator import messagesender - +import time """ Main Flask RESTful API @@ -27,9 +27,17 @@ if __name__ == "__main__": print("Producer: started sending") for i in range(30): msg = message.createMessage(20) - print("Producer: send ", i, "th message: ", msg) - if not conslist.checkcurrentconsumer(): - conslist.updateconsumer() - comm.sendmessage(msg) + print("Producer: trying to send ", i, "th message: ", msg) + available = False + if conslist.updateconsumer() is None: + print("Producer: no consumer available (waiting a bit)") + time.sleep(1) + else: + available = True + if available: + comm.sendmessage(msg) + print("Producer: message sent") + else: + print("Producer: failed to send message") print("Producer: finished") \ No newline at end of file diff --git a/consumerlocator.py b/consumerlocator.py index 6a77f83..535857a 100644 --- a/consumerlocator.py +++ b/consumerlocator.py @@ -24,10 +24,9 @@ class ConsumerLocator: """ Initialize class. """ - os.environ["KnownConsumer"] = "MockRemoveMe" + 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] - print(self.currentconsumer) def initcommunicator(self, comm: communicator.Communicator): """ @@ -43,8 +42,15 @@ class ConsumerLocator: recievedconsumerlist = self.communicator.discoveravailableconsumers() if recievedconsumerlist is None: return - for consumer in recievedconsumerlist: - self.consumerList.append({"Host": consumer, "State": True, "LastOk": datetime.datetime.now()}) + for recconsumer in recievedconsumerlist: + contains = False + for consumer in self.consumerlist: + if consumer["Host"] == recconsumer: + contains = True + + if not contains: + self.consumerlist.append({"Host": recconsumer, "State": True, "LastOk": datetime.datetime.now()}) + self.updateconsumerlist() def updateconsumerlist(self): @@ -59,18 +65,18 @@ class ConsumerLocator: removelist.append(consumer) else: consumer["LastOk"] = datetime.datetime.now() + consumer["State"] = True for rem in removelist: self.consumerlist.remove(rem) def updateconsumer(self): """ - Checks all the consumers in the list and updates the current consumer if necessary. + If the current consumer is not available, checks all the consumers in the list and updates the current one. :return: the current consumer or None if there are no available customers at the moment. """ - self.updateconsumerlist() if not self.checkcurrentconsumer(): - + self.updateconsumerlist() newcurrentconsumer = None for consumer in self.consumerlist: @@ -79,7 +85,8 @@ class ConsumerLocator: break self.currentconsumer = newcurrentconsumer - self.learnconsumerlist() + if self.currentconsumer is not None: + self.learnconsumerlist() if self.currentconsumer is not None: return self.currentconsumer["Host"] @@ -98,5 +105,6 @@ class ConsumerLocator: Check the consumers health. :return: True if OK, False if fail """ - # return self.communicator.checkconsumer(self.currentconsumer["Host"]) - return True # TODO remove this line and uncomment the above + if self.currentconsumer is None: + return False + return self.communicator.checkconsumer(self.currentconsumer["Host"]) -- 2.45.2