From dd5b76fc18da9d2866029a4ca6f73f5c94328caf Mon Sep 17 00:00:00 2001
From: Ben Morrice <ben.morrice@cern.ch>
Date: Tue, 24 Oct 2023 13:17:28 +0200
Subject: [PATCH] Add logic to fail script if it's taking too long

---
 package_alerts/package_alerts | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/package_alerts/package_alerts b/package_alerts/package_alerts
index 7ab5557..817e06d 100755
--- a/package_alerts/package_alerts
+++ b/package_alerts/package_alerts
@@ -5,9 +5,11 @@ import glob
 import html
 import json
 import re
+import signal
 import subprocess
 import sys
 import tempfile
+import time
 from collections import ChainMap
 from datetime import datetime
 import dnf
@@ -157,7 +159,13 @@ def get_upstream(token, feed):
     offset = 0
     packages = []
     while True:
-        result = call_api(token, url, params={"limit": 100, "offset": offset})
+        signal.signal(signal.SIGALRM, timeout_handler)
+        signal.alarm(30)
+        try:
+            result = call_api(token, url, params={"limit": 100, "offset": offset})
+        except TimeoutError:
+            print(f"Timeout occurred whilst trying to process {feed} with offset {offset}")
+            continue
         try:
             j = json.loads(result.content)
         except json.decoder.JSONDecodeError:
@@ -258,6 +266,10 @@ def format_release(p, f, token):
         )
         print(f"  -- Payload sent: {payload}", flush=True)
 
+def timeout_handler(num, stack):
+    """ function to handle timeouts for long running function """
+    print("Received SIGALRM")
+    raise TimeoutError
 
 if __name__ == "__main__":
     """this is the main class, it's amazing."""
-- 
GitLab