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

View File

@ -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