Fixed a lot of typos in consumerlocator and added some mocks

This commit is contained in:
Scharnitzky Donát 2020-03-29 18:27:19 +02:00
parent 39c34f2a67
commit 9783b8c20b
2 changed files with 31 additions and 26 deletions

6
app.py
View File

@ -20,14 +20,14 @@ if __name__ == "__main__":
print("Producer: init") print("Producer: init")
comm = communicator.Communicator() comm = communicator.Communicator()
conslist = consumerlocator.ConsumerLocator() conslist = consumerlocator.ConsumerLocator()
conslist.initCommunicator(comm) conslist.initcommunicator(comm)
message = messagesender.MessageSender() message = messagesender.MessageSender()
conslist.learnconsumerlist() conslist.learnconsumerlist()
print("Producer: started sending") print("Producer: started sending")
for i in range(30): 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(): if not conslist.checkcurrentconsumer():
conslist.updateconsumer() conslist.updateconsumer()
comm.sendmessage(msg) comm.sendmessage(msg)

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,16 @@ class ConsumerLocator:
""" """
Initialize class. Initialize class.
""" """
self.consumerList = [{"Host": "KnownHost", "State": True, "LastOk": datetime.datetime.now()}] os.environ["KnownConsumer"] = "MockRemoveMe"
self.currentConsumer = self.consumerList[0]["Host"] 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 self.communicator = comm
def learnconsumerlist(self): def learnconsumerlist(self):
@ -38,44 +45,44 @@ class ConsumerLocator:
return return
for consumer in recievedconsumerlist: for consumer in recievedconsumerlist:
self.consumerList.append({"Host": consumer, "State": True, "LastOk": datetime.datetime.now()}) self.consumerList.append({"Host": consumer, "State": True, "LastOk": datetime.datetime.now()})
self.updateConsumerList() 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: for rem in removelist:
self.consumerList.remove(rem) self.consumerlist.remove(rem)
def updateconsumer(self): def updateconsumer(self):
""" """
Checks all the consumers in the list and updates the current consumer if necessary. 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. :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"]: if consumer["State"]:
newCurrentConsumer = consumer newcurrentconsumer = consumer
break break
self.currentConsumer = newCurrentConsumer self.currentconsumer = newcurrentconsumer
self.learnconsumerlist() 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 +91,12 @@ 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"]): # return self.communicator.checkconsumer(self.currentconsumer["Host"])
return True return True # TODO remove this line and uncomment the above
else:
return False