From e7bae4d2a11f043f8dcabcb14222a48d3bc37b99 Mon Sep 17 00:00:00 2001 From: Eduardo Ramos Testillano Date: Tue, 8 Dec 2015 18:49:37 +0100 Subject: [PATCH] Timeout for signal operation. Put 5 mins for clone huge programming operations --- .../launcher/resources/scripts/clone.sh | 3 +- .../resources/scripts/operation_signal.sh | 114 +++++++++++++++--- 2 files changed, 101 insertions(+), 16 deletions(-) diff --git a/example/diameter/launcher/resources/scripts/clone.sh b/example/diameter/launcher/resources/scripts/clone.sh index c9b7a66..e994e9e 100755 --- a/example/diameter/launcher/resources/scripts/clone.sh +++ b/example/diameter/launcher/resources/scripts/clone.sh @@ -216,8 +216,7 @@ do echo -n . r_file=`readlink -f $file` dn_r_file=`dirname $r_file` - #$OPERATION -f $r_file >/dev/null - $OPERATION -f $r_file > $dn_r_file/result.txt + $OPERATION -t 300 -f $r_file > $dn_r_file/result.txt res=$? if [ $res -ne 0 ] then diff --git a/example/diameter/launcher/resources/scripts/operation_signal.sh b/example/diameter/launcher/resources/scripts/operation_signal.sh index c6c15be..3c928ce 100755 --- a/example/diameter/launcher/resources/scripts/operation_signal.sh +++ b/example/diameter/launcher/resources/scripts/operation_signal.sh @@ -1,13 +1,84 @@ #!/bin/bash +############# +# VARIABLES # +############# +TIMEOUT__dflt=3 +SCR_BN=`basename $0` + ############# # FUNCTIONS # ############# -_exit () { +_exit() { echo -e "\n$1\n" exit 1 } +usage() { + echo + echo "Usage: $0 [-h|--help] [-t|--timeout ] [-f|--file] " + echo + echo " -h|--help: this usage help." + echo " -t|--timeout: timeout for operation in seconds." + echo " Defaults to 2 seconds if not provided." + echo + echo " -f|--file: the parameter 'data' will be interpreted as a file" + echo " with one operation per line. If missing, it will be" + echo " a single operation string." + echo + echo " data: operation string or file with several operations." + echo + echo " For example:" + echo " $0 help" + echo " $0 \"test|ttps|50\"" + echo " $0 --file myOperationsList.txt" + echo " $0 --timeout 10 --file ./bigList.txt" + _exit +} + +parse_arguments() { + is_file= + timeout=$TIMEOUT__dflt + data= + + while [ $# -gt 0 ]; do + case $1 in + -h|--help) + usage + ;; + + -t|--timeout) + timeout=$2 + [ -z "$timeout" ] && _exit "Missing timeout value" + shift + ;; + + -f|--file) + is_file=yes + data=$2 + [ -z "$data" ] && _exit "Missing file" + [ ! -f "$data" ] && _exit "Can't found provided file '$data'." + shift + ;; + + *) + first=$(echo $1 | cut -c1) + [ "$first" = "-" ] && _exit "Unsupported script option: $1. Type '$SCR_BN -h' (or --help) to print the available options." + data=$1 + ;; + esac + shift + done + + [ -z "$data" ] && _exit "Missing data value" +} + +# $1: pid to check +check_pid() { + kill -0 $1 2>/dev/null + [ $? -ne 0 ] && _exit "Operation error: missing process with pid $1" +} + ############# # EXECUTION # ############# @@ -16,27 +87,42 @@ cd `dirname $0` [ ! -f .pid ] && _exit "Can't found '`pwd`/.pid'.\nTry to pgrep your process name and dump pid to that file." PID=`cat .pid` -# Send operation: -[ "$1" = "" ] && _exit "Usage: $0 [-f] ; i.e.: $0 help, $0 -f myOperationsList.txt" -FILE= -[ "$1" = "-f" ] && FILE=$2 +# Arguments: +[ "$1" = "" -o "$1" = "--help" -o "$1" = "-h" ] && usage +parse_arguments $@ -if [ -z "$FILE" ] +# Send operation: +if [ -n "$is_file" ] then - echo $1 > sigusr2.in + grep -v "^#" $data | sed '/^[ \t]*$/d' > sigusr2.in else - [ ! -f "$FILE" ] && _exit "Can't found provided file '$FILE'." - grep -v "^#" $FILE | sed '/^[ \t]*$/d' > sigusr2.in + echo $data > sigusr2.in fi 0> sigusr2.out -kill -0 $PID 2>/dev/null -[ $? -ne 0 ] && _exit "Operation error: missing process with pid $PID" +check_pid $PID kill -s SIGUSR2 $PID # Detect EOF and print all except that last line: -while [ -z "$(tail -1 sigusr2.out | grep ^EOF)" ]; do sleep 0.1; done -head --lines=-1 sigusr2.out +sleep $timeout & +timerPid=$! +rm -f .operation_eof +tail -n0 -F --pid=$timerPid sigusr2.out | while read line +do + if echo $line | grep "^EOF" >/dev/null; then + touch .operation_eof + # stop the timer + kill -13 $timerPid + fi +done + +if [ -f .operation_eof ] +then + head --lines=-1 sigusr2.out +else + _exit "Operation error: timeout expired ($timeout seconds)" +fi exception=$(grep exception sigusr2.out) -[ $? -eq 0 ] && _exit "(detected 'exception' within operation output)" +[ $? -eq 0 -a "$data" != "help" ] && _exit "(detected 'exception' within operation output)" exit 0 + -- 2.20.1