Skip to content

bot: replaced retry function with Tenacity #366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
Jan 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
3b22de0
Updated taskcluster.py to use Tenacity for retrying perform_download.…
seb-sojka Jan 6, 2020
ff03947
Uploader.py in bot uses tenacity to retrying gcp_ingest in gcp functi…
seb-sojka Jan 6, 2020
5da78f3
Merge branch 'master' into removeRetryFunction
seb-sojka Jan 7, 2020
45cd5bb
Remove extra : from retry in taskcluster.py
seb-sojka Jan 7, 2020
77b8b91
Add tenacity to requirementspdev.txt to intall
seb-sojka Jan 7, 2020
de3b91b
Add tenacity to requirements.txt for install and removed it from requ…
seb-sojka Jan 7, 2020
55113e8
Updated taskcluster.py and uploader.py to run tenacity
seb-sojka Jan 9, 2020
5e4b422
Add call for perform_download function
seb-sojka Jan 9, 2020
a825a35
Corrected error with import tenacity instead of from
seb-sojka Jan 9, 2020
ce6d80c
Merge branch 'master' into removeRetryFunction
seb-sojka Jan 10, 2020
f6883ba
Updated uploader.py to use tenacity functions
seb-sojka Jan 11, 2020
77a4eb0
Tabs to spaces and added tenacity to know third party
seb-sojka Jan 11, 2020
2e64b0d
Update requirements.txt and requirements-dev.txt with sorting. Update…
seb-sojka Jan 11, 2020
2a6ea47
Added back class RunException to utils.py and removed import time
seb-sojka Jan 11, 2020
e895b12
Moved perform download function outside of download artifact
seb-sojka Jan 11, 2020
1fbd552
Added agruements from urls and artifact path for perform download
seb-sojka Jan 11, 2020
da866ce
Moved perform download back into download_artifact function
seb-sojka Jan 11, 2020
ec20dea
Removed extra perform_download function and set reraise to try for re…
seb-sojka Jan 11, 2020
efbe2e9
Remove the function RaiseExpection and used expection instead in util…
seb-sojka Jan 14, 2020
3739f47
Merge branch 'master' into removeRetryFunction
seb-sojka Jan 15, 2020
744e114
Added back # noqa for lines too long in test_taskcluster.py
seb-sojka Jan 15, 2020
6e697bb
Merge branch 'removeRetryFunction' of github.com:seb-sojka/code-cover…
seb-sojka Jan 15, 2020
fe38b22
Merge branch 'master' into removeRetryFunction
seb-sojka Jan 28, 2020
bdb6464
Pre-commit changed retry style
seb-sojka Jan 28, 2020
acc1076
Merge branch 'master' into removeRetryFunction
seb-sojka Jan 28, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .isort.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[settings]
known_first_party = code_coverage_backend,code_coverage_bot,code_coverage_events,code_coverage_tools,conftest,firefox_code_coverage
known_third_party = connexion,datadog,dateutil,fakeredis,flask,flask_cors,flask_talisman,google,hglib,jsone,jsonschema,libmozdata,libmozevent,logbook,pytest,pytz,raven,redis,requests,responses,setuptools,structlog,taskcluster,werkzeug,zstandard
known_third_party = connexion,datadog,dateutil,fakeredis,flask,flask_cors,flask_talisman,google,hglib,jsone,jsonschema,libmozdata,libmozevent,logbook,pytest,pytz,raven,redis,requests,responses,setuptools,structlog,taskcluster,tenacity,werkzeug,zstandard
force_single_line = True
default_section=FIRSTPARTY
line_length=159
8 changes: 5 additions & 3 deletions bot/code_coverage_bot/taskcluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
import requests
import structlog
import taskcluster
import tenacity
from taskcluster.helper import TaskclusterConfig

from code_coverage_bot.utils import retry

logger = structlog.getLogger(__name__)
taskcluster_config = TaskclusterConfig("https://firefox-ci-tc.services.mozilla.com")

