Now when killed with (SIGINT, SIGTERM, SIGHUP), all will shut down (bound input/output + daemons ...)
This commit is contained in:
parent
432c7d8364
commit
3a94d8c357
40
master
40
master
|
@ -51,26 +51,28 @@ on_exit(){
|
|||
for logger_name in "${!OUTPUT_PID[@]}"; do
|
||||
|
||||
# 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;
|
||||
|
||||
for logger_name in "${!LOGGER_PID[@]}"; do
|
||||
|
||||
# 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;
|
||||
|
||||
echo "killing 'master' output socket";
|
||||
kill -9 $MASTER_PID 2>/dev/null;
|
||||
kill -INT $MASTER_PID 2>/dev/null;
|
||||
|
||||
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 #
|
||||
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=$?;
|
||||
|
||||
|
||||
|
@ -90,6 +92,7 @@ MASTER_PID=$?;
|
|||
PORT="$MIN_PORT";
|
||||
for logger_name in "${LOGGERS[@]}"; do
|
||||
|
||||
echo "(1)";
|
||||
# Find an available port
|
||||
while true; do
|
||||
|
||||
|
@ -102,28 +105,35 @@ for logger_name in "${LOGGERS[@]}"; do
|
|||
done;
|
||||
|
||||
# send logger port request
|
||||
echo "(2)";
|
||||
echo " (.) Sending logger@$logger_name request";
|
||||
$ROOT/write master "$POOL_KEY:$logger_name:$LOCAL_IP:$PORT";
|
||||
echo "(3)";
|
||||
|
||||
# check pool response
|
||||
RESP="`nc -w 5 -lp $PORT`";
|
||||
RESP="`nc -w 2 -lp $PORT`";
|
||||
echo "(4)";
|
||||
|
||||
# empty response -> ignore
|
||||
test -z "$RESP" && continue;
|
||||
echo "(5)";
|
||||
|
||||
# not a number -> ignore
|
||||
echo -n "$RESP" | grep -P '^\d+$' >/dev/null || continue;
|
||||
echo "(6)";
|
||||
|
||||
|
||||
# Create output bound
|
||||
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]=$!;
|
||||
echo "(7)";
|
||||
|
||||
echo " (.) launching logger@$logger_name";
|
||||
# launch logger bound to output
|
||||
( $LOGGER_DIR/$logger_name | cat > /tmp/outbuf_master_$logger_name )&
|
||||
LOGGER_PID[$logger_name]=$!;
|
||||
echo "(8)";
|
||||
|
||||
|
||||
|
||||
|
@ -134,4 +144,16 @@ done;
|
|||
|
||||
|
||||
# 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
62
pool
|
@ -21,44 +21,39 @@ POOL_PORT="$2";
|
|||
# (5) init logger variables #
|
||||
declare -A INPUT_PID; # will contain each bind-input pid
|
||||
|
||||
|
||||
|
||||
|
||||
# (2) Set exit management
|
||||
#--------------------------------------------------------#
|
||||
# (1) On-Exit routine #
|
||||
# (6) Define on-exit routine #
|
||||
LOOP_PID="";
|
||||
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
|
||||
|
||||
# kill each logger input
|
||||
kill -9 ${INPUT_PID[$logger_name]} 2>/dev/null;
|
||||
$INPUT_PID[$logger_name]="";
|
||||
echo ".killing input@$logger_name (${INPUT_PID[$logger_name]})";
|
||||
test ! -z "${INPUT_PID[$logger_name]}" && kill -INT ${INPUT_PID[$logger_name]} 2>/dev/null;
|
||||
|
||||
done;
|
||||
|
||||
echo "killing 'pool' input listener";
|
||||
kill -9 $POOL_PID 2>/dev/null;
|
||||
echo "killing 'pool' input listener ($POOL_PID)";
|
||||
kill -INT $POOL_PID 2>/dev/null;
|
||||
|
||||
echo "killing 'pool' loop";
|
||||
kill -9 $LOOP_PID 2>/dev/null;
|
||||
exit 1;
|
||||
|
||||
}
|
||||
|
||||
trap "on_exit;" INT KILL;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# (3) Launch pool listener
|
||||
# (2) Launch pool listener
|
||||
#--------------------------------------------------------#
|
||||
# (1) Bind-input #
|
||||
echo "(.) listen $POOL_PORT";
|
||||
$ROOT/bind-input pool $POOL_PORT&
|
||||
POOL_PID=$?;
|
||||
( $ROOT/bind-input pool $POOL_PORT )&
|
||||
POOL_PID=$!;
|
||||
|
||||
|
||||
|
||||
|
@ -66,14 +61,18 @@ POOL_PID=$?;
|
|||
#--------------------------------------------------------#
|
||||
# infinite listener
|
||||
PORT="$MIN_PORT";
|
||||
while sleep 1; do
|
||||
(
|
||||
|
||||
trap "on_exit;" HUP INT KILL TERM;
|
||||
|
||||
while true; do
|
||||
|
||||
# Listen on port @POOL_PORT
|
||||
MSG=`$ROOT/read pool`;
|
||||
|
||||
# Do nothing if empty msg or invalid format
|
||||
test -z "$MSG" && continue;
|
||||
echo -n "$MSG" | grep -vP '^([^:]+):([^:]+):([^:]+):([^:]+)$' >/dev/null && echo ' /!\ invalid format' && continue;
|
||||
test -z "$MSG" && sleep .5 && continue;
|
||||
echo -n "$MSG" | grep -vP '^([^:]+):([^:]+):([^:]+):([^:]+)$' >/dev/null && echo ' /!\ invalid format' && sleep .5 && continue;
|
||||
|
||||
# Extract ID - PORT
|
||||
KEY="`echo -ne \"$MSG\" | sed 's/^\(.\+\):\(.\+\):\(.\+\):\(.\+\)$/\1/'`";
|
||||
|
@ -82,7 +81,7 @@ while sleep 1; do
|
|||
LOGGER_PORT="`echo -ne \"$MSG\" | sed 's/^\(.\+\):\(.\+\):\(.\+\):\(.\+\)$/\4/'`";
|
||||
|
||||
# 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
|
||||
echo " (.) binding input for logger@$LOGGER_NAME ($LOGGER_HOST:$LOGGER_PORT)";
|
||||
|
@ -103,21 +102,26 @@ while sleep 1; do
|
|||
|
||||
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
|
||||
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
|
||||
INPUT_PID[LOGGER_NAME]=$!;
|
||||
INPUT_PID["$LOGGER_NAME"]=$!;
|
||||
|
||||
# Notify that input is active to MASTER
|
||||
bash -c "exec 4<>/dev/tcp/$LOGGER_HOST/$LOGGER_PORT; echo -n \"$PORT\" >&4;";
|
||||
|
||||
done&
|
||||
done;
|
||||
|
||||
)&
|
||||
LOOP_PID=$!;
|
||||
|
||||
# (3) Set exit management
|
||||
#--------------------------------------------------------#
|
||||
trap "kill -INT $LOOP_PID;" HUP INT KILL TERM;
|
||||
|
||||
wait $LOOP_PID;
|
||||
|
||||
|
|
Loading…
Reference in New Issue