Docker build: Retry on communication errors

This commit is contained in:
Bernhard Ehlers 2023-08-11 09:29:26 +02:00
parent 1143916e2a
commit 59e1b1787c

View File

@ -61,6 +61,7 @@ import json
import re import re
import shlex import shlex
import subprocess import subprocess
import time
import dxf import dxf
import requests.exceptions import requests.exceptions
import dateutil.parser import dateutil.parser
@ -132,6 +133,23 @@ def docker_auth(docker, response):
response=response) response=response)
def retry(tries, func, *func_args, **func_kwargs):
""" retry requests based function """
while True:
tries -= 1
try:
return func(*func_args, **func_kwargs)
except (requests.exceptions.ConnectionError,
requests.exceptions.Timeout):
if tries <= 0:
raise
except requests.exceptions.HTTPError as err:
if tries <= 0 or err.response.status_code not in \
(429, 500, 502, 503, 504):
raise
time.sleep(3)
def get_time_layers(repository): def get_time_layers(repository):
""" """
get created time and layer info from the docker registry get created time and layer info from the docker registry
@ -148,16 +166,16 @@ def get_time_layers(repository):
docker.registry_auth = docker_login.get(registry, [None, None]) docker.registry_auth = docker_login.get(registry, [None, None])
# get config digest # get config digest
try: try:
digest = docker.get_digest(tag, platform="linux/amd64") digest = retry(3, docker.get_digest, tag, platform="linux/amd64")
except dxf.exceptions.DXFUnauthorizedError: except dxf.exceptions.DXFUnauthorizedError:
return None return None
except requests.exceptions.HTTPError as err: except requests.exceptions.HTTPError as err:
if err.response.status_code not in (401, 403, 404): if err.response.status_code in (401, 403, 404):
raise return None
return None raise
# get config: pull_blob(digest) # get config: pull_blob(digest)
data = json.loads(b''.join(docker.pull_blob(digest))) data = json.loads(b''.join(retry(3, docker.pull_blob, digest)))
return {"created": dateutil.parser.parse(data["created"]), return {"created": dateutil.parser.parse(data["created"]),
"layers": data["rootfs"]["diff_ids"]} "layers": data["rootfs"]["diff_ids"]}