Expand Down Expand Up @@ -72,6 +71,9 @@ def download_artifact(artifact_path, task_id, artifact_name):
url = queue.buildUrl("getLatestArtifact", task_id, artifact_name)
logger.debug("Downloading artifact", url=url)

@tenacity.retry(
reraise=True, wait=tenacity.wait_fixed(30), stop=tenacity.stop_after_attempt(5)
)
def perform_download():
r = requests.get(url, stream=True)
r.raise_for_status()
Expand All @@ -83,7 +85,7 @@ def perform_download():
if artifact_path.endswith(".zip") and not is_zipfile(artifact_path):
raise BadZipFile("File is not a zip file")

retry(perform_download)
perform_download()


def is_coverage_task(task):
Expand Down
11 changes: 5 additions & 6 deletions bot/code_coverage_bot/uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

import requests
import structlog
import tenacity
import zstandard as zstd

from code_coverage_bot.secrets import secrets
from code_coverage_bot.utils import retry
from code_coverage_tools.gcp import get_bucket

logger = structlog.get_logger(__name__)
Expand Down Expand Up @@ -45,11 +45,7 @@ def gcp(repository, revision, report, platform, suite):
logger.info("Uploaded {} on {}".format(path, bucket))

# Trigger ingestion on backend
retry(
lambda: gcp_ingest(repository, revision, platform, suite),
retries=10,
wait_between_retries=60,
)
gcp_ingest(repository, revision, platform, suite)

return blob

Expand All @@ -66,6 +62,9 @@ def gcp_covdir_exists(repository, revision, platform, suite):
return blob.exists()


@tenacity.retry(
stop=tenacity.stop_after_attempt(10), wait=tenacity.wait_fixed(60), reraise=True
)
def gcp_ingest(repository, revision, platform, suite):
"""
The GCP report ingestion is triggered remotely on a backend
Expand Down
26 changes: 2 additions & 24 deletions bot/code_coverage_bot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,12 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import concurrent.futures
import subprocess
import time

import structlog

log = structlog.get_logger(__name__)


class RunException(Exception):
"""
Exception used to stop retrying
"""


def retry(
operation, retries=5, wait_between_retries=30, exception_to_break=RunException
):
while True:
try:
return operation()
except Exception as e:
if isinstance(e, exception_to_break):
raise
retries -= 1
if retries == 0:
raise
time.sleep(wait_between_retries)


def hide_secrets(text, secrets):
if type(text) is bytes:
encode_secret, xxx = lambda x: bytes(x, encoding="utf-8"), b"XXX"
Expand All @@ -54,7 +32,7 @@ def run_check(command, **kwargs):
assert isinstance(command, list)

if len(command) == 0:
raise RunException("Can't run an empty command.")
raise Exception("Can't run an empty command.")

_kwargs = dict(
stdin=subprocess.DEVNULL, # no interactions
Expand All @@ -81,7 +59,7 @@ def run_check(command, **kwargs):
error=error,
)

raise RunException(f"`{command[0]}` failed with code: {proc.returncode}.")
raise Exception(f"`{command[0]}` failed with code: {proc.returncode}.")

return output

Expand Down
1 change: 1 addition & 0 deletions bot/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ google-cloud-storage==1.25.0
libmozdata==0.1.64
pytoml==0.1.21
pytz==2019.3
tenacity==6.0.0
zstandard==0.13.0
4 changes: 2 additions & 2 deletions bot/tests/test_taskcluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ def test_download_artifact_forbidden(mocked_sleep, mock_taskcluster, tmpdir):
"public/test_info/code-coverage-grcov.zip",
)

assert mocked_sleep.call_count == 4
assert len(responses.calls) == 5


@mock.patch("time.sleep")
Expand All @@ -295,4 +295,4 @@ def test_download_artifact_badzip(mocked_sleep, mock_taskcluster, tmpdir):
"public/test_info/code-coverage-grcov.zip",
)

assert mocked_sleep.call_count == 4
assert len(responses.calls) == 5