From 96678d8156ee7bcb127bfd6dec0c2ea53b4f193c Mon Sep 17 00:00:00 2001 From: vagrant-bot Date: Thu, 27 Apr 2023 08:51:28 -0700 Subject: [PATCH] Terraform managed file --- .ci/.ci-utility-files/common.sh | 57 ++++++++++++++++++++++++++++++--- 1 file changed, 53 insertions(+), 4 deletions(-) diff --git a/.ci/.ci-utility-files/common.sh b/.ci/.ci-utility-files/common.sh index da615488e..0189968f9 100644 --- a/.ci/.ci-utility-files/common.sh +++ b/.ci/.ci-utility-files/common.sh @@ -370,7 +370,10 @@ function file_directory() { } # Wait until the number of background jobs falls below -# the maximum number provided. +# the maximum number provided. If the max number was reached +# and waiting was performed until a process completed, the +# string "waited" will be printed to stdout. +# # NOTE: using `wait -n` would be cleaner but only became # available in bash as of 4.3 # @@ -381,12 +384,58 @@ function background_jobs_limit() { failure "Maximum number of background jobs required" fi + local debug_printed local jobs - read -r -a jobs <<< "$(jobs -l)" || failure "Could not read background job list" - while [ "${#jobs[@]}" -gt "${max}" ]; do - debug "max background jobs reached (%d), waiting" + mapfile -t jobs <<< "$(jobs -p)" || + failure "Could not read background job list" + while [ "${#jobs[@]}" -ge "${max}" ]; do + if [ -z "${debug_printed}" ]; then + debug "max background jobs reached (%d), waiting for free process" "${max}" + debug_printed="1" + fi sleep 1 + jobs=() + local j_pids + mapfile -t j_pids <<< "$(jobs -p)" || + failure "Could not read background job list" + for j in "${j_pids[@]}"; do + if kill -0 "${j}" > /dev/null 2>&1; then + jobs+=( "${j}" ) + fi + done done + if [ -n "${debug_printed}" ]; then + debug "background jobs count (%s) under max, continuing" "${#jobs[@]}" + printf "waited" + fi +} + +# Reap a completed background process. If the process is +# not complete, the process is ignored. The success/failure +# returned from this function only applies to the process +# identified by the provided PID _if_ the matching PID value +# was written to stdout +# +# $1: PID +function reap_completed_background_job() { + local pid="${1}" + if [ -z "${pid}" ]; then + failure "PID of process to reap is required" + fi + if kill -0 "${pid}" > /dev/null 2>&1; then + debug "requested pid to reap (%d) has not completed, ignoring" "${pid}" + return 0 + fi + # The pid can be reaped so output the pid to indicate + # any error is from the job + printf "%s" "${pid}" + if ! wait "${pid}"; then + local code="${?}" + debug "wait error code %d returned for pid %d" "${code}" "${pid}" + return "${code}" + fi + + return 0 } # Submit given file to Apple's notarization service and