perf(svc-db-postgres): wait for TCP auth once per postgres container, not per consumer

Every database consumer resolved the postgres container and waited for TCP auth again, although the container was created once in 00_core with a handler flush and is not restarted later in the play. 01_init now includes the resolve-and-wait block (moved unchanged to 01_init/ready.yml) only when the service|container|port target differs from the last verified one; a container-manager-restart handler forgets the target so a Docker daemon restart forces the wait again. About 67 s per full production run.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
kevinveenbirkenbachandClaude Opus 5.5 committed 2026-10-25 19:46:49 +01:00
1 parent 1f61f738e1
commit 463974e8be
4 files changed
+60 -35

No files matched your search

+1
View File
@@ -1,3 +1,4 @@
---
database_init: false # When true a database is initialized
database_consumer_id: "svc-db-postgres" # Self consumer
postgres_verified_target: ""
+6
View File
@@ -0,0 +1,6 @@
---
- name: 🧊 Forget the Postgres container whose TCP auth was verified
ansible.builtin.set_fact:
postgres_verified_target: ""
changed_when: false
listen: container-manager-restart
+11 -35
View File
@@ -12,42 +12,18 @@
changed_when: false
no_log: "{{ MASK_CREDENTIALS_IN_LOGS | bool }}"
- name: "🔍 Postgres | Resolve hosting node + container id"
ansible.builtin.include_tasks: "{{ lookup('path_absolute', 'roles/sys-svc-compose/tasks/utils/swarm/resolve_host_cid.yml') }}"
- name: "🔍 Postgres | Resolve the container and wait for TCP auth once per container"
ansible.builtin.include_tasks: 01_init/ready.yml
when: postgres_verified_target != postgres_target
vars:
resolve_service_key: "{{ entity_name }}"
resolve_service_name: "{{ lookup('database', database_consumer_id, 'service_name') }}"
resolve_output_prefix: POSTGRES
resolve_compose_cid: "{{ lookup('database', database_consumer_id, 'container') }}"
- name: ⏳ Wait until postgres TCP auth works
block:
- name: ⏳ Wait for Postgres TCP auth
no_log: "{{ MASK_CREDENTIALS_IN_LOGS | bool }}"
ansible.builtin.shell: > # nocheck: container-exec-resolver POSTGRES_LOCAL_CID resolved at delegate_to runtime
container exec -e PGPASSWORD={{ POSTGRES_PASSWORD | quote }} {{ POSTGRES_LOCAL_CID | quote }} sh -lc
'psql -h "{{ POSTGRES_LOCAL_HOST }}" -p "{{ POSTGRES_PORT }}"
-U postgres -d postgres -c "SELECT 1"'
delegate_to: "{{ POSTGRES_HOST_NODE }}"
register: pg_ready
until: pg_ready.rc == 0
retries: 30
delay: 5
changed_when: false
rescue:
- name: "💾 Postgres | Dump pg_hba.conf into the diagnostics folder" # nocheck: rescue-extra-diagnostics pg_hba.conf is postgres-specific auth config the generic rescue script cannot capture, and it is what a failed TCP-auth wait needs
ansible.builtin.script: "{{ lookup('path_absolute', 'roles/svc-db-postgres/files/shell/dump_pg_hba.sh') }}"
environment:
POSTGRES_CONTAINER_ADDRESS: "{{ POSTGRES_LOCAL_CID }}"
PGPASSWORD: "{{ POSTGRES_PASSWORD }}"
delegate_to: "{{ POSTGRES_HOST_NODE }}"
changed_when: false
failed_when: false
no_log: "{{ MASK_CREDENTIALS_IN_LOGS | bool }}"
- name: "🛑 Postgres | fail after pg_hba dump"
ansible.builtin.fail:
msg: "Postgres TCP auth never became ready; pg_hba.conf was dumped into the diagnostics folder (the workflow-level rescue capture collects the full snapshot)"
postgres_target: >-
{{
[
lookup('database', database_consumer_id, 'service_name'),
lookup('database', database_consumer_id, 'container'),
POSTGRES_PORT
] | join('|')
}}
- name: "📁 Create database: {{ database_name }}"
community.postgresql.postgresql_db:
@@ -0,0 +1,42 @@
---
- name: "🔍 Postgres | Resolve hosting node + container id"
ansible.builtin.include_tasks: "{{ lookup('path_absolute', 'roles/sys-svc-compose/tasks/utils/swarm/resolve_host_cid.yml') }}"
vars:
resolve_service_key: "{{ entity_name }}"
resolve_service_name: "{{ lookup('database', database_consumer_id, 'service_name') }}"
resolve_output_prefix: POSTGRES
resolve_compose_cid: "{{ lookup('database', database_consumer_id, 'container') }}"
- name: ⏳ Wait until postgres TCP auth works
block:
- name: ⏳ Wait for Postgres TCP auth
no_log: "{{ MASK_CREDENTIALS_IN_LOGS | bool }}"
ansible.builtin.shell: > # nocheck: container-exec-resolver POSTGRES_LOCAL_CID resolved at delegate_to runtime
container exec -e PGPASSWORD={{ POSTGRES_PASSWORD | quote }} {{ POSTGRES_LOCAL_CID | quote }} sh -lc
'psql -h "{{ POSTGRES_LOCAL_HOST }}" -p "{{ POSTGRES_PORT }}"
-U postgres -d postgres -c "SELECT 1"'
delegate_to: "{{ POSTGRES_HOST_NODE }}"
register: pg_ready
until: pg_ready.rc == 0
retries: 30
delay: 5
changed_when: false
rescue:
- name: "💾 Postgres | Dump pg_hba.conf into the diagnostics folder" # nocheck: rescue-extra-diagnostics pg_hba.conf is postgres-specific auth config the generic rescue script cannot capture, and it is what a failed TCP-auth wait needs
ansible.builtin.script: "{{ lookup('path_absolute', 'roles/svc-db-postgres/files/shell/dump_pg_hba.sh') }}"
environment:
POSTGRES_CONTAINER_ADDRESS: "{{ POSTGRES_LOCAL_CID }}"
PGPASSWORD: "{{ POSTGRES_PASSWORD }}"
delegate_to: "{{ POSTGRES_HOST_NODE }}"
changed_when: false
failed_when: false
no_log: "{{ MASK_CREDENTIALS_IN_LOGS | bool }}"
- name: "🛑 Postgres | fail after pg_hba dump"
ansible.builtin.fail:
msg: "Postgres TCP auth never became ready; pg_hba.conf was dumped into the diagnostics folder (the workflow-level rescue capture collects the full snapshot)"
- name: "🧊 Postgres | Remember the container whose TCP auth works"
ansible.builtin.set_fact:
postgres_verified_target: "{{ postgres_target }}"
changed_when: false