From 152f3526df79166a21b5edc683d6c9fbdfb20b1a Mon Sep 17 00:00:00 2001
From: Tim Geoghegan <timg@divviup.org>
Date: Mon, 17 Jun 2024 15:34:36 -0700
Subject: [PATCH] `compose.yaml` rough edges (#1125)

Addresses some rough edges in compose.yaml:

- Adds a healthcheck on the postgres service to make startup of
  dependent schema migrator services more reliable.
- `pair_aggregator` would re-run every time `docker compose up` is run,
  and fail after the first time. It now drops a file `/tmp/done` to
  indicate it has already run to completion.

Part of #1096
---
 compose.yaml | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

diff --git a/compose.yaml b/compose.yaml
index 55bb2105..7ff6f32b 100644
--- a/compose.yaml
+++ b/compose.yaml
@@ -24,7 +24,7 @@ x-janus-migrate: &janus_migrate
     - "60"
   depends_on:
     postgres:
-      condition: service_started
+      condition: service_healthy
 
 x-janus-environment: &janus_environment
   RUST_LOG: info
@@ -54,6 +54,17 @@ services:
     image: docker.io/library/postgres:16
     ports:
       - "5432:5432"
+    healthcheck:
+      test:
+        - "CMD"
+        - "/bin/sh"
+        - "-c"
+        - |
+          psql postgres://postgres@127.0.0.1/divviup -c "" && \
+            psql postgres://postgres@127.0.0.1/janus_1 -c ""  && \
+            psql postgres://postgres@127.0.0.1/janus_2 -c ""
+      interval: 1s
+      retries: 10
     restart: always
     environment:
       POSTGRES_PASSWORD: postgres
@@ -70,7 +81,7 @@ services:
       DATABASE_URL: postgres://postgres:postgres@postgres:5432/divviup
     depends_on:
       postgres:
-        condition: service_started
+        condition: service_healthy
 
   pair_aggregator:
     <<: *divviup_image
@@ -78,6 +89,7 @@ services:
       - /bin/sh
       - -c
       - |
+        test -f /tmp/done || \
         /divviup --url=http://localhost:8080 --token="" \
           aggregator create \
           --name=leader --api-url=http://janus_1_aggregator:8080/aggregator-api \
@@ -88,7 +100,8 @@ services:
         /divviup --url=http://localhost:8080 --token="" \
           aggregator create \
           --name=helper --api-url=http://janus_2_aggregator:8080/aggregator-api \
-          --bearer-token=0000
+          --bearer-token=0000 && \
+        touch /tmp/done
     network_mode: service:divviup_api
     depends_on:
       divviup_api: