2019-01-22 23:44:17 +01:00
|
|
|
import asyncio
|
2018-04-02 21:11:27 +02:00
|
|
|
import logging
|
2019-06-21 02:55:47 +02:00
|
|
|
from lbry.conf import Config
|
2020-01-03 07:42:54 +01:00
|
|
|
from lbry.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)
|
2019-12-15 08:06:04 +01:00
|
|
|
if name != "Component" and newattrs['__module__'] != 'lbry.testcase':
|
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
|
|
|
"""
|
2019-06-21 03:02:58 +02:00
|
|
|
lbry-daemon component helper
|
2018-04-02 21:11:27 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2022-06-16 14:52:40 +02:00
|
|
|
async def get_status(self): # pylint: disable=no-self-use
|
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
|
2019-01-22 23:44:17 +01:00
|
|
|
except asyncio.CancelledError:
|
2019-07-08 03:03:05 +02:00
|
|
|
log.info("Cancelled setup of %s component", self.__class__.__name__)
|
|
|
|
raise
|
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
|
2019-01-22 23:44:17 +01:00
|
|
|
except asyncio.CancelledError:
|
2019-07-08 03:03:05 +02:00
|
|
|
log.info("Cancelled stop of %s component", self.__class__.__name__)
|
|
|
|
raise
|
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
|