diff --git a/lbrynet/core/log_support.py b/lbrynet/core/log_support.py index 1fb3979c7..88973761f 100644 --- a/lbrynet/core/log_support.py +++ b/lbrynet/core/log_support.py @@ -139,3 +139,17 @@ class JsonFormatter(logging.Formatter): if record.exc_info: data['exc_info'] = self.formatException(record.exc_info) return json.dumps(data) + + +def failure(failure, log, msg, *args): + """Log a failure message from a deferred. + + Args: + failure: twisted.python.failure.Failure + log: a python logger instance + msg: the message to log. Can use normal logging string interpolation. + the last argument will be set to the error message from the failure. + args: values to substitute into `msg` + """ + args += (failure.getErrorMessage(),) + log.error(msg, *args, exc_info=failure.getTracebackObject()) diff --git a/lbrynet/lbrynet_daemon/Daemon.py b/lbrynet/lbrynet_daemon/Daemon.py index fc67a4132..37edc9429 100644 --- a/lbrynet/lbrynet_daemon/Daemon.py +++ b/lbrynet/lbrynet_daemon/Daemon.py @@ -262,6 +262,8 @@ class Daemon(jsonrpc.JSONRPC): self.first_run_after_update = False self.uploaded_temp_files = [] self._session_id = base58.b58encode(generate_id()) + self.analytics_manager = None + self.lbryid = None if os.name == "nt": from lbrynet.winhelpers.knownpaths import get_path, FOLDERID, UserHandle @@ -636,11 +638,8 @@ class Daemon(jsonrpc.JSONRPC): d.addCallback(lambda _: self._setup_server()) d.addCallback(lambda _: _log_starting_vals()) d.addCallback(lambda _: _announce_startup()) - # TODO: handle errors here d.callback(None) - - return defer.succeed(None) - + return d def _get_platform(self): r = { @@ -870,13 +869,19 @@ class Daemon(jsonrpc.JSONRPC): log.info("Closing lbrynet session") log.info("Status at time of shutdown: " + self.startup_status[0]) self.looping_call_manager.shutdown() - self.analytics_manager.shutdown() + if self.analytics_manager: + self.analytics_manager.shutdown() if self.lbry_ui_manager.update_checker.running: self.lbry_ui_manager.update_checker.stop() self._clean_up_temp_files() - d = self._upload_log(log_type="close", exclude_previous=False if self.first_run else True) + try: + d = self._upload_log( + log_type="close", exclude_previous=False if self.first_run else True) + except Exception: + log.warn('Failed to upload log', exc_info=True) + d = defer.succeed(None) d.addCallback(lambda _: self._stop_server()) d.addCallback(lambda _: self._stop_reflector()) d.addErrback(lambda err: True) diff --git a/lbrynet/lbrynet_daemon/DaemonControl.py b/lbrynet/lbrynet_daemon/DaemonControl.py index 3c244b6eb..8ed28c170 100644 --- a/lbrynet/lbrynet_daemon/DaemonControl.py +++ b/lbrynet/lbrynet_daemon/DaemonControl.py @@ -109,6 +109,7 @@ def start(): branch_specified=True if args.branch else False) if args.launchui: d.addCallback(lambda _: webbrowser.open(UI_ADDRESS)) + d.addErrback(log_and_kill) lbrynet_server = server.Site(lbry.root) lbrynet_server.requestFactory = DaemonRequest @@ -123,5 +124,11 @@ def start(): print "Not connected to internet, unable to start" return + +def log_and_kill(failure): + log_support.failure(failure, log, 'Failed to startup: %s') + reactor.stop() + + if __name__ == "__main__": start()