Skip to content

Commit

Permalink
[Services] add creation_error_message
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeDSM committed Jan 10, 2024
1 parent 39d286e commit 4075a52
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
8 changes: 6 additions & 2 deletions octobot_services/abstract_service_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,18 @@ async def _initialize_impl(self, backtesting_enabled, edited_config) -> bool:

async def _create_or_get_service_instance(self, service, backtesting_enabled, edited_config):
service_factory = services.ServiceFactory(self.config)
if await service_factory.create_or_get_service(service, backtesting_enabled, edited_config):
created, error_message = await service_factory.create_or_get_service(
service, backtesting_enabled, edited_config
)
if created:
return True
else:
log_func = self.get_logger().debug
# log error when the issue is not related to configuration
if service.instance().has_required_configuration():
log_func = self.get_logger().warning
log_func(f"Impossible to start {self.get_name()}: required service {service.get_name()} is not available.")
log_func(f"Impossible to start {self.get_name()}: required service {service.get_name()} "
f"is not available ({error_message}).")
return False

def has_required_services_configuration(self):
Expand Down
7 changes: 4 additions & 3 deletions octobot_services/api/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,21 @@ def get_available_services() -> list:
async def get_service(service_class, is_backtesting, config=None):
# prevent concurrent access when creating a service
async with _service_async_lock(service_class):
if await create_service_factory(
created, error_message = await create_service_factory(
interfaces.get_startup_config(dict_only=True) if config is None else config
).create_or_get_service(
service_class,
is_backtesting,
interfaces.get_edited_config(dict_only=True) if config is None else config
):
)
if created:
service = service_class.instance()
if is_backtesting and not service.BACKTESTING_ENABLED:
raise errors.UnavailableInBacktestingError(
f"{service_class.__name__} service is not available in backtesting"
)
return service
raise errors.CreationError(f"{service_class.__name__} service has not been properly created")
raise errors.CreationError(f"{service_class.__name__} service is not initialized: {error_message}")


def create_service_factory(config) -> services.ServiceFactory:
Expand Down
1 change: 1 addition & 0 deletions octobot_services/services/abstract_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def __init__(self):
self.logger = None
self.config = None
self.edited_config = None
self.creation_error_message = None
self._created = True
self._healthy = False
self._has_been_created = False
Expand Down
14 changes: 9 additions & 5 deletions octobot_services/services/service_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,20 @@ def __init__(self, config):
def get_available_services() -> list:
return [service_class for service_class in services.AbstractService.__subclasses__()]

async def create_or_get_service(self, service_class, backtesting_enabled, edited_config) -> bool:
async def create_or_get_service(self, service_class, backtesting_enabled, edited_config) -> (bool, str):
"""
create_or_get_service will create a service instance if it doesn't exist, check the existing one otherwise
:param service_class: the class of the service to create
:return: True if the created service is working properly, False otherwise
"""
service_instance = service_class.instance()
if service_instance.get_has_been_created():
return service_instance.is_healthy()
return service_instance.is_healthy(), service_instance.creation_error_message
else:
return await self._create_service(service_instance, backtesting_enabled, edited_config)
return (
await self._create_service(service_instance, backtesting_enabled, edited_config),
service_instance.creation_error_message
)

async def _create_service(self, service, backtesting_enabled, edited_config) -> bool:
service.is_backtesting_enabled = backtesting_enabled
Expand All @@ -51,8 +54,9 @@ async def _create_service(self, service, backtesting_enabled, edited_config) ->
return await self._perform_checkup(service)
else:
if service.get_should_warn():
self.logger.info(f"{service.get_name()} can't be initialized: configuration "
f"is missing, wrong or incomplete. This is normal if you did not configure it yet.")
service.creation_error_message = \
"Configuration is missing, wrong or incomplete. This is normal if you did not configure it yet."
self.logger.info(f"{service.get_name()} can't be initialized: {service.creation_error_message}")
return False

async def _perform_checkup(self, service) -> bool:
Expand Down

0 comments on commit 4075a52

Please sign in to comment.