forked from openlp/openlp
Always retry on connection failure.
This commit is contained in:
parent
75960ac8da
commit
0af6dc8eea
@ -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,8 +304,10 @@ class FirstTimeForm(QtGui.QWizard, UiFirstTimeWizard, RegistryProperties):
|
|||||||
"""
|
"""
|
||||||
block_count = 0
|
block_count = 0
|
||||||
block_size = 4096
|
block_size = 4096
|
||||||
|
retries = 0
|
||||||
|
while True:
|
||||||
try:
|
try:
|
||||||
url_file = urllib.request.urlopen(url, timeout=30)
|
url_file = urllib.request.urlopen(url, timeout=CONNECTION_TIMEOUT)
|
||||||
filename = open(f_path, "wb")
|
filename = open(f_path, "wb")
|
||||||
# Download until finished or canceled.
|
# Download until finished or canceled.
|
||||||
while not self.was_download_cancelled:
|
while not self.was_download_cancelled:
|
||||||
@ -320,7 +322,12 @@ class FirstTimeForm(QtGui.QWizard, UiFirstTimeWizard, RegistryProperties):
|
|||||||
trace_error_handler(log)
|
trace_error_handler(log)
|
||||||
filename.close()
|
filename.close()
|
||||||
os.remove(f_path)
|
os.remove(f_path)
|
||||||
|
if retries > CONNECTION_RETRIES:
|
||||||
return False
|
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
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
site = urllib.request.urlopen(url, timeout=CONNECTION_TIMEOUT)
|
||||||
meta = site.info()
|
meta = site.info()
|
||||||
return int(meta.get("Content-Length"))
|
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):
|
||||||
"""
|
"""
|
||||||
|
@ -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
|
||||||
|
retries = 0
|
||||||
|
while True:
|
||||||
try:
|
try:
|
||||||
remote_version = str(urllib.request.urlopen(req, None).read().decode()).strip()
|
remote_version = str(urllib.request.urlopen(req, None, timeout=CONNECTION_TIMEOUT).read().decode()).strip()
|
||||||
except IOError:
|
except ConnectionException:
|
||||||
|
if retries > CONNECTION_RETRIES:
|
||||||
log.exception('Failed to download the latest OpenLP version file')
|
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)
|
||||||
|
retries = 0
|
||||||
|
while True:
|
||||||
try:
|
try:
|
||||||
page = urllib.request.urlopen(req, timeout=30)
|
page = urllib.request.urlopen(req, timeout=CONNECTION_TIMEOUT)
|
||||||
log.debug('Downloaded URL = %s' % page.geturl())
|
log.debug('Downloaded URL = %s' % page.geturl())
|
||||||
except (urllib.error.URLError, ConnectionError):
|
except (urllib.error.URLError, ConnectionError):
|
||||||
|
if retries > CONNECTION_RETRIES:
|
||||||
log.exception('The web page could not be downloaded')
|
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:
|
||||||
|
Loading…
Reference in New Issue
Block a user