Implement retrying mechanism for get results from PlatformIO API

This commit is contained in:
Ivan Kravets
2016-09-01 16:05:02 +03:00
parent 838063b1b7
commit ac4c054d1f
4 changed files with 34 additions and 23 deletions

View File

@ -89,8 +89,8 @@ it), please use ``--lib="."`` option for :ref:`cmd_ci` command
script: script:
- platformio ci --lib="." --board=ID_1 --board=ID_2 --board=ID_N - platformio ci --lib="." --board=ID_1 --board=ID_2 --board=ID_N
Library dependecies Library dependencies
~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~
There 2 options to test source code with dependent libraries: There 2 options to test source code with dependent libraries:

View File

@ -14,7 +14,7 @@
import sys import sys
VERSION = (3, 0, "0b6") VERSION = (3, 0, "0b7")
__version__ = ".".join([str(s) for s in VERSION]) __version__ = ".".join([str(s) for s in VERSION])
__title__ = "platformio" __title__ = "platformio"
@ -31,7 +31,6 @@ __license__ = "Apache Software License"
__copyright__ = "Copyright 2014-present PlatformIO" __copyright__ = "Copyright 2014-present PlatformIO"
__apiurl__ = "https://api.platformio.org" __apiurl__ = "https://api.platformio.org"
__apiip__ = "198.7.57.247"
if sys.version_info < (2, 7, 0) or sys.version_info >= (3, 0, 0): if sys.version_info < (2, 7, 0) or sys.version_info >= (3, 0, 0):
msg = ("PlatformIO version %s does not run under Python version %s.\n" msg = ("PlatformIO version %s does not run under Python version %s.\n"

View File

@ -26,8 +26,12 @@ from os.path import (abspath, basename, dirname, expanduser, isdir, isfile,
from platform import system, uname from platform import system, uname
from shutil import rmtree from shutil import rmtree
from threading import Thread from threading import Thread
from time import sleep
from platformio import __apiip__, __apiurl__, __version__, exception import click
import requests
from platformio import __apiurl__, __version__, exception
# pylint: disable=wrong-import-order # pylint: disable=wrong-import-order
try: try:
@ -357,20 +361,19 @@ def get_logicaldisks():
def get_request_defheaders(): def get_request_defheaders():
import requests
data = (__version__, int(is_ci()), requests.utils.default_user_agent()) data = (__version__, int(is_ci()), requests.utils.default_user_agent())
return {"User-Agent": "PlatformIO/%s CI/%d %s" % data} return {"User-Agent": "PlatformIO/%s CI/%d %s" % data}
@memoized @memoized
def _api_request_session(): def _api_request_session():
import requests
return requests.Session() return requests.Session()
def get_api_result(path, # pylint: disable=too-many-branches def _get_api_result(
params=None, data=None, skipdns=False): path, # pylint: disable=too-many-branches
import requests params=None,
data=None):
from platformio.app import get_setting from platformio.app import get_setting
result = None result = None
@ -378,9 +381,6 @@ def get_api_result(path, # pylint: disable=too-many-branches
headers = get_request_defheaders() headers = get_request_defheaders()
url = __apiurl__ url = __apiurl__
if skipdns:
url = "https://%s" % __apiip__
headers['host'] = __apiurl__[__apiurl__.index("://") + 3:]
if get_setting("disable_ssl"): if get_setting("disable_ssl"):
url = url.replace("https://", "http://") url = url.replace("https://", "http://")
@ -400,14 +400,6 @@ def get_api_result(path, # pylint: disable=too-many-branches
raise exception.APIRequestError(result['errors'][0]['title']) raise exception.APIRequestError(result['errors'][0]['title'])
else: else:
raise exception.APIRequestError(e) raise exception.APIRequestError(e)
except (requests.exceptions.ConnectionError,
requests.exceptions.ConnectTimeout,
requests.exceptions.ReadTimeout):
if not skipdns:
return get_api_result(path, params, data, skipdns=True)
raise exception.APIRequestError(
"Could not connect to PlatformIO Registry Service. "
"Please try later.")
except ValueError: except ValueError:
raise exception.APIRequestError("Invalid response: %s" % raise exception.APIRequestError("Invalid response: %s" %
r.text.encode("utf-8")) r.text.encode("utf-8"))
@ -417,6 +409,26 @@ def get_api_result(path, # pylint: disable=too-many-branches
return result return result
def get_api_result(path, params=None, data=None):
max_retries = 5
total = 0
while total < max_retries:
try:
return _get_api_result(path, params, data)
except (requests.exceptions.ConnectionError,
requests.exceptions.Timeout) as e:
total += 1
click.secho(
"[API] ConnectionError: {0} (incremented retry: max={1}, "
"total={2})".format(e, max_retries, total),
fg="yellow")
sleep(2 * total)
raise exception.APIRequestError(
"Could not connect to PlatformIO Registry Service. "
"Please try later.")
@memoized @memoized
def _lookup_frameworks(): def _lookup_frameworks():
frameworks = {} frameworks = {}

View File

@ -117,8 +117,8 @@ def test_init_special_board(clirunner, validate_cliresult):
def test_init_enable_auto_uploading(clirunner, validate_cliresult): def test_init_enable_auto_uploading(clirunner, validate_cliresult):
with clirunner.isolated_filesystem(): with clirunner.isolated_filesystem():
result = clirunner.invoke(cmd_init, result = clirunner.invoke(
["-b", "uno", "--enable-auto-uploading"]) cmd_init, ["-b", "uno", "--project-option", "targets=upload"])
validate_cliresult(result) validate_cliresult(result)
validate_pioproject(getcwd()) validate_pioproject(getcwd())
config = util.load_project_config() config = util.load_project_config()