Renamed ConsumerLocator to lowercase
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
91
consumerlocator.py
Normal file
91
consumerlocator.py
Normal file
@@ -0,0 +1,91 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import datetime
|
||||
import communicator
|
||||
|
||||
"""
|
||||
Main Flask RESTful API
|
||||
"""
|
||||
|
||||
__author__ = "@tormakris"
|
||||
__copyright__ = "Copyright 2020, GoldenPogácsa Team"
|
||||
__module_name__ = "consumerlocator"
|
||||
__version__text__ = "1"
|
||||
|
||||
|
||||
class ConsumerLocator:
|
||||
|
||||
"""
|
||||
Manages the list of consumers.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
"""
|
||||
Initialize class.
|
||||
"""
|
||||
self.consumerList = [{"Host": "KnownHost", "State": True, "LastOk": datetime.datetime.now()}]
|
||||
self.currentConsumer = self.consumerList[0]["Host"]
|
||||
|
||||
def learnconsumerlist(self):
|
||||
""""
|
||||
Learns the list of consumers.
|
||||
"""
|
||||
recievedConsumerList = communicator.discoveravailableconsumers()
|
||||
for consumer in recievedConsumerList:
|
||||
self.consumerList.append({"Host": consumer, "State": True, "LastOk": datetime.datetime.now()})
|
||||
self.updateConsumerList()
|
||||
|
||||
def updateconsumerlist(self):
|
||||
"""
|
||||
Updates the consumer list based on their availability.
|
||||
"""
|
||||
removeList = []
|
||||
for consumer in self.consumerList:
|
||||
if not communicator.checkconsumer(consumer["Host"]):
|
||||
consumer["State"] = False
|
||||
if datetime.datetime.now() - consumer["LastOk"] > datetime.timedelta(hours=1):
|
||||
removeList.append(consumer)
|
||||
else:
|
||||
consumer["LastOk"] = datetime.datetime.now()
|
||||
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()
|
||||
|
||||
if not self.checkConsumer():
|
||||
|
||||
newCurrentConsumer = None
|
||||
|
||||
for consumer in self.consumerList:
|
||||
if consumer["State"]:
|
||||
newCurrentConsumer = consumer
|
||||
break
|
||||
|
||||
self.currentConsumer = newCurrentConsumer
|
||||
|
||||
if self.currentConsumer is not None:
|
||||
return self.currentConsumer["Host"]
|
||||
else:
|
||||
return None
|
||||
|
||||
def getcurrentconsumer(self):
|
||||
"""
|
||||
Returns the currently selected consumer.
|
||||
:return: the current consumer
|
||||
"""
|
||||
return self.currentConsumer["Host"]
|
||||
|
||||
def checkcurrentconsumer(self):
|
||||
"""
|
||||
Check the consumers health.
|
||||
:return: True if OK, False if fail
|
||||
"""
|
||||
if communicator.checkconsumer(self.currentConsumer["Host"]):
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
Reference in New Issue
Block a user