Added redis to consumerlocator
This commit is contained in:
@@ -7,13 +7,14 @@ Consumer locator module, that manages the list of consumers.
|
||||
import datetime
|
||||
from communicator import Communicator
|
||||
import os
|
||||
from redisconnector import RedisConnector
|
||||
|
||||
__author__ = "@dscharnitzky"
|
||||
__copyright__ = "Copyright 2020, GoldenPogácsa Team"
|
||||
__module_name__ = "consumerlocator"
|
||||
__version__text__ = "1"
|
||||
|
||||
KNOWNCONSUMER = os.getenv("PRODUCER_KNOWNCONSUMER",'10.69.42.1')
|
||||
KNOWNCONSUMER = os.getenv("PRODUCER_KNOWNCONSUMER", '10.69.42.1')
|
||||
|
||||
|
||||
class ConsumerLocator:
|
||||
@@ -22,7 +23,7 @@ class ConsumerLocator:
|
||||
Component responsible for managing the list of consumers. Requires an instance of :class:`communicator.Communicator`
|
||||
"""
|
||||
|
||||
def __init__(self, uuid: str, communicator: Communicator):
|
||||
def __init__(self, uuid: str, communicator: Communicator, redisconnector: RedisConnector):
|
||||
"""**Constructor:**
|
||||
Initializes the object.
|
||||
|
||||
@@ -31,8 +32,9 @@ class ConsumerLocator:
|
||||
:param uuid: Not used
|
||||
:param communicator: the :class:'communicator.Communicator' instance that will be used for the low level communication.
|
||||
"""
|
||||
self.consumerlist = [{"Host": KNOWNCONSUMER, "State": True, "LastOk": datetime.datetime.now()}]
|
||||
self.currentconsumer = self.consumerlist[0]
|
||||
self.red = redisconnector
|
||||
self.red.consumerlist = [{"Host": KNOWNCONSUMER, "State": True, "LastOk": datetime.datetime.now()}]
|
||||
self.red.currentconsumer = self.red.consumerlist[0]
|
||||
self.communicator = communicator
|
||||
|
||||
def learnconsumerlist(self) -> None:
|
||||
@@ -48,12 +50,12 @@ class ConsumerLocator:
|
||||
return
|
||||
for recconsumer in recievedconsumerlist:
|
||||
contains = False
|
||||
for consumer in self.consumerlist:
|
||||
for consumer in self.red.consumerlist:
|
||||
if consumer["Host"] == recconsumer:
|
||||
contains = True
|
||||
|
||||
if not contains:
|
||||
self.consumerlist.append({"Host": recconsumer, "State": True, "LastOk": datetime.datetime.now()})
|
||||
self.red.consumerlist.append({"Host": recconsumer, "State": True, "LastOk": datetime.datetime.now()})
|
||||
|
||||
self.updateconsumerlist()
|
||||
|
||||
@@ -66,7 +68,7 @@ class ConsumerLocator:
|
||||
:return: None
|
||||
"""
|
||||
removelist = []
|
||||
for consumer in self.consumerlist:
|
||||
for consumer in self.red.consumerlist:
|
||||
if not self.communicator.checkconsumer(consumer["Host"]):
|
||||
consumer["State"] = False
|
||||
if datetime.datetime.now() - consumer["LastOk"] > datetime.timedelta(hours=1):
|
||||
@@ -75,7 +77,7 @@ class ConsumerLocator:
|
||||
consumer["LastOk"] = datetime.datetime.now()
|
||||
consumer["State"] = True
|
||||
for rem in removelist:
|
||||
self.consumerlist.remove(rem)
|
||||
self.red.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.
|
||||
@@ -91,18 +93,18 @@ class ConsumerLocator:
|
||||
self.updateconsumerlist()
|
||||
newcurrentconsumer = None
|
||||
|
||||
for consumer in self.consumerlist:
|
||||
for consumer in self.red.consumerlist:
|
||||
if consumer["State"]:
|
||||
newcurrentconsumer = consumer
|
||||
break
|
||||
|
||||
self.currentconsumer = newcurrentconsumer
|
||||
if self.currentconsumer is not None:
|
||||
self.red.currentconsumer = newcurrentconsumer
|
||||
if self.red.currentconsumer is not None:
|
||||
self.learnconsumerlist()
|
||||
|
||||
if self.currentconsumer is not None:
|
||||
self.communicator.set_currentconsumer(self.currentconsumer["Host"])
|
||||
return self.currentconsumer["Host"]
|
||||
if self.red.currentconsumer is not None:
|
||||
self.communicator.set_currentconsumer(self.red.currentconsumer["Host"])
|
||||
return self.red.currentconsumer["Host"]
|
||||
else:
|
||||
return None
|
||||
|
||||
@@ -111,13 +113,13 @@ class ConsumerLocator:
|
||||
|
||||
:return: the current consumer
|
||||
"""
|
||||
return self.currentconsumer["Host"]
|
||||
return self.red.currentconsumer["Host"]
|
||||
|
||||
def checkcurrentconsumer(self) -> bool:
|
||||
"""Check the current consumer's health.
|
||||
|
||||
:return: True if OK, False if fail
|
||||
"""
|
||||
if self.currentconsumer is None:
|
||||
if self.red.currentconsumer is None:
|
||||
return False
|
||||
return self.communicator.checkconsumer(self.currentconsumer["Host"])
|
||||
return self.communicator.checkconsumer(self.red.currentconsumer["Host"])
|
||||
|
||||
Reference in New Issue
Block a user