diff options
author | Bryan Drewery <bryan@shatow.net> | 2014-04-04 16:17:08 +0400 |
---|---|---|
committer | Bryan Drewery <bryan@shatow.net> | 2014-04-04 16:17:08 +0400 |
commit | 056dd29cb4c21e5e76b9cc8ee4ccce496d2326b8 (patch) | |
tree | 27b3018ad736a587b745d853f4ca97a4c22ef586 | |
parent | 4cf0fe8ff3a4bb0ba1a754a5d76e6be9cb6fd3e8 (diff) |
Move nohang to parallel.sh
-rwxr-xr-x | src/share/poudriere/common.sh | 80 | ||||
-rw-r--r-- | src/share/poudriere/include/parallel.sh | 80 |
2 files changed, 80 insertions, 80 deletions
diff --git a/src/share/poudriere/common.sh b/src/share/poudriere/common.sh index 76d4fcd5..6038bd64 100755 --- a/src/share/poudriere/common.sh +++ b/src/share/poudriere/common.sh @@ -1518,86 +1518,6 @@ check_fs_violation() { return $ret } -nohang() { - [ $# -gt 5 ] || eargs nohang cmd_timeout log_timeout logfile pidfile cmd - local cmd_timeout - local log_timeout - local logfile - local pidfile - local childpid - local now starttime - local fifo - local n - local read_timeout - local ret=0 - - cmd_timeout="$1" - log_timeout="$2" - logfile="$3" - pidfile="$4" - shift 4 - - read_timeout=$((log_timeout / 10)) - - fifo=$(mktemp -ut nohang) - mkfifo ${fifo} - exec 7<> ${fifo} - rm -f ${fifo} - - starttime=$(date +%s) - - # Run the actual command in a child subshell - ( - local ret=0 - "$@" || ret=1 - # Notify the pipe the command is done - echo done >&7 2>/dev/null || : - exit $ret - ) & - childpid=$! - echo "$childpid" > ${pidfile} - - # Now wait on the cmd with a timeout on the log's mtime - while :; do - if ! kill -CHLD $childpid 2>/dev/null; then - _wait $childpid || ret=1 - break - fi - - lastupdated=$(stat -f "%m" ${logfile}) - now=$(date +%s) - - # No need to actually kill anything as stop_build() - # will be called and kill -9 -1 the jail later - if [ $((now - lastupdated)) -gt $log_timeout ]; then - ret=2 - break - elif [ $((now - starttime)) -gt $cmd_timeout ]; then - ret=3 - break - fi - - # Wait until it is done, but check on it every so often - # This is done instead of a 'sleep' as it should recognize - # the command has completed right away instead of waiting - # on the 'sleep' to finish - unset n; until trappedinfo=; read -t $read_timeout n <&7 || - [ -z "$trappedinfo" ]; do :; done - if [ "${n}" = "done" ]; then - _wait $childpid || ret=1 - break - fi - # Not done, was a timeout, check the log time - done - - exec 7<&- - exec 7>&- - - rm -f ${pidfile} - - return $ret -} - gather_distfiles() { [ $# -eq 3 ] || eargs gather_distfiles portdir from to local portdir="$1" diff --git a/src/share/poudriere/include/parallel.sh b/src/share/poudriere/include/parallel.sh index 09375ffb..05e59196 100644 --- a/src/share/poudriere/include/parallel.sh +++ b/src/share/poudriere/include/parallel.sh @@ -190,3 +190,83 @@ parallel_run() { PARALLEL_CHILD=1 parallel_exec $cmd "$@" & PARALLEL_PIDS="${PARALLEL_PIDS} $! " } + +nohang() { + [ $# -gt 5 ] || eargs nohang cmd_timeout log_timeout logfile pidfile cmd + local cmd_timeout + local log_timeout + local logfile + local pidfile + local childpid + local now starttime + local fifo + local n + local read_timeout + local ret=0 + + cmd_timeout="$1" + log_timeout="$2" + logfile="$3" + pidfile="$4" + shift 4 + + read_timeout=$((log_timeout / 10)) + + fifo=$(mktemp -ut nohang) + mkfifo ${fifo} + exec 7<> ${fifo} + rm -f ${fifo} + + starttime=$(date +%s) + + # Run the actual command in a child subshell + ( + local ret=0 + "$@" || ret=1 + # Notify the pipe the command is done + echo done >&7 2>/dev/null || : + exit $ret + ) & + childpid=$! + echo "$childpid" > ${pidfile} + + # Now wait on the cmd with a timeout on the log's mtime + while :; do + if ! kill -CHLD $childpid 2>/dev/null; then + _wait $childpid || ret=1 + break + fi + + lastupdated=$(stat -f "%m" ${logfile}) + now=$(date +%s) + + # No need to actually kill anything as stop_build() + # will be called and kill -9 -1 the jail later + if [ $((now - lastupdated)) -gt $log_timeout ]; then + ret=2 + break + elif [ $((now - starttime)) -gt $cmd_timeout ]; then + ret=3 + break + fi + + # Wait until it is done, but check on it every so often + # This is done instead of a 'sleep' as it should recognize + # the command has completed right away instead of waiting + # on the 'sleep' to finish + unset n; until trappedinfo=; read -t $read_timeout n <&7 || + [ -z "$trappedinfo" ]; do :; done + if [ "${n}" = "done" ]; then + _wait $childpid || ret=1 + break + fi + # Not done, was a timeout, check the log time + done + + exec 7<&- + exec 7>&- + + rm -f ${pidfile} + + return $ret +} |