Skip to content

Commit 972d7d8

Browse files
committed
add supervisor
1 parent faba152 commit 972d7d8

3 files changed

Lines changed: 65 additions & 1 deletion

File tree

Dockerfile

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
FROM nginx:alpine
2+
3+
RUN apk add python3 py3-pip supervisor
4+
RUN pip3 install google-cloud-run
5+
26
COPY 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"]

cleanup_watcher.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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

supervisord.conf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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

0 commit comments

Comments
 (0)