Communicator API impementation #1

Closed
tormakris wants to merge 4 commits from master into dev-communicator
2 changed files with 31 additions and 15 deletions
Showing only changes of commit 2b44a5bbad - Show all commits

16
app.py
View File

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

View File

@ -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,6 +85,7 @@ class ConsumerLocator:
break
self.currentconsumer = newcurrentconsumer
if self.currentconsumer is not None:
self.learnconsumerlist()
if self.currentconsumer is not None:
@ -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"])