2018-04-02 21:11:27 +02:00
|
|
|
import logging
|
|
|
|
from twisted.internet import defer
|
|
|
|
|
2018-07-24 18:03:43 +02:00
|
|
|
from lbrynet.core.Error import ComponentStartConditionNotMet
|
|
|
|
|
2018-04-02 21:11:27 +02:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2018-07-25 07:23:02 +02:00
|
|
|
class RegisteredConditions:
|
2018-07-25 00:35:18 +02:00
|
|
|
conditions = {}
|
|
|
|
|
|
|
|
|
|
|
|
class RequiredConditionType(type):
|
|
|
|
def __new__(mcs, name, bases, newattrs):
|
|
|
|
klass = type.__new__(mcs, name, bases, newattrs)
|
|
|
|
if name != "RequiredCondition":
|
|
|
|
if klass.name in RegisteredConditions.conditions:
|
|
|
|
raise SyntaxError("already have a component registered for \"%s\"" % klass.name)
|
|
|
|
RegisteredConditions.conditions[klass.name] = klass
|
|
|
|
return klass
|
|
|
|
|
|
|
|
|
2018-07-25 07:15:51 +02:00
|
|
|
class RequiredCondition(metaclass=RequiredConditionType):
|
2018-07-25 00:35:18 +02:00
|
|
|
name = ""
|
|
|
|
component = ""
|
|
|
|
message = ""
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def evaluate(component):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
|
2018-07-25 07:23:02 +02:00
|
|
|
class ComponentManager:
|
2018-07-24 18:03:43 +02:00
|
|
|
default_component_classes = {}
|
|
|
|
|
|
|
|
def __init__(self, reactor=None, analytics_manager=None, skip_components=None, **override_components):
|
|
|
|
self.skip_components = skip_components or []
|
|
|
|
|
|
|
|
self.reactor = reactor
|
|
|
|
self.component_classes = {}
|
|
|
|
self.components = set()
|
|
|
|
self.analytics_manager = analytics_manager
|
|
|
|
|
2018-07-06 07:17:20 +02:00
|
|
|
for component_name, component_class in self.default_component_classes.items():
|
2018-07-24 18:03:43 +02:00
|
|
|
if component_name in override_components:
|
|
|
|
component_class = override_components.pop(component_name)
|
|
|
|
if component_name not in self.skip_components:
|
|
|
|
self.component_classes[component_name] = component_class
|
|
|
|
|
|
|
|
if override_components:
|
|
|
|
raise SyntaxError("unexpected components: %s" % override_components)
|
|
|
|
|
2018-07-06 07:17:20 +02:00
|
|
|
for component_class in self.component_classes.values():
|
2018-07-24 18:03:43 +02:00
|
|
|
self.components.add(component_class(self))
|
2018-04-02 21:11:27 +02:00
|
|
|
|
2018-07-25 00:35:18 +02:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def evaluate_condition(self, condition_name):
|
|
|
|
if condition_name not in RegisteredConditions.conditions:
|
|
|
|
raise NameError(condition_name)
|
|
|
|
condition = RegisteredConditions.conditions[condition_name]
|
|
|
|
try:
|
|
|
|
component = self.get_component(condition.component)
|
|
|
|
result = yield defer.maybeDeferred(condition.evaluate, component)
|
|
|
|
except Exception as err:
|
|
|
|
result = False
|
|
|
|
defer.returnValue((result, "" if result else condition.message))
|
|
|
|
|
2018-07-24 18:03:43 +02:00
|
|
|
def sort_components(self, reverse=False):
|
2018-04-02 21:11:27 +02:00
|
|
|
"""
|
|
|
|
Sort components by requirements
|
|
|
|
"""
|
|
|
|
steps = []
|
|
|
|
staged = set()
|
2018-07-24 18:03:43 +02:00
|
|
|
components = set(self.components)
|
2018-04-02 21:11:27 +02:00
|
|
|
|
|
|
|
# components with no requirements
|
|
|
|
step = []
|
|
|
|
for component in set(components):
|
|
|
|
if not component.depends_on:
|
|
|
|
step.append(component)
|
|
|
|
staged.add(component.component_name)
|
|
|
|
components.remove(component)
|
|
|
|
|
|
|
|
if step:
|
2018-07-24 18:03:43 +02:00
|
|
|
step.sort()
|
2018-04-02 21:11:27 +02:00
|
|
|
steps.append(step)
|
|
|
|
|
|
|
|
while components:
|
|
|
|
step = []
|
|
|
|
to_stage = set()
|
|
|
|
for component in set(components):
|
|
|
|
reqs_met = 0
|
|
|
|
for needed in component.depends_on:
|
|
|
|
if needed in staged:
|
|
|
|
reqs_met += 1
|
|
|
|
if reqs_met == len(component.depends_on):
|
|
|
|
step.append(component)
|
|
|
|
to_stage.add(component.component_name)
|
|
|
|
components.remove(component)
|
|
|
|
if step:
|
2018-07-24 18:03:43 +02:00
|
|
|
step.sort()
|
2018-04-02 21:11:27 +02:00
|
|
|
staged.update(to_stage)
|
|
|
|
steps.append(step)
|
|
|
|
elif components:
|
2018-07-24 18:03:43 +02:00
|
|
|
raise ComponentStartConditionNotMet("Unresolved dependencies for: %s" % components)
|
2018-04-02 21:11:27 +02:00
|
|
|
if reverse:
|
|
|
|
steps.reverse()
|
|
|
|
return steps
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2018-07-24 18:03:43 +02:00
|
|
|
def setup(self, **callbacks):
|
2018-04-02 21:11:27 +02:00
|
|
|
"""
|
|
|
|
Start Components in sequence sorted by requirements
|
|
|
|
|
|
|
|
:return: (defer.Deferred)
|
|
|
|
"""
|
2018-07-24 18:03:43 +02:00
|
|
|
|
2018-07-06 07:17:20 +02:00
|
|
|
for component_name, cb in callbacks.items():
|
2018-07-24 18:03:43 +02:00
|
|
|
if component_name not in self.component_classes:
|
|
|
|
raise NameError("unknown component: %s" % component_name)
|
2018-04-02 22:49:48 +02:00
|
|
|
if not callable(cb):
|
|
|
|
raise ValueError("%s is not callable" % cb)
|
|
|
|
|
|
|
|
def _setup(component):
|
|
|
|
if component.component_name in callbacks:
|
|
|
|
d = component._setup()
|
2018-07-20 20:48:31 +02:00
|
|
|
d.addCallback(callbacks[component.component_name], component)
|
2018-04-02 22:49:48 +02:00
|
|
|
return d
|
2018-07-24 18:03:43 +02:00
|
|
|
return component._setup()
|
2018-04-02 22:49:48 +02:00
|
|
|
|
2018-07-24 18:03:43 +02:00
|
|
|
stages = self.sort_components()
|
2018-04-02 21:11:27 +02:00
|
|
|
for stage in stages:
|
2018-07-31 15:48:57 +02:00
|
|
|
yield defer.DeferredList([_setup(component) for component in stage if not component.running])
|
2018-04-02 21:11:27 +02:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2018-07-24 18:03:43 +02:00
|
|
|
def stop(self):
|
2018-04-02 21:11:27 +02:00
|
|
|
"""
|
|
|
|
Stop Components in reversed startup order
|
|
|
|
|
|
|
|
:return: (defer.Deferred)
|
|
|
|
"""
|
2018-07-24 18:03:43 +02:00
|
|
|
stages = self.sort_components(reverse=True)
|
2018-04-02 21:11:27 +02:00
|
|
|
for stage in stages:
|
2018-07-24 18:03:43 +02:00
|
|
|
yield defer.DeferredList([component._stop() for component in stage if component.running])
|
2018-04-02 21:11:27 +02:00
|
|
|
|
2018-07-24 18:03:43 +02:00
|
|
|
def all_components_running(self, *component_names):
|
2018-04-02 21:11:27 +02:00
|
|
|
"""
|
2018-04-02 22:49:48 +02:00
|
|
|
Check if components are running
|
|
|
|
|
2018-04-02 21:11:27 +02:00
|
|
|
:return: (bool) True if all specified components are running
|
|
|
|
"""
|
2018-07-24 18:03:43 +02:00
|
|
|
components = {component.component_name: component for component in self.components}
|
2018-04-02 21:11:27 +02:00
|
|
|
for component in component_names:
|
2018-04-02 22:49:48 +02:00
|
|
|
if component not in components:
|
2018-04-02 21:11:27 +02:00
|
|
|
raise NameError("%s is not a known Component" % component)
|
2018-04-02 22:49:48 +02:00
|
|
|
if not components[component].running:
|
2018-04-02 21:11:27 +02:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2018-07-24 18:03:43 +02:00
|
|
|
def get_components_status(self):
|
|
|
|
"""
|
|
|
|
List status of all the components, whether they are running or not
|
|
|
|
|
|
|
|
:return: (dict) {(str) component_name: (bool) True is running else False}
|
|
|
|
"""
|
|
|
|
return {
|
|
|
|
component.component_name: component.running
|
|
|
|
for component in self.components
|
|
|
|
}
|
|
|
|
|
|
|
|
def get_component(self, component_name):
|
|
|
|
for component in self.components:
|
2018-04-02 21:11:27 +02:00
|
|
|
if component.component_name == component_name:
|
2018-07-24 18:03:43 +02:00
|
|
|
return component.component
|
2018-04-02 21:11:27 +02:00
|
|
|
raise NameError(component_name)
|