From 801ed1f45ebf86a54d39d610c7fb5281d33189ad Mon Sep 17 00:00:00 2001 From: NetworkCats Date: Sun, 8 Mar 2026 04:40:53 +0800 Subject: [PATCH] fix: verify backend is UP in HAProxy before removing old container --- deploy/ansible/roles/app/tasks/main.yml | 18 +++++++++++++++--- .../ansible/roles/app/templates/haproxy.cfg.j2 | 10 +++++++++- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/deploy/ansible/roles/app/tasks/main.yml b/deploy/ansible/roles/app/tasks/main.yml index 9b5c54a..9f51353 100644 --- a/deploy/ansible/roles/app/tasks/main.yml +++ b/deploy/ansible/roles/app/tasks/main.yml @@ -179,9 +179,21 @@ docker kill -s HUP rustyip-haproxy changed_when: true - - name: Wait for HAProxy to settle - ansible.builtin.pause: - seconds: 5 + - name: Wait for new backend to be UP in HAProxy + ansible.builtin.shell: | + for i in $(seq 1 30); do + csv=$(docker exec rustyip-haproxy wget -qO- \ + "http://127.0.0.1:9000/stats;csv;norefresh" 2>/dev/null) + status=$(echo "$csv" | awk -F, '$1 == "app" && $2 == "{{ deploy_target }}" {print $18}') + if [ "$status" = "UP" ]; then + echo "backend {{ deploy_target }} is UP in HAProxy" + exit 0 + fi + sleep 2 + done + echo "timeout waiting for backend {{ deploy_target }} to become UP" + exit 1 + changed_when: false - name: Stop old active container ansible.builtin.shell: | diff --git a/deploy/ansible/roles/app/templates/haproxy.cfg.j2 b/deploy/ansible/roles/app/templates/haproxy.cfg.j2 index 7e3ec3c..73e83c0 100644 --- a/deploy/ansible/roles/app/templates/haproxy.cfg.j2 +++ b/deploy/ansible/roles/app/templates/haproxy.cfg.j2 @@ -65,13 +65,21 @@ frontend https_in backend global_rate_tracking stick-table type string len 6 size 1 expire 10m store http_req_rate(1s) +frontend stats_internal + bind 127.0.0.1:9000 + mode http + stats enable + stats uri /stats + stats refresh 2s + resolvers docker_dns nameserver dns1 127.0.0.11:53 resolve_retries 3 timeout resolve 1s timeout retry 1s hold valid 10s - hold nx 5s + hold nx 30s + hold obsolete 30s backend app balance roundrobin