File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11FROM nginx:alpine
2+
3+ RUN apk add python3 py3-pip supervisor
4+ RUN pip3 install google-cloud-run
5+
26COPY nginx.conf /etc/nginx/nginx.conf
37
4- EXPOSE 80
8+ COPY supervisord.conf /etc/supervisord.conf
9+ RUN mkdir -p /var/log/supervisor
10+
11+ COPY cleanup_watcher.py /usr/local/bin/cleanup_watcher.py
12+ RUN chmod +x /usr/local/bin/cleanup_watcher.py
13+
14+ CMD ["/usr/bin/supervisord" , "-c" , "/etc/supervisord.conf" ]
Original file line number Diff line number Diff line change 1+ import os
2+ import time
3+ import requests
4+ from google .cloud import run_v2
5+
6+ PROJECT = os .getenv ("GOOGLE_CLOUD_PROJECT" )
7+ REGION = os .getenv ("GOOGLE_CLOUD_REGION" , "us-central1" )
8+ SERVICE_NAME = os .getenv ("K_SERVICE" )
9+
10+ HEARTBEAT_URL = "http://127.0.0.1:5000/health"
11+
12+
13+ def delete_service ():
14+ if not PROJECT or not SERVICE_NAME :
15+ print ("Cannot delete: missing env vars" )
16+ return
17+ client = run_v2 .ServicesClient ()
18+ name = f"projects/{ PROJECT } /locations/{ REGION } /services/{ SERVICE_NAME } "
19+ try :
20+ print (f"Flask appears down → Deleting service { name } " )
21+ client .delete_service (name = name )
22+ except Exception as e :
23+ print (f"Delete failed: { e } " )
24+
25+
26+ while True :
27+ time .sleep (60 )
28+ delete_service ()
29+ break
30+ # try:
31+ # r = requests.get(HEARTBEAT_URL, timeout=5)
32+ # if r.status_code != 200:
33+ # raise Exception("Bad status")
34+ # except Exception:
35+ # delete_service()
36+ # break
Original file line number Diff line number Diff line change 1+ [supervisord]
2+ nodaemon=true
3+ logfile=/dev/stdout
4+ pidfile=/var/run/supervisord.pid
5+
6+ [program:nginx]
7+ command=nginx -g 'daemon off;'
8+ autostart=true
9+ autorestart=true
10+ stdout_logfile=/dev/stdout
11+ stderr_logfile=/dev/stderr
12+
13+ [program:cleanup-watcher]
14+ command=python3 /usr/local/bin/cleanup_watcher.py
15+ autostart=true
16+ autorestart=true
17+ stdout_logfile=/dev/stdout
18+ stderr_logfile=/dev/stderr
You can’t perform that action at this time.
0 commit comments