Skip to content

Commit

Permalink
Merge pull request #96 from s-kostyuk/graceful_degradation
Browse files Browse the repository at this point in the history
Graceful degradation for local_announce module

This pull request changes the logic of initialization for local_announce module of the system and local announcement API itself.

From now the local_announce module will not be imported until it'll become explicitly needed to satisfy user requirements (i.e. until it will be explicitly enabled in everpl configuration file). If the module will be enabled by user but will not be able to be imported, the user will receive an error message and the whole platform will crash with exception.

Closes #94
  • Loading branch information
s-kostyuk committed May 4, 2018
2 parents 0df1de1 + 9233b62 commit 6baa455
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions dpl/core/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
from dpl.api.rest_api.messages_subapp import build_messages_subapp
from dpl.api.rest_api.rest_api_provider import RestApiProvider

from dpl.api.local_announce import LocalAnnounce

module_logger = logging.getLogger(__name__)
dpl_root_logger = logging.getLogger(name='dpl')
Expand Down Expand Up @@ -181,7 +180,31 @@ def __init__(self):
auth_service=self._auth_service
)

self._local_announce = LocalAnnounce()
# None will indicate that this module was disabled
self._local_announce = None

# if module ws enabled...
if 'local_announce' in self._apis_config['enabled_apis']:
self._initialize_local_announcement()

def _initialize_local_announcement(self):
"""
Performs an import of local_announce module an initializes the
self._local_announce field with LocalAnnounce constructor
:return: None
"""
try: # ...try to import a module and initialize its instance
from dpl.api.local_announce import LocalAnnounce
self._local_announce = LocalAnnounce()
except ImportError as e: # if something gone wrong...
# ... print an error ...
logging.error(
"Failed to enable local_announce module: %s. Install all "
"missing dependencies or disable local_announce module in "
"everpl config file" % e
)
raise # ...and terminate

def parse_arguments(self):
"""
Expand Down Expand Up @@ -350,6 +373,8 @@ async def _bootstrap_integrations(self):
self._db_session_manager.remove_session()

async def shutdown(self):
if self._local_announce is not None:
self._local_announce.shutdown_server()

await self._rest_api.shutdown_server()
self._local_announce.shutdown_server()
self._thing_service_raw.disable_all()

0 comments on commit 6baa455

Please sign in to comment.