Skip to content

Instantly share code, notes, and snippets.

@lucj
Created January 23, 2017 10:44
Show Gist options
  • Save lucj/73941c0bb0f12ed139c23082437c2c42 to your computer and use it in GitHub Desktop.
Save lucj/73941c0bb0f12ed139c23082437c2c42 to your computer and use it in GitHub Desktop.
api-piloted
$cat containerpilot.json
{
"consul": "localhost:8500",
"preStart": "/app/manage.sh prestart",
"services": [
{
"name": "api",
"health": "/usr/bin/curl -o /dev/null --fail -s http://localhost/health",
"poll": 3,
"ttl": 10,
"port": 80
}
],
"coprocesses": [
{
"command": ["/usr/local/bin/consul", "agent",
"-data-dir=/data",
"-config-dir=/config",
"-rejoin",
"-retry-join", "{{ if .CONSUL_HOST }}{{ .CONSUL_HOST }}{{ else }}consul{{ end }}",
"-retry-max", "10",
"-retry-interval", "10s"],
"restarts": "unlimited"
}
],
"backends": [
{
"name": "mongodb-replicaset",
"poll": 3,
"onChange": "/app/manage.sh db-change"
}
]
}
$ cat manage.sh
#!/bin/sh
event=$1
echo "Received event:$event"
if [ "$event" = "prestart" ];then
while [[ "$(curl -s http://${CONSUL_HOST}:8500/v1/health/service/mongodb-replicaset | grep passing)" = "" ]]
do
echo "db is not yet healthly..."
sleep 5
done
echo "db is healthly, moving on..."
exit 0
fi
# If db not accessible anymore, restart the api service
if [ "$event" = "db-change" ];then
pkill -SIGHUP node
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment