From 49e57dc334dabd3290ad75047c216f5e4cad9723 Mon Sep 17 00:00:00 2001 From: Rebecca Graber Date: Mon, 11 Dec 2023 09:21:51 -0500 Subject: [PATCH] feat: add retry when fetching current image --- tubular/scripts/retrieve_latest_base_ami.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tubular/scripts/retrieve_latest_base_ami.py b/tubular/scripts/retrieve_latest_base_ami.py index 07fe88a3..69a33823 100755 --- a/tubular/scripts/retrieve_latest_base_ami.py +++ b/tubular/scripts/retrieve_latest_base_ami.py @@ -80,7 +80,7 @@ def retrieve_latest_base_ami(environment, deployment, play, override, ubuntu_ver if url == "": url = "https://cloud-images.ubuntu.com/query/focal/server/released.current.txt" click.secho('Using default focal images.\n: {}'.format(url), fg='red') - data = requests.get(url) + data = get_with_retry(url) parse_ami = re.findall('ebs-ssd(.+?)amd64(.+?){}(.+?)hvm'.format(region), data.content.decode('utf-8')) ami_id = parse_ami[0][2].strip() click.secho('AMI ID fetched from Ubuntu Cloud : {}'.format(ami_id), fg='red') @@ -110,6 +110,14 @@ def retrieve_latest_base_ami(environment, deployment, play, override, ubuntu_ver sys.exit(0) +@backoff.on_exception( + backoff.expo, + requests.exceptions.RequestException, + max_tries=5, jitter=backoff.random_jitter, on_backoff=_backoff_logger, +) +def get_with_retry(url): + data = requests.get(url) + return data if __name__ == "__main__": retrieve_latest_base_ami() # pylint: disable=no-value-for-parameter