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"])