From 1a9eaf13df9e5caf07e3b03b1643ee8f08bf4e56 Mon Sep 17 00:00:00 2001 From: Haitao Pan Date: Fri, 23 Jan 2026 23:50:52 +0800 Subject: [PATCH] fix: wait for stunnel before starting --- config/account.cloudrun.yaml | 2 +- deploy/gcp/cloud-run/service.yaml | 11 +++++++++-- entrypoint.sh | 17 +++++++++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/config/account.cloudrun.yaml b/config/account.cloudrun.yaml index 6aa26f7..f2df451 100644 --- a/config/account.cloudrun.yaml +++ b/config/account.cloudrun.yaml @@ -31,7 +31,7 @@ server: store: driver: "postgres" - dsn: "postgres://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}?sslmode=disable" + dsn: "postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}?sslmode=disable" maxOpenConns: 30 maxIdleConns: 10 diff --git a/deploy/gcp/cloud-run/service.yaml b/deploy/gcp/cloud-run/service.yaml index 822781e..ae2416d 100644 --- a/deploy/gcp/cloud-run/service.yaml +++ b/deploy/gcp/cloud-run/service.yaml @@ -20,7 +20,7 @@ spec: containers: # --- 主应用容器 --- - name: accounts-api - image: asia-northeast1-docker.pkg.dev/xzerolab-480008/cloud-run-source-deploy/accounts.svc.plus/accounts-svc-plus:latest + image: asia-northeast1-docker.pkg.dev/xzerolab-480008/cloud-run-source-deploy/accounts.svc.plus/accounts-svc-plus:217c617b25328b3529ba2ec983d5a4f71849cf30 ports: - name: http1 containerPort: 8080 @@ -32,7 +32,12 @@ spec: - name: DB_PASSWORD valueFrom: secretKeyRef: - name: admin_password + name: postgres-password + key: latest + - name: POSTGRES_PASSWORD + valueFrom: + secretKeyRef: + name: postgres-password key: latest - name: DB_HOST value: "127.0.0.1" @@ -40,6 +45,8 @@ spec: value: "15432" - name: DB_USER value: postgres + - name: POSTGRES_USER + value: postgres - name: DB_NAME value: postgres resources: diff --git a/entrypoint.sh b/entrypoint.sh index 88f600f..e59ea04 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -31,4 +31,21 @@ if [ -n "${PORT:-}" ]; then CONFIG_FILE="${tmp_cfg}" fi +if [ -n "${DB_HOST:-}" ] && [ -n "${DB_PORT:-}" ]; then + if [ "${DB_HOST}" = "127.0.0.1" ] || [ "${DB_HOST}" = "localhost" ]; then + if command -v nc >/dev/null; then + wait_seconds="${STUNNEL_WAIT_SECONDS:-30}" + i=0 + while ! nc -z "${DB_HOST}" "${DB_PORT}" >/dev/null 2>&1; do + i=$((i + 1)) + if [ "${i}" -ge "${wait_seconds}" ]; then + echo "stunnel not ready after ${wait_seconds}s on ${DB_HOST}:${DB_PORT}" >&2 + break + fi + sleep 1 + done + fi + fi +fi + exec /usr/local/bin/account --config "${CONFIG_FILE}" "$@"