From e83e7e7ec7b1a984221ce7396eae73a7d097942e Mon Sep 17 00:00:00 2001 From: Sorin Sbarnea Date: Wed, 7 Apr 2021 13:40:39 +0100 Subject: [PATCH] Retry prepare environment three times before failing (#1517) This should avoid random error caused by galaxy servers returning errors. --- .pre-commit-config.yaml | 2 ++ docs/requirements.txt | 2 ++ setup.cfg | 1 + src/ansiblelint/prerun.py | 9 ++++++++- test-requirements.txt | 2 ++ 5 files changed, 15 insertions(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index f326060357..f66e50ecbc 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -88,6 +88,7 @@ repos: - flaky - pytest - ruamel.yaml + - tenacity - types-PyYAML - wcmatch - yamllint @@ -111,5 +112,6 @@ repos: - pyyaml - rich - ruamel.yaml + - tenacity - sphinx - wcmatch diff --git a/docs/requirements.txt b/docs/requirements.txt index b5441aa187..9e757ed116 100644 --- a/docs/requirements.txt +++ b/docs/requirements.txt @@ -35,6 +35,7 @@ requests==2.25.1 rich==10.1.0 ruamel.yaml.clib==0.2.2 ruamel.yaml==0.17.2 ; python_version >= "3.7" +six==1.15.0 snowballstemmer==2.1.0 sphinx-ansible-theme==0.3.2 sphinx-notfound-page==0.6 @@ -47,6 +48,7 @@ sphinxcontrib-jsmath==1.0.1 sphinxcontrib-programoutput2==2.0a1 sphinxcontrib-qthelp==1.0.3 sphinxcontrib-serializinghtml==1.1.4 +tenacity==7.0.0 typing-extensions==3.7.4.3 urllib3==1.26.4 wcmatch==8.1.2 diff --git a/setup.cfg b/setup.cfg index 2ce06fc515..d461d7b09c 100644 --- a/setup.cfg +++ b/setup.cfg @@ -78,6 +78,7 @@ install_requires = ruamel.yaml >= 0.15.34,<1; python_version < "3.7" ruamel.yaml >= 0.15.37,<1; python_version >= "3.7" # NOTE: per issue #509 0.15.34 included in debian backports + tenacity typing-extensions; python_version < "3.8" wcmatch>=7.0 # MIT diff --git a/src/ansiblelint/prerun.py b/src/ansiblelint/prerun.py index 43c82b6b5f..4650d1e3bc 100644 --- a/src/ansiblelint/prerun.py +++ b/src/ansiblelint/prerun.py @@ -8,6 +8,7 @@ from functools import lru_cache from typing import List, Optional, Tuple +import tenacity from packaging import version from ansiblelint.config import ( @@ -94,6 +95,12 @@ def _get_ver_err() -> Tuple[str, str]: return ver, err +@tenacity.retry( # Retry up to 3 times as galaxy server can return errors + reraise=True, + wait=tenacity.wait_fixed(30), # type: ignore + stop=tenacity.stop_after_attempt(3), # type: ignore + before_sleep=tenacity.after_log(_logger, logging.WARNING), # type: ignore +) def prepare_environment() -> None: """Make dependencies available if needed.""" if not options.configured: @@ -151,7 +158,7 @@ def prepare_environment() -> None: ) if run.returncode != 0: _logger.error(run.stdout) - sys.exit(run.returncode) + raise RuntimeError(run.returncode) _install_galaxy_role() _perform_mockings() diff --git a/test-requirements.txt b/test-requirements.txt index f85b6b59ac..77ab55e052 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -27,6 +27,8 @@ pyyaml==5.4.1 rich==10.1.0 ruamel.yaml.clib==0.2.2 ruamel.yaml==0.17.2 ; python_version >= "3.7" +six==1.15.0 +tenacity==7.0.0 toml==0.10.2 typing-extensions==3.7.4.3 wcmatch==8.1.2