Skip to content
Merged

Next #15

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
FROM nginx:alpine

RUN apk add python3 py3-pip supervisor
RUN pip3 install --break-system-packages google-cloud-run
RUN apk add curl jq bash 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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Toujours utile ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oui lui c'est le maitre, celui qui lance 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"]
66 changes: 66 additions & 0 deletions cleanup.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/usr/bin/env bash
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}"

# 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

echo "Health check loop ended."
31 changes: 0 additions & 31 deletions cleanup_watcher.py

This file was deleted.

8 changes: 8 additions & 0 deletions supervisor/conf.d/cleanup.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[program:cleanup]
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
8 changes: 8 additions & 0 deletions supervisor/conf.d/nginx.conf
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions supervisor/supervisord.conf
Original file line number Diff line number Diff line change
@@ -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
23 changes: 0 additions & 23 deletions supervisord.conf

This file was deleted.

Loading