Now when killed with (SIGINT, SIGTERM, SIGHUP), all will shut down (bound input/output + daemons ...)

This commit is contained in:
xdrm-brackets 2018-01-31 20:55:11 +01:00
parent 432c7d8364
commit 3a94d8c357
2 changed files with 95 additions and 69 deletions

40
master
View File

@ -51,26 +51,28 @@ on_exit(){
for logger_name in "${!OUTPUT_PID[@]}"; do for logger_name in "${!OUTPUT_PID[@]}"; do
# kill each logger OUTPUT # kill each logger OUTPUT
kill -9 ${OUTPUT_PID[$logger_name]} 2>/dev/null; echo " > killing output@$logger_name";
kill -INT ${OUTPUT_PID[$logger_name]} 2>/dev/null;
done; done;
for logger_name in "${!LOGGER_PID[@]}"; do for logger_name in "${!LOGGER_PID[@]}"; do
# kill each logger OUTPUT # kill each logger OUTPUT
kill -9 ${LOGGER_PID[$logger_name]} 2>/dev/null; echo " > killing logger@$logger_name";
kill -INT ${LOGGER_PID[$logger_name]} 2>/dev/null;
done; done;
echo "killing 'master' output socket"; echo "killing 'master' output socket";
kill -9 $MASTER_PID 2>/dev/null; kill -INT $MASTER_PID 2>/dev/null;
echo "killing 'master' loop"; echo "killing 'master' loop";
# kill -9 $LOOP_PID 2>/dev/null; # kill -INT $LOOP_PID 2>/dev/null;
} }
trap "on_exit;" INT KILL; trap "on_exit;" HUP INT KILL TERM;
@ -80,7 +82,7 @@ trap "on_exit;" INT KILL;
#--------------------------------------------------------# #--------------------------------------------------------#
# (1) Bind-output # # (1) Bind-output #
echo "(.) connect to $POOL_HOST:$POOL_PORT"; echo "(.) connect to $POOL_HOST:$POOL_PORT";
$ROOT/bind-output master $POOL_HOST $POOL_PORT& ( $ROOT/bind-output master $POOL_HOST $POOL_PORT )&
MASTER_PID=$?; MASTER_PID=$?;
@ -90,6 +92,7 @@ MASTER_PID=$?;
PORT="$MIN_PORT"; PORT="$MIN_PORT";
for logger_name in "${LOGGERS[@]}"; do for logger_name in "${LOGGERS[@]}"; do
echo "(1)";
# Find an available port # Find an available port
while true; do while true; do
@ -102,28 +105,35 @@ for logger_name in "${LOGGERS[@]}"; do
done; done;
# send logger port request # send logger port request
echo "(2)";
echo " (.) Sending logger@$logger_name request"; echo " (.) Sending logger@$logger_name request";
$ROOT/write master "$POOL_KEY:$logger_name:$LOCAL_IP:$PORT"; $ROOT/write master "$POOL_KEY:$logger_name:$LOCAL_IP:$PORT";
echo "(3)";
# check pool response # check pool response
RESP="`nc -w 5 -lp $PORT`"; RESP="`nc -w 2 -lp $PORT`";
echo "(4)";
# empty response -> ignore # empty response -> ignore
test -z "$RESP" && continue; test -z "$RESP" && continue;
echo "(5)";
# not a number -> ignore # not a number -> ignore
echo -n "$RESP" | grep -P '^\d+$' >/dev/null || continue; echo -n "$RESP" | grep -P '^\d+$' >/dev/null || continue;
echo "(6)";
# Create output bound # Create output bound
echo " (.) Connecting '$logger_name' to $POOL_HOST:$RESP"; echo " (.) Connecting '$logger_name' to $POOL_HOST:$RESP";
$ROOT/bind-output master_$logger_name $POOL_HOST $RESP& ( $ROOT/bind-output master_$logger_name $POOL_HOST $RESP )&
OUTPUT_PID[$logger_name]=$!; OUTPUT_PID[$logger_name]=$!;
echo "(7)";
echo " (.) launching logger@$logger_name"; echo " (.) launching logger@$logger_name";
# launch logger bound to output # launch logger bound to output
( $LOGGER_DIR/$logger_name | cat > /tmp/outbuf_master_$logger_name )& ( $LOGGER_DIR/$logger_name | cat > /tmp/outbuf_master_$logger_name )&
LOGGER_PID[$logger_name]=$!; LOGGER_PID[$logger_name]=$!;
echo "(8)";
@ -134,4 +144,16 @@ done;
# When all loggers are bound, destroy pool connection # When all loggers are bound, destroy pool connection
kill -9 $MASTER_PID 2>/dev/null; # kill -INT $MASTER_PID 2>/dev/null;
echo "waiting for ${#LOGGER_PID[@]} + ${#OUTPUT_PID[@]} ..";
for logger_name in "${!LOGGER_PID[@]}"; do
echo "waiting for $logger_name";
wait ${LOGGER_PID[$logger_name]};
echo "done with $logger_name";
done;
# on_exit;

62
pool
View File

