diff --git a/build.yaml b/build.yaml index 4aea474..52447b5 100644 --- a/build.yaml +++ b/build.yaml @@ -2,4 +2,5 @@ version: 1 platform: python platformVersion: "3.14" run: - port: 8080 \ No newline at end of file + port: 8080 + startCommand: "bash /output/flaky-startup.sh" diff --git a/flaky-startup.sh b/flaky-startup.sh new file mode 100755 index 0000000..590ee0e --- /dev/null +++ b/flaky-startup.sh @@ -0,0 +1,41 @@ +#!/bin/bash +set +e +TAG="${EMBR_DEPLOYMENT_ID:-unknown}" +echo "[healthy-startup ${TAG}] starting at $(date -u)" +echo "[healthy-startup ${TAG}] PID=$$, PPID=$PPID" +echo "[healthy-startup ${TAG}] ports in use BEFORE bind:" +ss -ltnp 2>&1 | grep -E ':8080|LISTEN' | head -10 +echo "[healthy-startup ${TAG}] my parent process tree:" +ps -ef --forest 2>&1 | head -30 + +python -c " +import socket, sys, time, os +print(f'[healthy-startup {os.environ.get(\"EMBR_DEPLOYMENT_ID\",\"?\")}] attempting bind on :8080', flush=True) +attempts = 0 +while attempts < 30: + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + try: + s.bind(('0.0.0.0', 8080)) + s.listen(8) + print(f'[healthy-startup {os.environ.get(\"EMBR_DEPLOYMENT_ID\",\"?\")}] BOUND :8080 successfully after {attempts} attempts', flush=True) + break + except OSError as e: + attempts += 1 + print(f'[healthy-startup {os.environ.get(\"EMBR_DEPLOYMENT_ID\",\"?\")}] BIND FAILED attempt {attempts}: {e}', flush=True) + s.close() + time.sleep(1) +else: + print(f'[healthy-startup {os.environ.get(\"EMBR_DEPLOYMENT_ID\",\"?\")}] GAVE UP after 30 attempts', flush=True) + sys.exit(98) +print(f'[healthy-startup {os.environ.get(\"EMBR_DEPLOYMENT_ID\",\"?\")}] serving forever now', flush=True) +# minimal HTTP responder +from http.server import HTTPServer, BaseHTTPRequestHandler +class H(BaseHTTPRequestHandler): + def do_GET(self): + self.send_response(200); self.end_headers(); self.wfile.write(b'ok') + def log_message(self, *a): pass +HTTPServer.allow_reuse_address = True +HTTPServer(('0.0.0.0', 8080), H, bind_and_activate=False).serve_forever() +" || true +sleep 60