From 0cc27d5cd605da5371a4e9ae78b086a505ecac68 Mon Sep 17 00:00:00 2001 From: linl33 Date: Tue, 25 Jul 2017 16:24:40 -0700 Subject: [PATCH] let db-bootstrap check sync endpoint --- README.md | 2 -- db-bootstrap/bootstrap.sh | 20 ++++++++++++++++---- docker-compose.yml | 2 +- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 6e1dc2a..69221bb 100644 --- a/README.md +++ b/README.md @@ -17,8 +17,6 @@ Same as [sync-endpoint-containers](https://github.com/jbeorse/sync-endpoint-cont 3. Navigate to `https://127.0.0.1:40000` and create a user, see the [LDAP](#ldap) section below for detail 4. Sync endpoint is now running at `http://127.0.0.1` -Sometimes the Sync endpoint service is started before the database service, when this happens remove the stack and recreate it. - ## Clean up 1. Remove the stack with, `docker stack rm syncldap` diff --git a/db-bootstrap/bootstrap.sh b/db-bootstrap/bootstrap.sh index 7a9d671..c56565b 100755 --- a/db-bootstrap/bootstrap.sh +++ b/db-bootstrap/bootstrap.sh @@ -1,4 +1,4 @@ -#!/bin/sh -eu +#!/bin/sh -u set -o pipefail @@ -10,9 +10,9 @@ if [ ${DB_BOOTSTRAP} != true ]; then exit 0 fi; -# sleep 5 seconds to wait for db service to be created +# sleep 10 seconds to wait for db service to be created # TODO: test for it in a loop instead -sleep 5 +sleep 10 STACK_NAME=$(docker ps -f "ID=${HOSTNAME}" --format '{{ .Names }}' | grep -oE '^[^_]*') DB_VAR=$(docker service inspect -f "{{if eq \"${STACK_NAME}_db\" .Spec.Name}}{{.Spec.TaskTemplate.ContainerSpec.Image}}{{end}}" $(docker service ls -q) | grep -oE '^[^:]*') @@ -53,6 +53,18 @@ esac echo "Done" -# TODO ping Sync after db bootstrap done +echo "Checking Sync endpoint" + +# Wait 5 seconds for a 200 from Sync +timeout -t 5 sh -c 'while ! echo -ne "GET / HTTP/1.1\nHost: sync\n\n" | nc -w 1 sync 8080 | grep -q "HTTP/1.1 200"; do echo '\''waiting for Sync'\''; sleep 1; done' + +if [ $? -eq 143 ]; then + echo "Timeout" + echo "Killing Sync endpoint" + # let Docker restart the container + docker kill $(docker ps -f "label=com.docker.swarm.service.name=${STACK_NAME}_sync" --format '{{.ID}}') +fi; + +echo "Exit" exit 0 \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index c4ee919..cd0db69 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -59,7 +59,7 @@ services: - "80:8080" env_file: - sync.env - # uncomment these after uncomment the ones below in the root level configs + # uncomment these after uncommenting the ones below in the root level configs # configs: # - org.opendatakit.sync.ldapcert # - org.opendatakit.aggregate.logging.properties