Skip to content

Commit 96c7b81

Browse files
authored
bot: Replace retry function with Tenacity (#366)
Fixes #110
1 parent e621d18 commit 96c7b81

File tree

6 files changed

+16
-36
lines changed

6 files changed

+16
-36
lines changed

.isort.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[settings]
22
known_first_party = code_coverage_backend,code_coverage_bot,code_coverage_events,code_coverage_tools,conftest,firefox_code_coverage
3-
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
3+
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
44
force_single_line = True
55
default_section=FIRSTPARTY
66
line_length=159

bot/code_coverage_bot/taskcluster.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77
import requests
88
import structlog
99
import taskcluster
10+
import tenacity
1011
from taskcluster.helper import TaskclusterConfig
1112

12-
from code_coverage_bot.utils import retry
13-
1413
logger = structlog.getLogger(__name__)
1514
taskcluster_config = TaskclusterConfig("https://firefox-ci-tc.services.mozilla.com")
1615

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

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

86-
retry(perform_download)
88+
perform_download()
8789

8890

8991
def is_coverage_task(task):

bot/code_coverage_bot/uploader.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
import requests
66
import structlog
7+
import tenacity
78
import zstandard as zstd
89

910
from code_coverage_bot.secrets import secrets
10-
from code_coverage_bot.utils import retry
1111
from code_coverage_tools.gcp import get_bucket
1212

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

4747
# Trigger ingestion on backend
48-
retry(
49-
lambda: gcp_ingest(repository, revision, platform, suite),
50-
retries=10,
51-
wait_between_retries=60,
52-
)
48+
gcp_ingest(repository, revision, platform, suite)
5349

5450
return blob
5551

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

6864

65+
@tenacity.retry(
66+
stop=tenacity.stop_after_attempt(10), wait=tenacity.wait_fixed(60), reraise=True
67+
)
6968
def gcp_ingest(repository, revision, platform, suite):
7069
"""
7170
The GCP report ingestion is triggered remotely on a backend

bot/code_coverage_bot/utils.py

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,12 @@
44
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
55
import concurrent.futures
66
import subprocess
7-
import time
87

98
import structlog
109

1110
log = structlog.get_logger(__name__)
1211

1312

14-
class RunException(Exception):
15-
"""
16-
Exception used to stop retrying
17-
"""
18-
19-
20-
def retry(
21-
operation, retries=5, wait_between_retries=30, exception_to_break=RunException
22-
):
23-
while True:
24-
try:
25-
return operation()
26-
except Exception as e:
27-
if isinstance(e, exception_to_break):
28-
raise
29-
retries -= 1
30-
if retries == 0:
31-
raise
32-
time.sleep(wait_between_retries)
33-
34-
3513
def hide_secrets(text, secrets):
3614
if type(text) is bytes:
3715
encode_secret, xxx = lambda x: bytes(x, encoding="utf-8"), b"XXX"
@@ -54,7 +32,7 @@ def run_check(command, **kwargs):
5432
assert isinstance(command, list)
5533

5634
if len(command) == 0:
57-
raise RunException("Can't run an empty command.")
35+
raise Exception("Can't run an empty command.")
5836

5937
_kwargs = dict(
6038
stdin=subprocess.DEVNULL, # no interactions
@@ -81,7 +59,7 @@ def run_check(command, **kwargs):
8159
error=error,
8260
)
8361

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

8664
return output
8765

bot/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ google-cloud-storage==1.25.0
33
libmozdata==0.1.64
44
pytoml==0.1.21
55
pytz==2019.3
6+
tenacity==6.0.0
67
zstandard==0.13.0

bot/tests/test_taskcluster.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ def test_download_artifact_forbidden(mocked_sleep, mock_taskcluster, tmpdir):
275275
"public/test_info/code-coverage-grcov.zip",
276276
)
277277

278-
assert mocked_sleep.call_count == 4
278+
assert len(responses.calls) == 5
279279

280280

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

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

0 commit comments

Comments
 (0)