diff --git a/app.py b/app.py index 77cb1b4..e818b33 100644 --- a/app.py +++ b/app.py @@ -1,14 +1,13 @@ #!/usr/bin/env python import random -import time import uuid import logging import sentry_sdk +import time from communicator import Communicator from consumerlocator import ConsumerLocator from messagesender import MessageSender - """ Main entrypoint """ @@ -19,7 +18,6 @@ __module_name__ = "app" __version__text__ = "1" sentry_sdk.init("https://3fa5ae886ba1489092ad49a93cb419c1@sentry.kmlabz.com/9") - logging.basicConfig(level=logging.INFO) LOGGER = logging.getLogger(__name__) diff --git a/consumerlocator.py b/consumerlocator.py index 540b6af..535857a 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,15 @@ class ConsumerLocator: """ Initialize class. """ - self.consumerList = [{"Host": "KnownHost", "State": True, "LastOk": datetime.datetime.now()}] - self.currentConsumer = self.consumerList[0]["Host"] + 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] - 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): @@ -36,46 +42,54 @@ 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()}) - self.updateConsumerList() + 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): """ 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) + 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.checkConsumer(): + if not self.checkcurrentconsumer(): + self.updateconsumerlist() + 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.learnconsumerlist() + self.currentconsumer = newcurrentconsumer + if self.currentconsumer is not None: + 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 +98,13 @@ 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: + if self.currentconsumer is None: return False + return self.communicator.checkconsumer(self.currentconsumer["Host"])