Communicator API impementation #1
18
app.py
18
app.py
@ -3,7 +3,7 @@ import sentry_sdk
|
|||||||
import communicator
|
import communicator
|
||||||
import consumerlocator
|
import consumerlocator
|
||||||
import messagesender
|
import messagesender
|
||||||
|
import time
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Main Flask RESTful API
|
Main Flask RESTful API
|
||||||
@ -27,9 +27,17 @@ if __name__ == "__main__":
|
|||||||
print("Producer: started sending")
|
print("Producer: started sending")
|
||||||
for i in range(30):
|
for i in range(30):
|
||||||
msg = message.createMessage(20)
|
msg = message.createMessage(20)
|
||||||
print("Producer: send ", i, "th message: ", msg)
|
print("Producer: trying to send ", i, "th message: ", msg)
|
||||||
if not conslist.checkcurrentconsumer():
|
available = False
|
||||||
conslist.updateconsumer()
|
if conslist.updateconsumer() is None:
|
||||||
comm.sendmessage(msg)
|
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")
|
print("Producer: finished")
|
@ -24,10 +24,9 @@ class ConsumerLocator:
|
|||||||
"""
|
"""
|
||||||
Initialize class.
|
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.consumerlist = [{"Host": os.environ["KnownConsumer"], "State": True, "LastOk": datetime.datetime.now()}]
|
||||||
self.currentconsumer = self.consumerlist[0]
|
self.currentconsumer = self.consumerlist[0]
|
||||||
print(self.currentconsumer)
|
|
||||||
|
|
||||||
def initcommunicator(self, comm: communicator.Communicator):
|
def initcommunicator(self, comm: communicator.Communicator):
|
||||||
"""
|
"""
|
||||||
@ -43,8 +42,15 @@ 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
|
||||||
|
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()
|
self.updateconsumerlist()
|
||||||
|
|
||||||
def updateconsumerlist(self):
|
def updateconsumerlist(self):
|
||||||
@ -59,18 +65,18 @@ class ConsumerLocator:
|
|||||||
removelist.append(consumer)
|
removelist.append(consumer)
|
||||||
else:
|
else:
|
||||||
consumer["LastOk"] = datetime.datetime.now()
|
consumer["LastOk"] = datetime.datetime.now()
|
||||||
|
consumer["State"] = True
|
||||||
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.
|
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.checkcurrentconsumer():
|
if not self.checkcurrentconsumer():
|
||||||
|
self.updateconsumerlist()
|
||||||
newcurrentconsumer = None
|
newcurrentconsumer = None
|
||||||
|
|
||||||
for consumer in self.consumerlist:
|
for consumer in self.consumerlist:
|
||||||
@ -79,7 +85,8 @@ class ConsumerLocator:
|
|||||||
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"]
|
||||||
@ -98,5 +105,6 @@ class ConsumerLocator:
|
|||||||
Check the consumers health.
|
Check the consumers health.
|
||||||
:return: True if OK, False if fail
|
:return: True if OK, False if fail
|
||||||
"""
|
"""
|
||||||
# return self.communicator.checkconsumer(self.currentconsumer["Host"])
|
if self.currentconsumer is None:
|
||||||
return True # TODO remove this line and uncomment the above
|
return False
|
||||||
|
return self.communicator.checkconsumer(self.currentconsumer["Host"])
|
||||||
|
Reference in New Issue
Block a user