@@ -37,32 +37,51 @@ _killall ${app_name}
3737
3838swift build # synchronously ensure built
3939
40- swift run ${app_name} &
40+ declare -r log_file=" /tmp/${app_name} .log"
41+ rm -f ${log_file}
42+ swift run ${app_name} > ${log_file} &
43+
44+ declare -r supervision_replace_grep_txt=' supervision: REPLACE'
45+ declare -r supervision_stop_grep_txt=' supervision: STOP'
46+
47+ # we want to wait until 2 STOPs are found in the logs; then we can check if the other conditions are as we expect
48+ echo " Waiting for servants to REPLACE and STOP..."
49+ spin=1 # spin counter
50+ max_spins=20
51+ while [[ $( cat ${log_file} | grep " ${supervision_stop_grep_txt} " | wc -l) -ne 2 ]]; do
52+ sleep 1
53+ spin=$(( spin+ 1 ))
54+ if [[ ${spin} -eq ${max_spins} ]]; then
55+ echoerr " Never saw enough '${supervision_stop_grep_txt} ' in logs."
56+ cat ${log_file}
57+ exit -1
58+ fi
59+ done
4160
42- await_n_processes " $app_name " 3
43-
44- pid_master=$( ps aux | grep ${app_name} | grep -v grep | grep -v servant | awk ' { print $2 }' )
45- pid_servant=$( ps aux | grep ${app_name} | grep -v grep | grep servant | head -n1 | awk ' { print $2 }' )
46-
47- echo " > PID Master: ${pid_master} "
48- echo " > PID Servant: ${pid_servant} "
61+ echo ' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
62+ cat ${log_file} | grep " ${supervision_replace_grep_txt} "
63+ echo ' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
4964
50- echo ' ~~~~~~~~~~~~BEFORE KILL~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
51- ps aux | grep ${app_name}
5265echo ' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
66+ cat ${log_file} | grep " ${supervision_stop_grep_txt} "
67+ echo ' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
68+
69+ if [[ $( cat ${log_file} | grep " ${supervision_replace_grep_txt} " | wc -l) -ne 2 ]]; then
70+ echoerr " ERROR: We expected 2 servants to only restart once, yet more restarts were detected!"
71+ cat ${log_file}
5372
54- sleep 3 # TODO rather, sleep until another proc replaces the servant automatically
73+ _killall ${app_name}
74+ exit -1
75+ fi
5576
56- # the 1 servant should die, but be restarted so we'll be back at two processes
57- await_n_processes " $app_name " 3
77+ if [[ $( cat ${log_file} | grep " ${supervision_stop_grep_txt} " | wc -l) -ne 2 ]]; then
78+ echoerr " ERROR: Expected the servants to STOP after they are replaced once!"
79+ cat ${log_file}
5880
59- if [[ $( ps aux | awk ' {print $2}' | grep ${pid_servant} | grep -v ' grep' | wc -l) -ne 0 ]]; then
60- echo " ERROR: Seems the servant was not killed!!!"
81+ _killall ${app_name}
6182 exit -2
6283fi
6384
64- await_n_processes " $app_name " 2
65-
6685# === cleanup ----------------------------------------------------------------------------------------------------------
6786
6887_killall ${app_name}
0 commit comments