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
|
2019-01-21 21:55:50 +01:00
|
|
|
|
|
|
|
from lbrynet.conf import Config
|
2018-11-09 20:02:03 +01:00
|
|
|
from lbrynet.extras.daemon.ComponentManager import ComponentManager
|
2018-04-02 21:11:27 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2018-07-22 00:34:59 +02:00
|
|
|
class Component(metaclass=ComponentType):
|
2018-04-02 21:11:27 +02:00
|
|
|
"""
|
|
|
|
lbrynet-daemon component helper
|
|
|
|
|
|
|
|
Inheriting classes will be automatically registered with the ComponentManager and must implement setup and stop
|
|
|
|
methods
|
|
|
|
"""
|
|
|
|
|
|
|
|
depends_on = []
|
|
|
|
component_name = None
|
|
|
|
|
2018-07-24 18:03:43 +02:00
|
|
|
def __init__(self, component_manager):
|
2019-01-21 21:55:50 +01:00
|
|
|
self.conf: Config = component_manager.conf
|
2018-07-24 18:03:43 +02:00
|
|
|
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-12-15 21:31:02 +01:00
|
|
|
async def get_status(self):
|
2018-07-25 21:33:43 +02:00
|
|
|
return
|
|
|
|
|
2018-12-15 21:31:02 +01:00
|
|
|
async def start(self):
|
2018-07-24 18:03:43 +02:00
|
|
|
raise NotImplementedError()
|
|
|
|
|
2018-12-15 21:31:02 +01:00
|
|
|
async def stop(self):
|
2018-07-24 18:03:43 +02:00
|
|
|
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
|
|
|
|
2018-12-15 21:31:02 +01:00
|
|
|
async def _setup(self):
|
2018-04-02 21:11:27 +02:00
|
|
|
try:
|
2018-12-15 21:31:02 +01:00
|
|
|
result = await self.start()
|
2018-07-24 18:03:43 +02:00
|
|
|
self._running = True
|
2018-12-15 21:31:02 +01:00
|
|
|
return 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
|
|
|
|
|
2018-12-15 21:31:02 +01:00
|
|
|
async def _stop(self):
|
2018-04-02 21:11:27 +02:00
|
|
|
try:
|
2018-12-15 21:31:02 +01:00
|
|
|
result = await self.stop()
|
2018-07-24 18:03:43 +02:00
|
|
|
self._running = False
|
2018-12-15 21:31:02 +01:00
|
|
|
return 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
|