jenkins-bot has submitted this change and it was merged. (
https://gerrit.wikimedia.org/r/351241 )
Change subject: getInternetArchiveURL: Retry http.fetch if there is a ConnectionError
......................................................................
getInternetArchiveURL: Retry http.fetch if there is a ConnectionError
The server sometimes returns an invalid response. Retrying should mitigate
the issue.
Skip the related test on ConnectionError. `max_retries` is reduced to 1
during tests and it might not be enough.
Bug: T164208
Change-Id: I19e287bacb5f6165ed50d8cb82e92b7880347066
---
M pywikibot/weblib.py
M tests/weblib_tests.py
2 files changed, 24 insertions(+), 2 deletions(-)
Approvals:
jenkins-bot: Verified
Xqt: Looks good to me, approved
diff --git a/pywikibot/weblib.py b/pywikibot/weblib.py
index 4c27358..136fc22 100644
--- a/pywikibot/weblib.py
+++ b/pywikibot/weblib.py
@@ -11,6 +11,7 @@
import json
import sys
+from time import sleep
import xml.etree.ElementTree as ET
if sys.version_info[0] > 2:
@@ -18,7 +19,10 @@
else:
from urllib import urlencode
+from requests.exceptions import ConnectionError as RequestsConnectionError
+
from pywikibot.comms import http
+from pywikibot import config2
from pywikibot.tools import deprecated
@@ -42,7 +46,19 @@
query['timestamp'] = timestamp
uri = uri + urlencode(query)
- jsontext = http.fetch(uri).content
+
+ retry_count = 0
+ while retry_count <= config2.max_retries:
+ try:
+ jsontext = http.fetch(uri).content
+ break
+ except RequestsConnectionError as e:
+ error = e
+ retry_count += 1
+ sleep(config2.retry_wait)
+ else:
+ raise error
+
if "closest" in jsontext:
data = json.loads(jsontext)
return data['archived_snapshots']['closest']['url']
diff --git a/tests/weblib_tests.py b/tests/weblib_tests.py
index 33f88bb..e4b5414 100644
--- a/tests/weblib_tests.py
+++ b/tests/weblib_tests.py
@@ -7,6 +7,8 @@
#
from __future__ import absolute_import, unicode_literals
+from requests.exceptions import ConnectionError as RequestsConnectionError
+
from pywikibot.tools import PY2
if not PY2:
@@ -38,7 +40,11 @@
def _get_archive_url(self, url, date_string=None):
with PatchedHttp(weblib, False) as p:
p.after_fetch = self._test_response
- archivedversion = weblib.getInternetArchiveURL(url, date_string)
+ try:
+ archivedversion = weblib.getInternetArchiveURL(
+ url, date_string)
+ except RequestsConnectionError as e:
+ self.skipTest(e)
self.assertOneDeprecation()
return archivedversion
--
To view, visit
https://gerrit.wikimedia.org/r/351241
To unsubscribe, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I19e287bacb5f6165ed50d8cb82e92b7880347066
Gerrit-PatchSet: 3
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Dalba <dalba.wiki(a)gmail.com>
Gerrit-Reviewer: Dalba <dalba.wiki(a)gmail.com>
Gerrit-Reviewer: Magul <tomasz.magulski(a)gmail.com>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot <>