retry the get call three times before failing

This commit is contained in:
jobevers 2017-01-26 14:09:54 -06:00
parent 854dea797e
commit 3dc1a523cf
2 changed files with 39 additions and 26 deletions

View file

@ -1550,32 +1550,40 @@ class Daemon(AuthJSONRPCServer):
download_id = utils.random_string() download_id = utils.random_string()
self.analytics_manager.send_download_started(download_id, name, stream_info) self.analytics_manager.send_download_started(download_id, name, stream_info)
try: tries = 1
sd_hash, file_path = yield self._download_name( max_tries = 3
name=params.name, while tries <= max_tries:
timeout=params.timeout, try:
download_directory=params.download_directory, log.info(
stream_info=params.stream_info, 'Making try %s / %s to start download of %s', tries, max_tries, params.name)
file_name=params.file_name, sd_hash, file_path = yield self._download_name(
wait_for_write=params.wait_for_write name=params.name,
) timeout=params.timeout,
except Exception as e: download_directory=params.download_directory,
self.analytics_manager.send_download_errored(download_id, name, stream_info) stream_info=params.stream_info,
log.exception('Failed to get %s', params.name) file_name=params.file_name,
response = yield self._render_response(str(e)) wait_for_write=params.wait_for_write
else:
# TODO: should stream_hash key be changed to sd_hash?
message = {
'stream_hash': params.sd_hash if params.stream_info else sd_hash,
'path': file_path
}
stream = self.streams.get(name)
if stream:
stream.downloader.finished_deferred.addCallback(
lambda _: self.analytics_manager.send_download_finished(
download_id, name, stream_info)
) )
response = yield self._render_response(message) break
except Exception as e:
log.exception('Failed to get %s', params.name)
if tries == max_tries:
self.analytics_manager.send_download_errored(download_id, name, stream_info)
response = yield self._render_response(str(e))
defer.returnValue(response)
tries += 1
# TODO: should stream_hash key be changed to sd_hash?
message = {
'stream_hash': params.sd_hash if params.stream_info else sd_hash,
'path': file_path
}
stream = self.streams.get(name)
if stream:
stream.downloader.finished_deferred.addCallback(
lambda _: self.analytics_manager.send_download_finished(
download_id, name, stream_info)
)
response = yield self._render_response(message)
defer.returnValue(response) defer.returnValue(response)
@AuthJSONRPCServer.auth_required @AuthJSONRPCServer.auth_required

View file

@ -72,7 +72,7 @@ class GetStream(object):
self.finished.callback((True, self.sd_hash, self.download_path)) self.finished.callback((True, self.sd_hash, self.download_path))
elif self.timeout_counter >= self.timeout: elif self.timeout_counter >= self.timeout:
log.info("Timeout downloading lbry://%s" % self.resolved_name) log.info("Timeout downloading lbry://%s", self.resolved_name)
self.checker.stop() self.checker.stop()
self._d.cancel() self._d.cancel()
self.code = STREAM_STAGES[4] self.code = STREAM_STAGES[4]
@ -86,6 +86,11 @@ class GetStream(object):
def start(self, stream_info, name): def start(self, stream_info, name):
def _cancel(err): def _cancel(err):
# this callback sequence gets cancelled in check_status if
# it takes too long when that happens, we want the logic
# to live in check_status
if err.check(defer.CancelledError):
return
if self.checker: if self.checker:
self.checker.stop() self.checker.stop()
self.finished.errback(err) self.finished.errback(err)