Always retry on connection failure.

This commit is contained in:
Tomas Groth 2014-11-23 20:52:16 +00:00
parent 75960ac8da
commit 0af6dc8eea
2 changed files with 62 additions and 30 deletions

View File

@ -45,7 +45,7 @@ from openlp.core.common import Registry, RegistryProperties, AppLocation, Settin
translate, clean_button_text, trace_error_handler translate, clean_button_text, trace_error_handler
from openlp.core.lib import PluginStatus, build_icon from openlp.core.lib import PluginStatus, build_icon
from openlp.core.lib.ui import critical_error_message_box from openlp.core.lib.ui import critical_error_message_box
from openlp.core.utils import get_web_page from openlp.core.utils import get_web_page, CONNECTION_RETRIES, CONNECTION_TIMEOUT
from .firsttimewizard import UiFirstTimeWizard, FirstTimePage from .firsttimewizard import UiFirstTimeWizard, FirstTimePage
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -304,23 +304,30 @@ class FirstTimeForm(QtGui.QWizard, UiFirstTimeWizard, RegistryProperties):
""" """
block_count = 0 block_count = 0
block_size = 4096 block_size = 4096
try: retries = 0
url_file = urllib.request.urlopen(url, timeout=30) while True:
filename = open(f_path, "wb") try:
# Download until finished or canceled. url_file = urllib.request.urlopen(url, timeout=CONNECTION_TIMEOUT)
while not self.was_download_cancelled: filename = open(f_path, "wb")
data = url_file.read(block_size) # Download until finished or canceled.
if not data: while not self.was_download_cancelled:
break data = url_file.read(block_size)
filename.write(data) if not data:
block_count += 1 break
self._download_progress(block_count, block_size) filename.write(data)
filename.close() block_count += 1
except ConnectionError: self._download_progress(block_count, block_size)
trace_error_handler(log) filename.close()
filename.close() except ConnectionError:
os.remove(f_path) trace_error_handler(log)
return False filename.close()
os.remove(f_path)
if retries > CONNECTION_RETRIES:
return False
else:
retries += 1
continue
break
# Delete file if cancelled, it may be a partial file. # Delete file if cancelled, it may be a partial file.
if self.was_download_cancelled: if self.was_download_cancelled:
os.remove(f_path) os.remove(f_path)
@ -343,9 +350,18 @@ class FirstTimeForm(QtGui.QWizard, UiFirstTimeWizard, RegistryProperties):
:param url: The URL of the file we want to download. :param url: The URL of the file we want to download.
""" """
site = urllib.request.urlopen(url, timeout=30) retries = 0
meta = site.info() while True:
return int(meta.get("Content-Length")) try:
site = urllib.request.urlopen(url, timeout=CONNECTION_TIMEOUT)
meta = site.info()
return int(meta.get("Content-Length"))
except ConnectionException:
if retries > CONNECTION_RETRIES:
raise
else:
retries += 1
continue
def _download_progress(self, count, block_size): def _download_progress(self, count, block_size):
""" """

View File

@ -91,6 +91,8 @@ USER_AGENTS = {
'Mozilla/5.0 (X11; NetBSD amd64; rv:18.0) Gecko/20130120 Firefox/18.0' 'Mozilla/5.0 (X11; NetBSD amd64; rv:18.0) Gecko/20130120 Firefox/18.0'
] ]
} }
CONNECTION_TIMEOUT = 30
CONNECTION_RETRIES = 2
class VersionThread(QtCore.QThread): class VersionThread(QtCore.QThread):
@ -250,10 +252,17 @@ def check_latest_version(current_version):
req = urllib.request.Request('http://www.openlp.org/files/version.txt') req = urllib.request.Request('http://www.openlp.org/files/version.txt')
req.add_header('User-Agent', 'OpenLP/%s' % current_version['full']) req.add_header('User-Agent', 'OpenLP/%s' % current_version['full'])
remote_version = None remote_version = None
try: retries = 0
remote_version = str(urllib.request.urlopen(req, None).read().decode()).strip() while True:
except IOError: try:
log.exception('Failed to download the latest OpenLP version file') remote_version = str(urllib.request.urlopen(req, None, timeout=CONNECTION_TIMEOUT).read().decode()).strip()
except ConnectionException:
if retries > CONNECTION_RETRIES:
log.exception('Failed to download the latest OpenLP version file')
else:
retries += 1
continue
break
if remote_version: if remote_version:
version_string = remote_version version_string = remote_version
return version_string return version_string
@ -389,11 +398,18 @@ def get_web_page(url, header=None, update_openlp=False):
req.add_header(header[0], header[1]) req.add_header(header[0], header[1])
page = None page = None
log.debug('Downloading URL = %s' % url) log.debug('Downloading URL = %s' % url)
try: retries = 0
page = urllib.request.urlopen(req, timeout=30) while True:
log.debug('Downloaded URL = %s' % page.geturl()) try:
except (urllib.error.URLError, ConnectionError): page = urllib.request.urlopen(req, timeout=CONNECTION_TIMEOUT)
log.exception('The web page could not be downloaded') log.debug('Downloaded URL = %s' % page.geturl())
except (urllib.error.URLError, ConnectionError):
if retries > CONNECTION_RETRIES:
log.exception('The web page could not be downloaded')
raise
else:
continue
break
if not page: if not page:
return None return None
if update_openlp: if update_openlp: