2020-03-29 15:27:36 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2020-03-29 16:38:40 +02:00
|
|
|
import datetime
|
2020-03-29 16:48:59 +02:00
|
|
|
import communicator
|
2020-03-29 18:27:19 +02:00
|
|
|
import os
|
2020-03-29 15:27:36 +02:00
|
|
|
|
|
|
|
"""
|
2020-03-29 18:27:19 +02:00
|
|
|
Consumer locator module, that manages the list of consumers.
|
2020-03-29 15:27:36 +02:00
|
|
|
"""
|
|
|
|
|
2020-03-29 17:21:36 +02:00
|
|
|
__author__ = "@dscharnitzky"
|
2020-03-29 15:27:36 +02:00
|
|
|
__copyright__ = "Copyright 2020, GoldenPogácsa Team"
|
|
|
|
__module_name__ = "consumerlocator"
|
|
|
|
__version__text__ = "1"
|
|
|
|
|
2020-03-29 15:54:52 +02:00
|
|
|
|
2020-03-29 15:26:01 +02:00
|
|
|
class ConsumerLocator:
|
2020-03-29 15:54:52 +02:00
|
|
|
|
2020-03-29 16:46:28 +02:00
|
|
|
"""
|
|
|
|
Manages the list of consumers.
|
|
|
|
"""
|
|
|
|
|
2020-03-29 17:39:31 +02:00
|
|
|
def __init__(self):
|
2020-03-29 16:46:28 +02:00
|
|
|
"""
|
|
|
|
Initialize class.
|
|
|
|
"""
|
2020-03-29 18:27:19 +02:00
|
|
|
os.environ["KnownConsumer"] = "MockRemoveMe"
|
|
|
|
self.consumerlist = [{"Host": os.environ["KnownConsumer"], "State": True, "LastOk": datetime.datetime.now()}]
|
|
|
|
self.currentconsumer = self.consumerlist[0]
|
|
|
|
print(self.currentconsumer)
|
2020-03-29 17:39:31 +02:00
|
|
|
|
2020-03-29 18:27:19 +02:00
|
|
|
def initcommunicator(self, comm: communicator.Communicator):
|
|
|
|
"""
|
|
|
|
Initialize the reference to the communicator
|
|
|
|
:param comm: is the communicator
|
|
|
|
"""
|
2020-03-29 17:19:05 +02:00
|
|
|
self.communicator = comm
|
2020-03-29 15:54:52 +02:00
|
|
|
|
2020-03-29 16:50:23 +02:00
|
|
|
def learnconsumerlist(self):
|
2020-03-29 15:54:52 +02:00
|
|
|
""""
|
|
|
|
Learns the list of consumers.
|
|
|
|
"""
|
2020-03-29 18:00:00 +02:00
|
|
|
recievedconsumerlist = self.communicator.discoveravailableconsumers()
|
|
|
|
if recievedconsumerlist is None:
|
|
|
|
return
|
|
|
|
for consumer in recievedconsumerlist:
|
2020-03-29 16:38:40 +02:00
|
|
|
self.consumerList.append({"Host": consumer, "State": True, "LastOk": datetime.datetime.now()})
|
2020-03-29 18:27:19 +02:00
|
|
|
self.updateconsumerlist()
|
2020-03-29 16:38:40 +02:00
|
|
|
|
2020-03-29 16:50:23 +02:00
|
|
|
def updateconsumerlist(self):
|
2020-03-29 16:38:40 +02:00
|
|
|
"""
|
|
|
|
Updates the consumer list based on their availability.
|
|
|
|
"""
|
2020-03-29 18:27:19 +02:00
|
|
|
removelist = []
|
|
|
|
for consumer in self.consumerlist:
|
2020-03-29 17:19:05 +02:00
|
|
|
if not self.communicator.checkconsumer(consumer["Host"]):
|
2020-03-29 16:38:40 +02:00
|
|
|
consumer["State"] = False
|
|
|
|
if datetime.datetime.now() - consumer["LastOk"] > datetime.timedelta(hours=1):
|
2020-03-29 18:27:19 +02:00
|
|
|
removelist.append(consumer)
|
2020-03-29 16:38:40 +02:00
|
|
|
else:
|
|
|
|
consumer["LastOk"] = datetime.datetime.now()
|
2020-03-29 18:27:19 +02:00
|
|
|
for rem in removelist:
|
|
|
|
self.consumerlist.remove(rem)
|
2020-03-29 15:54:52 +02:00
|
|
|
|
2020-03-29 16:50:23 +02:00
|
|
|
def updateconsumer(self):
|
2020-03-29 15:54:52 +02:00
|
|
|
"""
|
2020-03-29 16:38:40 +02:00
|
|
|
Checks all the consumers in the list and updates the current consumer if necessary.
|
2020-03-29 16:46:28 +02:00
|
|
|
:return: the current consumer or None if there are no available customers at the moment.
|
2020-03-29 15:54:52 +02:00
|
|
|
"""
|
2020-03-29 18:27:19 +02:00
|
|
|
self.updateconsumerlist()
|
2020-03-29 16:38:40 +02:00
|
|
|
|
2020-03-29 18:27:19 +02:00
|
|
|
if not self.checkcurrentconsumer():
|
2020-03-29 16:38:40 +02:00
|
|
|
|
2020-03-29 18:27:19 +02:00
|
|
|
newcurrentconsumer = None
|
2020-03-29 16:38:40 +02:00
|
|
|
|
2020-03-29 18:27:19 +02:00
|
|
|
for consumer in self.consumerlist:
|
2020-03-29 16:38:40 +02:00
|
|
|
if consumer["State"]:
|
2020-03-29 18:27:19 +02:00
|
|
|
newcurrentconsumer = consumer
|
2020-03-29 16:38:40 +02:00
|
|
|
break
|
|
|
|
|
2020-03-29 18:27:19 +02:00
|
|
|
self.currentconsumer = newcurrentconsumer
|
2020-03-29 18:00:00 +02:00
|
|
|
self.learnconsumerlist()
|
2020-03-29 16:38:40 +02:00
|
|
|
|
2020-03-29 18:27:19 +02:00
|
|
|
if self.currentconsumer is not None:
|
|
|
|
return self.currentconsumer["Host"]
|
2020-03-29 16:38:40 +02:00
|
|
|
else:
|
|
|
|
return None
|
2020-03-29 15:54:52 +02:00
|
|
|
|
2020-03-29 16:50:23 +02:00
|
|
|
def getcurrentconsumer(self):
|
2020-03-29 15:54:52 +02:00
|
|
|
"""
|
|
|
|
Returns the currently selected consumer.
|
|
|
|
:return: the current consumer
|
|
|
|
"""
|
2020-03-29 18:27:19 +02:00
|
|
|
return self.currentconsumer["Host"]
|
2020-03-29 15:54:52 +02:00
|
|
|
|
2020-03-29 16:57:13 +02:00
|
|
|
def checkcurrentconsumer(self):
|
2020-03-29 15:54:52 +02:00
|
|
|
"""
|
|
|
|
Check the consumers health.
|
|
|
|
:return: True if OK, False if fail
|
|
|
|
"""
|
2020-03-29 18:27:19 +02:00
|
|
|
# return self.communicator.checkconsumer(self.currentconsumer["Host"])
|
|
|
|
return True # TODO remove this line and uncomment the above
|