Communicator API impementation #1

Closed
tormakris wants to merge 4 commits from master into dev-communicator
2 changed files with 41 additions and 30 deletions

4
app.py
View File

@ -1,14 +1,13 @@
#!/usr/bin/env python #!/usr/bin/env python
import random import random
import time
import uuid import uuid
import logging import logging
import sentry_sdk import sentry_sdk
import time
from communicator import Communicator from communicator import Communicator
from consumerlocator import ConsumerLocator from consumerlocator import ConsumerLocator
from messagesender import MessageSender from messagesender import MessageSender
""" """
Main entrypoint Main entrypoint
""" """
@ -19,7 +18,6 @@ __module_name__ = "app"
__version__text__ = "1" __version__text__ = "1"
sentry_sdk.init("https://3fa5ae886ba1489092ad49a93cb419c1@sentry.kmlabz.com/9") sentry_sdk.init("https://3fa5ae886ba1489092ad49a93cb419c1@sentry.kmlabz.com/9")
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
LOGGER = logging.getLogger(__name__) LOGGER = logging.getLogger(__name__)

View File

@ -2,9 +2,10 @@
import datetime import datetime
import communicator import communicator
import os
""" """
Consumer locator modul, that manages the list of consumers. Consumer locator module, that manages the list of consumers.
""" """
__author__ = "@dscharnitzky" __author__ = "@dscharnitzky"
@ -23,10 +24,15 @@ class ConsumerLocator:
""" """
Initialize class. Initialize class.
""" """
self.consumerList = [{"Host": "KnownHost", "State": True, "LastOk": datetime.datetime.now()}] os.environ["KnownConsumer"] = "10.69.42.2" # TODO remove
self.currentConsumer = self.consumerList[0]["Host"] 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 self.communicator = comm
def learnconsumerlist(self): def learnconsumerlist(self):
@ -36,46 +42,54 @@ class ConsumerLocator:
recievedconsumerlist = self.communicator.discoveravailableconsumers() recievedconsumerlist = self.communicator.discoveravailableconsumers()
if recievedconsumerlist is None: if recievedconsumerlist is None:
return return
for consumer in recievedconsumerlist: for recconsumer in recievedconsumerlist:
self.consumerList.append({"Host": consumer, "State": True, "LastOk": datetime.datetime.now()}) contains = False
self.updateConsumerList() 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): def updateconsumerlist(self):
""" """
Updates the consumer list based on their availability. Updates the consumer list based on their availability.
""" """
removeList = [] removelist = []
for consumer in self.consumerList: for consumer in self.consumerlist:
if not self.communicator.checkconsumer(consumer["Host"]): if not self.communicator.checkconsumer(consumer["Host"]):
consumer["State"] = False consumer["State"] = False
if datetime.datetime.now() - consumer["LastOk"] > datetime.timedelta(hours=1): if datetime.datetime.now() - consumer["LastOk"] > datetime.timedelta(hours=1):
removeList.append(consumer) removelist.append(consumer)
else: else:
consumer["LastOk"] = datetime.datetime.now() consumer["LastOk"] = datetime.datetime.now()
for rem in removeList: consumer["State"] = True
self.consumerList.remove(rem) for rem in removelist:
self.consumerlist.remove(rem)
def updateconsumer(self): 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. :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"]: if consumer["State"]:
newCurrentConsumer = consumer newcurrentconsumer = consumer
break break
self.currentConsumer = newCurrentConsumer self.currentconsumer = newcurrentconsumer
self.learnconsumerlist() if self.currentconsumer is not None:
self.learnconsumerlist()
if self.currentConsumer is not None: if self.currentconsumer is not None:
return self.currentConsumer["Host"] return self.currentconsumer["Host"]
else: else:
return None return None
@ -84,14 +98,13 @@ class ConsumerLocator:
Returns the currently selected consumer. Returns the currently selected consumer.
:return: the current consumer :return: the current consumer
""" """
return self.currentConsumer["Host"] return self.currentconsumer["Host"]
def checkcurrentconsumer(self): def checkcurrentconsumer(self):
""" """
Check the consumers health. Check the consumers health.
:return: True if OK, False if fail :return: True if OK, False if fail
""" """
if self.communicator.checkconsumer(self.currentConsumer["Host"]): if self.currentconsumer is None:
return True
else:
return False return False
return self.communicator.checkconsumer(self.currentconsumer["Host"])