Created
April 27, 2019 12:53
-
-
Save seeekr/de3e93d28c8b1f4a19493b4d7b5619d5 to your computer and use it in GitHub Desktop.
run one off tasks in a docker swarm context
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -euo pipefail | |
svc=$1 | |
replicas () { | |
docker service ls -f name=$svc --format '{{.Replicas}}' | |
} | |
# make sure it's at 0 running instances | |
[[ "$(replicas)" == "0/0" ]] || docker service scale $svc=0 > /dev/null | |
# scale it up, tail the logs | |
# todo we'll have to scale it to N=num_backend_replica once we go beyond a single backend instance | |
# and we're also in trouble once we go beyond 1 backend running per host, but that'll require more changes anyway | |
docker service scale $svc=1 -d | |
docker service logs $svc --raw --no-trunc -f --since 1s & | |
# wait until task starts running, then is not running any more, then kill the log tailing process | |
sleep 2 | |
# DISCLAIMER: this is going to break for really short tasks, don't have a good solution yet | |
until [[ "$(replicas)" =~ ^"1/" ]]; do sleep 1; done | |
until [[ "$(replicas)" =~ ^"0/" ]]; do sleep 1; done | |
sleep 1 # make sure we got the logs | |
kill %1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment