From 8c38531743b13a0162641a4741ff27894fe866d5 Mon Sep 17 00:00:00 2001
From: Ben Morrice <ben.morrice@cern.ch>
Date: Thu, 12 Oct 2023 11:23:00 +0200
Subject: [PATCH] Add retry logic to get_changelog function

---
 package_alerts/package_alerts | 38 ++++++++++++++++++++++++++---------
 1 file changed, 28 insertions(+), 10 deletions(-)

diff --git a/package_alerts/package_alerts b/package_alerts/package_alerts
index 60e5901..a1af9f5 100755
--- a/package_alerts/package_alerts
+++ b/package_alerts/package_alerts
@@ -82,17 +82,35 @@ def do_execute(cmd, tdir="/tmp", return_output=False, return_error=False):
 
 def get_changelog(token, checksum):
     """get the changelog for a rpm, which requires downloading it"""
-    url = f"https://api.access.redhat.com/management/v1/packages/{checksum}/download"
-    result = call_api(token, url, stream=True)
+    # We don't usually retry stuff from api calls as the script runs
+    # frequently, however - in this case a failure results in a blank
+    # changelog which is not very useful
+    attempts = 0
+    max_attempts = 3
+    rpm_downloaded = False
     temp_file = generate_temp_file("checksum")
-    # TODO: try and do this with dnf 'add_remote_rpms', although we would need to auth
-    with open(temp_file, "wb") as f:
-        try:
-            for chunk in result.iter_content(chunk_size=1024):
-                if chunk:
-                    f.write(chunk)
-        except:
-            f.close()
+    url = f"https://api.access.redhat.com/management/v1/packages/{checksum}/download"
+    while attempts < max_attempts:
+        attempts += 1
+        result = call_api(token, url, stream=True)
+        if result.status_code == 200:
+            # TODO: try and do this with dnf 'add_remote_rpms', although we would need to auth
+            with open(temp_file, "wb") as f:
+                try:
+                    for chunk in result.iter_content(chunk_size=1024):
+                        if chunk:
+                             f.write(chunk)
+                    rpm_downloaded = True
+                except:
+                    print(f"Saving changelog appeared to have failed. Retrying {attempts}/{max_attempts}", flush=True)
+                    f.close()
+            if rpm_downloaded:
+                break
+        else:
+            print(f"API call failed, Retrying {attempts}/{max_attempts}", flush=True)
+    if not rpm_downloaded:
+        print(f"Changelog failed to download, exiting", flush=True)
+        sys.exit(1)
     # TODO: maybe have some better logic instead of the head command
     changelog = do_execute(f"rpm -qp {temp_file} --changelog | head -20", return_output=True)
     # Even though we are running through a container, let's clean up after ourselves
-- 
GitLab