@ -21,44 +21,39 @@ POOL_PORT="$2";
# (5) init logger variables # # (5) init logger variables #
declare -A INPUT_PID; # will contain each bind-input pid declare -A INPUT_PID; # will contain each bind-input pid
# (6) Define on-exit routine #
LOOP_PID="";
# (2) Set exit management
#--------------------------------------------------------#
# (1) On-Exit routine #
on_exit(){ on_exit(){
echo "killing 'bind-input' sub processes";
echo "killing 'pool' loop";
kill -INT $LOOP_PID 2>/dev/null;
echo "killing ${#INPUT_PID[@]} background 'bind-input'";
for logger_name in "${!INPUT_PID[@]}"; do for logger_name in "${!INPUT_PID[@]}"; do
# kill each logger input # kill each logger input
kill -9 ${INPUT_PID[$logger_name]} 2>/dev/null; echo ".killing input@$logger_name (${INPUT_PID[$logger_name]})";
$INPUT_PID[$logger_name]=""; test ! -z "${INPUT_PID[$logger_name]}" && kill -INT ${INPUT_PID[$logger_name]} 2>/dev/null;
done; done;
echo "killing 'pool' input listener"; echo "killing 'pool' input listener ($POOL_PID)";
kill -9 $POOL_PID 2>/dev/null; kill -INT $POOL_PID 2>/dev/null;
echo "killing 'pool' loop"; exit 1;
kill -9 $LOOP_PID 2>/dev/null;
} }
trap "on_exit;" INT KILL;
# (2) Launch pool listener
# (3) Launch pool listener
#--------------------------------------------------------# #--------------------------------------------------------#
# (1) Bind-input # # (1) Bind-input #
echo "(.) listen $POOL_PORT"; echo "(.) listen $POOL_PORT";
$ROOT/bind-input pool $POOL_PORT& ( $ROOT/bind-input pool $POOL_PORT )&
POOL_PID=$?; POOL_PID=$!;
@ -66,14 +61,18 @@ POOL_PID=$?;
#--------------------------------------------------------# #--------------------------------------------------------#
# infinite listener # infinite listener
PORT="$MIN_PORT"; PORT="$MIN_PORT";
while sleep 1; do (
trap "on_exit;" HUP INT KILL TERM;
while true; do
# Listen on port @POOL_PORT # Listen on port @POOL_PORT
MSG=`$ROOT/read pool`; MSG=`$ROOT/read pool`;
# Do nothing if empty msg or invalid format # Do nothing if empty msg or invalid format
test -z "$MSG" && continue; test -z "$MSG" && sleep .5 && continue;
echo -n "$MSG" | grep -vP '^([^:]+):([^:]+):([^:]+):([^:]+)$' >/dev/null && echo ' /!\ invalid format' && continue; echo -n "$MSG" | grep -vP '^([^:]+):([^:]+):([^:]+):([^:]+)$' >/dev/null && echo ' /!\ invalid format' && sleep .5 && continue;
# Extract ID - PORT # Extract ID - PORT
KEY="`echo -ne \"$MSG\" | sed 's/^\(.\+\):\(.\+\):\(.\+\):\(.\+\)$/\1/'`"; KEY="`echo -ne \"$MSG\" | sed 's/^\(.\+\):\(.\+\):\(.\+\):\(.\+\)$/\1/'`";
@ -82,7 +81,7 @@ while sleep 1; do
LOGGER_PORT="`echo -ne \"$MSG\" | sed 's/^\(.\+\):\(.\+\):\(.\+\):\(.\+\)$/\4/'`"; LOGGER_PORT="`echo -ne \"$MSG\" | sed 's/^\(.\+\):\(.\+\):\(.\+\):\(.\+\)$/\4/'`";
# if ID does not match # if ID does not match
test "$KEY" != "$POOL_KEY" && echo ' /!\ wrong id' && continue; test "$KEY" != "$POOL_KEY" && echo ' /!\ wrong id' && sleep .5 && continue;
# create new listening socket # create new listening socket
echo " (.) binding input for logger@$LOGGER_NAME ($LOGGER_HOST:$LOGGER_PORT)"; echo " (.) binding input for logger@$LOGGER_NAME ($LOGGER_HOST:$LOGGER_PORT)";
@ -103,21 +102,26 @@ while sleep 1; do
done; done;
# Bind-input for the logger at the free port found
$ROOT/bind-input $LOGGER_NAME $PORT&
# if already an input for this logger -> kill it # if already an input for this logger -> kill it
test ! -z "${INPUT_PID[$LOGGER_NAME]}" && kill -9 ${INPUT_PID[$LOGGER_NAME]}; test ! -z "${INPUT_PID[$LOGGER_NAME]}" && kill -INT ${INPUT_PID[$LOGGER_NAME]};
# Bind-input for the logger at the free port found
( $ROOT/bind-input $LOGGER_NAME $PORT )&
# Store bind-input PID # Store bind-input PID
INPUT_PID[LOGGER_NAME]=$!; INPUT_PID["$LOGGER_NAME"]=$!;
# Notify that input is active to MASTER # Notify that input is active to MASTER
bash -c "exec 4<>/dev/tcp/$LOGGER_HOST/$LOGGER_PORT; echo -n \"$PORT\" >&4;"; bash -c "exec 4<>/dev/tcp/$LOGGER_HOST/$LOGGER_PORT; echo -n \"$PORT\" >&4;";
done& done;
)&
LOOP_PID=$!; LOOP_PID=$!;
# (3) Set exit management
#--------------------------------------------------------#
trap "kill -INT $LOOP_PID;" HUP INT KILL TERM;
wait $LOOP_PID; wait $LOOP_PID;