From ce94a54717f657dd0305fb75c3294cd36742bad6 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 10 Sep 2019 15:42:33 +0200 Subject: [run-virt] Run cleanup functions in parallel Spawns a subshell for each cleanup function, then waits for up to 4 seconds for them to complete... --- .../vmchooser_runvirt_functions.inc | 30 +++++++++++++--------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/core/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/vmchooser_runvirt_functions.inc b/core/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/vmchooser_runvirt_functions.inc index 08ab4f72..2bd8edad 100644 --- a/core/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/vmchooser_runvirt_functions.inc +++ b/core/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/vmchooser_runvirt_functions.inc @@ -127,18 +127,24 @@ cleanexit() { trap "" SIGHUP SIGINT SIGTERM EXIT writelog "Cleanexit '$1' triggered by '${BASH_SOURCE[1]}:${FUNCNAME[1]}'" usleep 250000 - while isset CLEANUP_TASKS; do - local TASK=${CLEANUP_TASKS[-1]} - unset -v CLEANUP_TASKS[-1] - if ! is_function $TASK; then - writelog "Registered cleanup function '$TASK' is not a function. This should not be..." - continue - fi - if ! ${TASK}; then - writelog "Failed to run cleanup function '$TASK'! Exit code: $RET" - fi - done - usleep 250000 + local TASK + if [ "${#CLEANUP_TASKS[@]}" -gt 0 ]; then + declare -a cleanups= + declare -a copy=( "${CLEANUP_TASKS[@]}" ) + unset CLEANUP_TASKS + for TASK in "${copy[@]}"; do + if ! is_function "$TASK"; then + writelog "Registered cleanup function '$TASK' is not a function. This should not be..." + continue + fi + "${TASK}" & + cleanups+=( "$!" ) + done + for i in 1 1 2 2 3 3 4 4; do + usleep 500000 + kill -0 "${cleanups[@]}" &> /dev/null || break + done + fi # kill potential remaining background jobs kill $(jobs -p) -- cgit v1.2.3-55-g7522