switch startup sequence to use inlineCallbacks

This cleans up the process, makes it more readable,
and better surfaces errors.
This commit is contained in:
Job Evers-Meltzer 2016-12-28 11:20:04 -06:00
parent fc24d9b5aa
commit 2c7fc3fb46
3 changed files with 30 additions and 37 deletions

View file

@ -290,13 +290,10 @@ class Daemon(AuthJSONRPCServer):
self.lbry_file_metadata_manager = None self.lbry_file_metadata_manager = None
self.lbry_file_manager = None self.lbry_file_manager = None
@defer.inlineCallbacks
def setup(self): def setup(self):
self._modify_loggly_formatter() self._modify_loggly_formatter()
def _log_starting_vals():
log.info("Starting balance: " + str(self.session.wallet.wallet_balance))
return defer.succeed(None)
def _announce_startup(): def _announce_startup():
def _wait_for_credits(): def _wait_for_credits():
if float(self.session.wallet.wallet_balance) == 0.0: if float(self.session.wallet.wallet_balance) == 0.0:
@ -330,26 +327,23 @@ class Daemon(AuthJSONRPCServer):
self.looping_call_manager.start(Checker.CONNECTION_PROBLEM, 1) self.looping_call_manager.start(Checker.CONNECTION_PROBLEM, 1)
self.exchange_rate_manager.start() self.exchange_rate_manager.start()
d = defer.Deferred()
if conf.settings.host_ui: if conf.settings.host_ui:
self.lbry_ui_manager.update_checker.start(1800, now=False) self.lbry_ui_manager.update_checker.start(1800, now=False)
d.addCallback(lambda _: self.lbry_ui_manager.setup()) yield self.lbry_ui_manager.setup()
d.addCallback(lambda _: self._initial_setup()) yield self._initial_setup()
d.addCallback(lambda _: threads.deferToThread(self._setup_data_directory)) yield threads.deferToThread(self._setup_data_directory)
d.addCallback(lambda _: self._check_db_migration()) yield self._check_db_migration()
d.addCallback(lambda _: self._load_caches()) yield self._load_caches()
d.addCallback(lambda _: self._set_events()) yield self._set_events()
d.addCallback(lambda _: self._get_session()) yield self._get_session()
d.addCallback(lambda _: self._get_analytics()) yield self._get_analytics()
d.addCallback(lambda _: add_lbry_file_to_sd_identifier(self.sd_identifier)) yield add_lbry_file_to_sd_identifier(self.sd_identifier)
d.addCallback(lambda _: self._setup_stream_identifier()) yield self._setup_stream_identifier()
d.addCallback(lambda _: self._setup_lbry_file_manager()) yield self._setup_lbry_file_manager()
d.addCallback(lambda _: self._setup_query_handlers()) yield self._setup_query_handlers()
d.addCallback(lambda _: self._setup_server()) yield self._setup_server()
d.addCallback(lambda _: _log_starting_vals()) log.info("Starting balance: " + str(self.session.wallet.wallet_balance))
d.addCallback(lambda _: _announce_startup()) yield _announce_startup()
d.callback(None)
return d
def _get_platform(self): def _get_platform(self):
if self.platform is None: if self.platform is None:
@ -756,7 +750,6 @@ class Daemon(AuthJSONRPCServer):
d.addCallback(create_session) d.addCallback(create_session)
d.addCallback(lambda _: self.session.setup()) d.addCallback(lambda _: self.session.setup())
return d return d
def _setup_stream_identifier(self): def _setup_stream_identifier(self):

View file

@ -117,11 +117,7 @@ def update_settings_from_args(args):
settings.update(to_pass) settings.update(to_pass)
def kill(failure, analytics_manager): @defer.inlineCallbacks
analytics_manager.send_server_startup_error(failure.getErrorMessage() + " " + str(failure))
reactor.callFromThread(reactor.stop)
def start_server_and_listen(launchui, use_auth, analytics_manager): def start_server_and_listen(launchui, use_auth, analytics_manager):
"""The primary entry point for launching the daemon. """The primary entry point for launching the daemon.
@ -130,12 +126,16 @@ def start_server_and_listen(launchui, use_auth, analytics_manager):
use_auth: set to true to enable http authentication use_auth: set to true to enable http authentication
analytics_manager: to send analytics analytics_manager: to send analytics
""" """
try:
daemon_server = DaemonServer(analytics_manager) daemon_server = DaemonServer(analytics_manager)
d = daemon_server.start(use_auth) yield daemon_server.start(use_auth)
if launchui: if launchui:
d.addCallback(lambda _: webbrowser.open(settings.UI_ADDRESS)) yield webbrowser.open(settings.UI_ADDRESS)
d.addCallback(lambda _: analytics_manager.send_server_startup_success()) yield analytics_manager.send_server_startup_success()
d.addErrback(log.fail(kill, analytics_manager), 'Failed to startup') except Exception as e:
log.exception('Failed to startup')
analytics_manager.send_server_startup_error(str(e))
reactor.callFromThread(reactor.stop)
if __name__ == "__main__": if __name__ == "__main__":

View file

@ -41,10 +41,10 @@ class DaemonServer(object):
return defer.succeed(True) return defer.succeed(True)
@defer.inlineCallbacks
def start(self, use_auth): def start(self, use_auth):
d = self._setup_server(use_auth) yield self._setup_server(use_auth)
d.addCallback(lambda _: self._api.setup()) yield self._api.setup()
return d
def get_site_base(use_auth, root): def get_site_base(use_auth, root):