← Back to team overview

canonical-ubuntu-qa team mailing list archive

[Merge] ~andersson123/autopkgtest-cloud:cache-amqp-fix into autopkgtest-cloud:master

 

Tim Andersson has proposed merging ~andersson123/autopkgtest-cloud:cache-amqp-fix into autopkgtest-cloud:master.

Requested reviews:
  Canonical's Ubuntu QA (canonical-ubuntu-qa)

For more details, see:
https://code.launchpad.net/~andersson123/autopkgtest-cloud/+git/autopkgtest-cloud/+merge/456445
-- 
Your team Canonical's Ubuntu QA is requested to review the proposed merge of ~andersson123/autopkgtest-cloud:cache-amqp-fix into autopkgtest-cloud:master.
diff --git a/charms/focal/autopkgtest-web/webcontrol/cache-amqp b/charms/focal/autopkgtest-web/webcontrol/cache-amqp
index 512b495..202f331 100755
--- a/charms/focal/autopkgtest-web/webcontrol/cache-amqp
+++ b/charms/focal/autopkgtest-web/webcontrol/cache-amqp
@@ -10,11 +10,13 @@ import sys
 import tempfile
 import time
 import urllib.parse
+import urllib.request
 
 import amqplib.client_0_8 as amqp
 from amqplib.client_0_8.exceptions import AMQPChannelException
 
 AMQP_CONTEXTS = ["ubuntu", "huge", "ppa", "upstream"]
+WEBPAGE = "autopkgtest.ubuntu.com"  # <- maybe put this in a config file?
 
 
 class AutopkgtestQueueContents:
@@ -260,8 +262,31 @@ if __name__ == "__main__":
         print("No database found", file=sys.stderr)
         sys.exit(1)
 
-    aq = AutopkgtestQueueContents(amqp_uri, database)
-    queue_contents = aq.get_queue_contents()
+    queue_contents = {}
+    # We only actually want to interact with the rabbitmq queue if
+    # we're the leader - otherwise these two cache-amqp processes
+    # make the queue size go crazy in the KPI
+    if os.path.isfile("/run/autopkgtest-web-is-leader"):
+        # Get queue details from rabbitmq directly
+        aq = AutopkgtestQueueContents(amqp_uri, database)
+        queue_contents = aq.get_queue_contents()
+    else:
+        # We get queues.json from autopkgtest.ubuntu.com, see if it's
+        # the same as the version on disk. If it's not, then this webworker
+        # (the non leader one) has acquired queues.json from the leader.
+        # If they're the same we've queried ourselves for queues.json
+        # and we increment the SRVNAME cookie to query the other
+        # webworker (the leader)
+        queued_local = {}
+        if os.path.isfile(args.output):
+            with open(args.output, "r") as f:
+                queued_local = json.load(f)
+        for srvname_cookie in ["S0", "S1"]:
+            req = urllib.request.Request(WEBPAGE + "/queues.json")
+            req.add_header("Cookie", "SRVNAME=" + srvname_cookie)
+            queue_contents = json.loads(urllib.request.urlopen(req).read())
+            if queue_contents != queued_local:
+                break
 
     with tempfile.NamedTemporaryFile(
         mode="w", dir=os.path.dirname(args.output), delete=False