Instrumental | 08e9340 | 2018-10-03 08:38:52 -0500 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # |
| 3 | # A Script for use in Pods... Check for status files, and validate before moving on. |
| 4 | # |
| 5 | DIR="/opt/app/aaf/status" |
| 6 | APP=$1 |
| 7 | shift |
| 8 | OTHER=$1 |
| 9 | shift |
| 10 | |
| 11 | function status { |
| 12 | if [ -d "$DIR" ]; then |
| 13 | echo "$@" > $DIR/$APP |
| 14 | fi |
| 15 | } |
| 16 | |
Instrumental | 08e9340 | 2018-10-03 08:38:52 -0500 | [diff] [blame] | 17 | |
| 18 | function check { |
| 19 | if [ -d "$DIR" ]; then |
| 20 | if [ -e "$DIR/$OTHER" ]; then |
| 21 | echo "$(cat $DIR/$OTHER)" |
| 22 | else |
| 23 | echo "$DIR/$OTHER does not exist" |
| 24 | fi |
| 25 | else |
| 26 | echo "$DIR does not exist" |
| 27 | fi |
| 28 | } |
| 29 | |
Instrumental | ca788dc | 2018-11-03 14:38:21 -0500 | [diff] [blame^] | 30 | function wait { |
| 31 | n=0 |
| 32 | while [ $n -lt 40 ]; do |
| 33 | rv="$(check)" |
| 34 | echo "$rv" |
| 35 | if [ "$rv" = "ready" ]; then |
| 36 | echo "$OTHER is $rv" |
| 37 | n=10000 |
| 38 | else |
| 39 | (( ++n )) |
| 40 | echo "Sleep 10 (iteration $n)" |
| 41 | sleep 10 |
| 42 | fi |
| 43 | done |
| 44 | } |
| 45 | |
Instrumental | 14f3707 | 2018-10-09 08:32:06 -0500 | [diff] [blame] | 46 | function start { |
| 47 | n=0 |
| 48 | while [ $n -lt 40 ]; do |
| 49 | rv="$(check)" |
| 50 | echo "$OTHER is $rv" |
| 51 | if [ "$rv" = "ready" ]; then |
| 52 | # This is critical. Until status is literally "ready" in the status directory, no processes will start |
| 53 | status ready |
| 54 | echo "Starting $@" |
| 55 | n=10000 |
| 56 | else |
| 57 | (( ++n )) |
| 58 | echo "Sleep 10 (iteration $n)" |
| 59 | sleep 10 |
| 60 | fi |
| 61 | done |
| 62 | } |
Instrumental | 08e9340 | 2018-10-03 08:38:52 -0500 | [diff] [blame] | 63 | |
Instrumental | ca788dc | 2018-11-03 14:38:21 -0500 | [diff] [blame^] | 64 | case "$OTHER" in |
| 65 | sleep) |
| 66 | echo "Sleeping $1" |
| 67 | status "Sleeping $1" |
| 68 | sleep $1 |
| 69 | shift |
| 70 | status "ready" |
| 71 | echo "Done" |
| 72 | ;; |
| 73 | wait) |
| 74 | OTHER="$1" |
| 75 | shift |
| 76 | wait |
| 77 | ;; |
| 78 | *) |
| 79 | echo "App $APP is waiting to start until $OTHER is ready" |
| 80 | status "waiting for $OTHER" |
| 81 | |
| 82 | start |
| 83 | ;; |
| 84 | esac |
Instrumental | 08e9340 | 2018-10-03 08:38:52 -0500 | [diff] [blame] | 85 | |
| 86 | eval "$@" |