From 92fdb49f2751a2d95f0df869c4000d9eb9053a13 Mon Sep 17 00:00:00 2001
From: Vincent SELLIER <vincent.sellier@softwareheritage.org>
Date: Mon, 26 Aug 2024 17:25:12 +0200
Subject: [PATCH] swh/counters: Avoid refresh_history cronjob infinite retries

- Use the retry command line
- Split the rpc checks from the refresh endpoint call
- Fix a couple of minor side issues

Related to swh/infra/sysadm-environment#5387
---
 .../counters/refresh-counters-cache-cronjob.yaml  |  3 ++-
 swh/templates/counters/script-utils.yaml          | 15 +++++++--------
 2 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/swh/templates/counters/refresh-counters-cache-cronjob.yaml b/swh/templates/counters/refresh-counters-cache-cronjob.yaml
index 2b4053331..1cd62eede 100644
--- a/swh/templates/counters/refresh-counters-cache-cronjob.yaml
+++ b/swh/templates/counters/refresh-counters-cache-cronjob.yaml
@@ -4,6 +4,7 @@
 apiVersion: batch/v1
 kind: CronJob
 metadata:
+  namespace: {{ $.Values.namespace }}
   name: counters-refresh-counters-cache-cronjob
 spec:
   # By default, every 4h
@@ -92,7 +93,7 @@ spec:
               items:
               - key: "refresh-counters-cache.sh"
                 path: "refresh-counters-cache.sh"
-          restartPolicy: OnFailure
+          restartPolicy: Never
 
 {{- end -}}
 {{- end -}}
diff --git a/swh/templates/counters/script-utils.yaml b/swh/templates/counters/script-utils.yaml
index f84c81440..7a91e01b8 100644
--- a/swh/templates/counters/script-utils.yaml
+++ b/swh/templates/counters/script-utils.yaml
@@ -34,14 +34,13 @@ data:
     }
     EOF
 
-    # This waits for the rpc to be up
-    while true; do
-        sleep 1
-        set +e
-        curl -f -v -XPOST -H 'Content-Type: application/json' $host/refresh_history -d @"${tmp_file}"
-        [[ $? -ne 0 ]] || break
-        set -e
-    done
+    echo Wait 60s for the ${host} rpc service
+    retry -t 6 -d 10 -- curl -f -s ${host}
+    echo "${host} is up"
+
+    echo "Refreshing the counters history"
+    curl -f -v -XPOST -H 'Content-Type: application/json' $host/refresh_history -d @"${tmp_file}"
+    echo "Done"
 
   fetch-static-history.sh: |
     #!/bin/bash
-- 
GitLab