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
from openlp.core.lib import PluginStatus, build_icon
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
log = logging.getLogger(__name__)
@ -304,23 +304,30 @@ class FirstTimeForm(QtGui.QWizard, UiFirstTimeWizard, RegistryProperties):
"""
block_count = 0
block_size = 4096
try:
url_file = urllib.request.urlopen(url, timeout=30)
filename = open(f_path, "wb")
# Download until finished or canceled.
while not self.was_download_cancelled:
data = url_file.read(block_size)
if not data:
break
filename.write(data)
block_count += 1
self._download_progress(block_count, block_size)
filename.close()
except ConnectionError:
trace_error_handler(log)
filename.close()
os.remove(f_path)
return False
retries = 0
while True:
try:
url_file = urllib.request.urlopen(url, timeout=CONNECTION_TIMEOUT)
filename = open(f_path, "wb")
# Download until finished or canceled.
while not self.was_download_cancelled:
data = url_file.read(block_size)
if not data:
break
filename.write(data)
block_count += 1
self._download_progress(block_count, block_size)
filename.close()
except ConnectionError:
trace_error_handler(log)
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.
if self.was_download_cancelled:
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.
"""
site = urllib.request.urlopen(url, timeout=30)
meta = site.info()
return int(meta.get("Content-Length"))
retries = 0
while True:
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):
"""

View File

@ -91,6 +91,8 @@ USER_AGENTS = {
'Mozilla/5.0 (X11; NetBSD amd64; rv:18.0) Gecko/20130120 Firefox/18.0'
]
}
CONNECTION_TIMEOUT = 30
CONNECTION_RETRIES = 2
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.add_header('User-Agent', 'OpenLP/%s' % current_version['full'])
remote_version = None
try:
remote_version = str(urllib.request.urlopen(req, None).read().decode()).strip()
except IOError:
log.exception('Failed to download the latest OpenLP version file')
retries = 0
while True:
try:
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:
version_string = remote_version
return version_string
@ -389,11 +398,18 @@ def get_web_page(url, header=None, update_openlp=False):
req.add_header(header[0], header[1])
page = None
log.debug('Downloading URL = %s' % url)
try:
page = urllib.request.urlopen(req, timeout=30)
log.debug('Downloaded URL = %s' % page.geturl())
except (urllib.error.URLError, ConnectionError):
log.exception('The web page could not be downloaded')
retries = 0
while True:
try:
page = urllib.request.urlopen(req, timeout=CONNECTION_TIMEOUT)
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:
return None
if update_openlp: