This repository has been archived on 2020-09-24. You can view files and clone it, but cannot push or open issues or pull requests.
producer/consumerlocator.py
Torma Kristóf 50dec05e6b
Some checks reported errors
continuous-integration/drone/pr Build was killed
continuous-integration/drone/push Build was killed
tests done
2020-03-30 19:33:09 +02:00

105 lines
3.3 KiB
Python

#!/usr/bin/env python
import datetime
from communicator import Communicator
import os
"""
Consumer locator module, that manages the list of consumers.
"""
__author__ = "@dscharnitzky"
__copyright__ = "Copyright 2020, GoldenPogácsa Team"
__module_name__ = "consumerlocator"
__version__text__ = "1"
class ConsumerLocator:
"""
Manages the list of consumers.
"""
def __init__(self, uuid: str, communicator: Communicator):
"""
Initialize class.
"""
self.consumerlist = [{"Host": os.environ["KnownConsumer"], "State": True, "LastOk": datetime.datetime.now()}]
self.currentconsumer = self.consumerlist[0]
self.communicator = communicator
def learnconsumerlist(self) -> None:
""""
Learns the list of consumers.
"""
recievedconsumerlist = self.communicator.discoveravailableconsumers()
if recievedconsumerlist is None:
return
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) -> None:
"""
Updates the consumer list based on their availability.
"""
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)
else:
consumer["LastOk"] = datetime.datetime.now()
consumer["State"] = True
for rem in removelist:
self.consumerlist.remove(rem)
def updateconsumer(self):
"""
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.
"""
if not self.checkcurrentconsumer():
self.updateconsumerlist()
newcurrentconsumer = None
for consumer in self.consumerlist:
if consumer["State"]:
newcurrentconsumer = consumer
break
self.currentconsumer = newcurrentconsumer
if self.currentconsumer is not None:
self.learnconsumerlist()
if self.currentconsumer is not None:
self.communicator.set_currentconsumer(self.currentconsumer["Host"])
return self.currentconsumer["Host"]
else:
return None
def getcurrentconsumer(self) -> str:
"""
Returns the currently selected consumer.
:return: the current consumer
"""
return self.currentconsumer["Host"]
def checkcurrentconsumer(self) -> bool:
"""
Check the consumers health.
:return: True if OK, False if fail
"""
if self.currentconsumer is None:
return False
return self.communicator.checkconsumer(self.currentconsumer["Host"])