From 2b036d3d9352283db50aa3f3f2aba21acc8fee47 Mon Sep 17 00:00:00 2001 From: Arnaud Botella Date: Fri, 24 Jul 2026 13:35:47 +0200 Subject: [PATCH 1/6] test --- Dockerfile | 13 +++++----- cleanup.bash | 43 ++++++++++++++++++++++++++++++++++ supervisor/conf.d/cleanup.conf | 8 +++++++ supervisor/conf.d/nginx.conf | 8 +++++++ supervisor/supervisord.conf | 8 +++++++ supervisord.conf | 23 ------------------ 6 files changed, 74 insertions(+), 29 deletions(-) create mode 100644 cleanup.bash create mode 100644 supervisor/conf.d/cleanup.conf create mode 100644 supervisor/conf.d/nginx.conf create mode 100644 supervisor/supervisord.conf delete mode 100644 supervisord.conf diff --git a/Dockerfile b/Dockerfile index 096068a..6702658 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,14 +1,15 @@ FROM nginx:alpine -RUN apk add python3 py3-pip supervisor -RUN pip3 install --break-system-packages google-cloud-run +RUN apk add curl jq supervisor -COPY nginx.conf /etc/nginx/nginx.conf -COPY supervisord.conf /etc/supervisord.conf +COPY supervisor/supervisord.conf /etc/supervisord.conf RUN mkdir -p /var/log/supervisor +RUN mkdir -p /etc/supervisor/conf.d -COPY cleanup_watcher.py /usr/local/bin/cleanup_watcher.py -RUN chmod +x /usr/local/bin/cleanup_watcher.py +COPY supervisor/conf.d /etc/supervisor/conf.d + +COPY cleanup.bash /usr/local/bin/cleanup.bash +RUN chmod +x /usr/local/bin/cleanup.bash CMD ["/usr/bin/supervisord", "-c", "/etc/supervisord.conf"] \ No newline at end of file diff --git a/cleanup.bash b/cleanup.bash new file mode 100644 index 0000000..4e4d59e --- /dev/null +++ b/cleanup.bash @@ -0,0 +1,43 @@ +#!/usr/bin/env bash +set -uo pipefail + +PARENT="${PARENT:?PARENT env var required}" +SERVICE_NAME="${K_SERVICE:?K_SERVICE env var required}" + +delete_service() { + echo "Flask appears down → Deleting service ${PARENT}/services/${SERVICE_NAME}" + + # Get an OAuth2 access token from the GCE/Cloud Run metadata server + local token + token=$(curl -s -H "Metadata-Flavor: Google" \ + "http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/token" \ + | jq -r .access_token) + + if [ -z "$token" ]; then + echo "Delete failed: could not obtain access token" + return 1 + fi + + local http_code + http_code=$(curl -s -o /tmp/delete_response.json -w "%{http_code}" \ + -X DELETE \ + -H "Authorization: Bearer ${token}" \ + "https://run.googleapis.com/v2/${PARENT}/services/${SERVICE_NAME}") + + if [[ "$http_code" == 2* ]]; then + echo "Service deletion request accepted (HTTP ${http_code})" + else + echo "Delete failed (HTTP ${http_code}): $(cat /tmp/delete_response.json)" + fi +} + +while true; do + sleep 30 + response=$(curl -s -o /dev/null -w "%{http_code}" --max-time 5 "http://127.0.0.1/geode/health") + echo "response ${response}" + + if [ "$response" != "200" ]; then + delete_service + break + fi +done \ No newline at end of file diff --git a/supervisor/conf.d/cleanup.conf b/supervisor/conf.d/cleanup.conf new file mode 100644 index 0000000..2c92b10 --- /dev/null +++ b/supervisor/conf.d/cleanup.conf @@ -0,0 +1,8 @@ +[program:cleanup-watcher] +command=bash /usr/local/bin/cleanup.bash +autostart=true +autorestart=true +stdout_logfile=/dev/stdout +stdout_logfile_maxbytes=0 +stderr_logfile=/dev/stderr +stderr_logfile_maxbytes=0 \ No newline at end of file diff --git a/supervisor/conf.d/nginx.conf b/supervisor/conf.d/nginx.conf new file mode 100644 index 0000000..7162fb9 --- /dev/null +++ b/supervisor/conf.d/nginx.conf @@ -0,0 +1,8 @@ +[program:nginx] +command=nginx -g 'daemon off;' +autostart=true +autorestart=true +stdout_logfile=/dev/stdout +stdout_logfile_maxbytes=0 +stderr_logfile=/dev/stderr +stderr_logfile_maxbytes=0 \ No newline at end of file diff --git a/supervisor/supervisord.conf b/supervisor/supervisord.conf new file mode 100644 index 0000000..be54755 --- /dev/null +++ b/supervisor/supervisord.conf @@ -0,0 +1,8 @@ +[supervisord] +nodaemon=true +logfile=/dev/stdout +logfile_maxbytes=0 +pidfile=/var/run/supervisord.pid + +[include] +files = /etc/supervisor/conf.d/*.conf \ No newline at end of file diff --git a/supervisord.conf b/supervisord.conf deleted file mode 100644 index 448e6b2..0000000 --- a/supervisord.conf +++ /dev/null @@ -1,23 +0,0 @@ -[supervisord] -nodaemon=true -logfile=/dev/stdout -logfile_maxbytes=0 -pidfile=/var/run/supervisord.pid - -[program:nginx] -command=nginx -g 'daemon off;' -autostart=true -autorestart=true -stdout_logfile=/dev/stdout -stdout_logfile_maxbytes=0 -stderr_logfile=/dev/stderr -stderr_logfile_maxbytes=0 - -[program:cleanup-watcher] -command=python3 /usr/local/bin/cleanup_watcher.py -autostart=true -autorestart=true -stdout_logfile=/dev/stdout -stdout_logfile_maxbytes=0 -stderr_logfile=/dev/stderr -stderr_logfile_maxbytes=0 \ No newline at end of file From fe129049bf3f1221747cf9bae5ee0661711f3285 Mon Sep 17 00:00:00 2001 From: Arnaud Botella Date: Fri, 24 Jul 2026 13:40:11 +0200 Subject: [PATCH 2/6] bash --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 6702658..c58efbf 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,6 @@ FROM nginx:alpine -RUN apk add curl jq supervisor +RUN apk add curl jq bash supervisor COPY supervisor/supervisord.conf /etc/supervisord.conf From 1319c1843e5e7c3fe138a2e95163b195677bebf0 Mon Sep 17 00:00:00 2001 From: Arnaud Botella Date: Fri, 24 Jul 2026 13:47:33 +0200 Subject: [PATCH 3/6] missing nginx conf --- Dockerfile | 1 + 1 file changed, 1 insertion(+) diff --git a/Dockerfile b/Dockerfile index c58efbf..f87a996 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,6 +2,7 @@ FROM nginx:alpine RUN apk add curl jq bash supervisor +COPY nginx.conf /etc/nginx/nginx.conf COPY supervisor/supervisord.conf /etc/supervisord.conf RUN mkdir -p /var/log/supervisor From 844e2303d4754a88b837abf93cd25b075bad3a06 Mon Sep 17 00:00:00 2001 From: Arnaud Botella Date: Fri, 24 Jul 2026 14:46:27 +0200 Subject: [PATCH 4/6] cleanup --- cleanup.bash | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/cleanup.bash b/cleanup.bash index 4e4d59e..72aeb4a 100644 --- a/cleanup.bash +++ b/cleanup.bash @@ -4,6 +4,27 @@ set -uo pipefail PARENT="${PARENT:?PARENT env var required}" SERVICE_NAME="${K_SERVICE:?K_SERVICE env var required}" +# ============================================= +# Cleanup / Crash Handler +# ============================================= +cleanup() { + EXIT_CODE=$? + if [ $EXIT_CODE -ne 0 ]; then + echo "==================================================================" + echo "❌ SCRIPT CRASHED with exit code ${EXIT_CODE}" + echo " This usually means something went wrong during health checking" + echo " or during the delete operation." + echo "==================================================================" + else + echo "Script finished normally." + fi +} + +# Catch errors, interrupts, and normal exit +trap cleanup EXIT +trap 'echo "❌ Script interrupted (SIGINT)"; exit 1' INT +trap 'echo "❌ Script terminated (SIGTERM)"; exit 1' TERM + delete_service() { echo "Flask appears down → Deleting service ${PARENT}/services/${SERVICE_NAME}" @@ -40,4 +61,6 @@ while true; do delete_service break fi -done \ No newline at end of file +done + +echo "Health check loop ended." \ No newline at end of file From d656578c8f4e8b9432bc493619ffbf21564e3120 Mon Sep 17 00:00:00 2001 From: Arnaud Botella Date: Fri, 24 Jul 2026 15:25:44 +0200 Subject: [PATCH 5/6] clean --- cleanup_watcher.py | 31 ------------------------------- 1 file changed, 31 deletions(-) delete mode 100644 cleanup_watcher.py diff --git a/cleanup_watcher.py b/cleanup_watcher.py deleted file mode 100644 index 81da247..0000000 --- a/cleanup_watcher.py +++ /dev/null @@ -1,31 +0,0 @@ -import os -import time -import requests -from google.cloud import run_v2 - - -PARENT = os.getenv("PARENT") -SERVICE_NAME = os.getenv("K_SERVICE") - - -def delete_service(): - print("Deleting service") - client = run_v2.ServicesClient() - name = f"{PARENT}/services/{SERVICE_NAME}" - try: - print(f"Flask appears down → Deleting service {name}") - client.delete_service(name=name) - except Exception as e: - print(f"Delete failed: {e}") - - -while True: - time.sleep(30) - try: - response = requests.get("http://127.0.0.1/geode/health", timeout=5) - print("response", response, flush=True) - if response.status_code != 200: - raise Exception("Bad status") - except Exception: - delete_service() - break From fef4bd7f9a436f659194b5ae7d867f4b5c6948d7 Mon Sep 17 00:00:00 2001 From: Arnaud Botella Date: Fri, 24 Jul 2026 15:26:39 +0200 Subject: [PATCH 6/6] typo --- supervisor/conf.d/cleanup.conf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/supervisor/conf.d/cleanup.conf b/supervisor/conf.d/cleanup.conf index 2c92b10..4429623 100644 --- a/supervisor/conf.d/cleanup.conf +++ b/supervisor/conf.d/cleanup.conf @@ -1,4 +1,4 @@ -[program:cleanup-watcher] +[program:cleanup] command=bash /usr/local/bin/cleanup.bash autostart=true autorestart=true