2018-04-02 21:11:27 +02:00
|
|
|
import logging
|
|
|
|
from twisted.internet import defer
|
2018-07-23 22:13:56 +02:00
|
|
|
from twisted._threads import AlreadyQuit
|
2018-04-02 21:11:27 +02:00
|
|
|
from ComponentManager import ComponentManager
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class ComponentType(type):
|
|
|
|
def __new__(mcs, name, bases, newattrs):
|
|
|
|
klass = type.__new__(mcs, name, bases, newattrs)
|
|
|
|
if name != "Component":
|
2018-07-24 18:03:43 +02:00
|
|
|
ComponentManager.default_component_classes[klass.component_name] = klass
|
2018-04-02 21:11:27 +02:00
|
|
|
return klass
|
|
|
|
|
|
|
|
|
|
|
|
class Component(object):
|
|
|
|
"""
|
|
|
|
lbrynet-daemon component helper
|
|
|
|
|
|
|
|
Inheriting classes will be automatically registered with the ComponentManager and must implement setup and stop
|
|
|
|
methods
|
|
|
|
"""
|
|
|
|
|
|
|
|
__metaclass__ = ComponentType
|
|
|
|
depends_on = []
|
|
|
|
component_name = None
|
|
|
|
|
2018-07-24 18:03:43 +02:00
|
|
|
def __init__(self, component_manager):
|
|
|
|
self.component_manager = component_manager
|
|
|
|
self._running = False
|
2018-04-02 21:11:27 +02:00
|
|
|
|
2018-07-24 18:03:43 +02:00
|
|
|
def __lt__(self, other):
|
|
|
|
return self.component_name < other.component_name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def running(self):
|
|
|
|
return self._running
|
|
|
|
|
2018-07-25 21:33:43 +02:00
|
|
|
def get_status(self):
|
|
|
|
return
|
|
|
|
|
2018-07-24 18:03:43 +02:00
|
|
|
def start(self):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
2018-07-20 20:48:31 +02:00
|
|
|
@property
|
2018-07-24 18:03:43 +02:00
|
|
|
def component(self):
|
|
|
|
raise NotImplementedError()
|
2018-04-02 21:11:27 +02:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2018-07-24 18:03:43 +02:00
|
|
|
def _setup(self):
|
2018-04-02 21:11:27 +02:00
|
|
|
try:
|
2018-07-24 18:03:43 +02:00
|
|
|
result = yield defer.maybeDeferred(self.start)
|
|
|
|
self._running = True
|
2018-04-02 21:11:27 +02:00
|
|
|
defer.returnValue(result)
|
2018-07-23 22:13:56 +02:00
|
|
|
except (defer.CancelledError, AlreadyQuit):
|
|
|
|
pass
|
2018-04-02 21:11:27 +02:00
|
|
|
except Exception as err:
|
2018-07-24 18:03:43 +02:00
|
|
|
log.exception("Error setting up %s", self.component_name or self.__class__.__name__)
|
2018-04-02 21:11:27 +02:00
|
|
|
raise err
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2018-07-24 18:03:43 +02:00
|
|
|
def _stop(self):
|
2018-04-02 21:11:27 +02:00
|
|
|
try:
|
2018-07-24 18:03:43 +02:00
|
|
|
result = yield defer.maybeDeferred(self.stop)
|
|
|
|
self._running = False
|
2018-04-02 21:11:27 +02:00
|
|
|
defer.returnValue(result)
|
2018-07-23 22:13:56 +02:00
|
|
|
except (defer.CancelledError, AlreadyQuit):
|
|
|
|
pass
|
2018-04-02 21:11:27 +02:00
|
|
|
except Exception as err:
|
2018-07-24 18:03:43 +02:00
|
|
|
log.exception("Error stopping %s", self.__class__.__name__)
|
2018-04-02 21:11:27 +02:00
|
|
|
raise err
|