change wait-for-multiply.sh

This commit is contained in:
2026-03-27 16:51:12 +06:00
parent ce9e9a2d26
commit eec582c66b

View File

@@ -1,54 +1,49 @@
#!/usr/bin/env bash #!/bin/sh
# wait-for-multiple.sh # wait-for-multiple.sh (POSIX / sh version)
set -e set -e
# Список хостов передается через аргументы до -- HOSTS=""
# Команда после -- CMD=""
HOSTS=()
CMD=() while [ $# -gt 0 ]; do
while [[ $# -gt 0 ]]; do
case "$1" in case "$1" in
--) --)
shift shift
CMD=("$@") CMD="$*"
break break
;; ;;
*) *)
HOSTS+=("$1") HOSTS="$HOSTS $1"
shift shift
;; ;;
esac esac
done done
if [[ ${#HOSTS[@]} -eq 0 ]]; then if [ -z "$HOSTS" ]; then
echo "Usage: $0 host1:port1 host2:port2 ... -- command args" echo "Usage: $0 host1:port1 host2:port2 ... -- command args"
exit 1 exit 1
fi fi
# Функция для wait-for-it
wait_host() { wait_host() {
local hostport=$1 hostport="$1"
/usr/bin/wait-for-it.sh "$hostport" --timeout=30 --strict /usr/bin/wait-for-it.sh "$hostport" --timeout=30 --strict
} }
# Запускаем все ожидания параллельно PIDS=""
PIDS=() for h in $HOSTS; do
for h in "${HOSTS[@]}"; do
wait_host "$h" & wait_host "$h" &
PIDS+=($!) PIDS="$PIDS $!"
done done
# Ждем всех
FAIL=0 FAIL=0
for pid in "${PIDS[@]}"; do for pid in $PIDS; do
wait $pid || FAIL=1 wait $pid || FAIL=1
done done
if [[ $FAIL -ne 0 ]]; then if [ $FAIL -ne 0 ]; then
echo "One or more hosts failed to become available" echo "One or more hosts failed to become available"
exit 1 exit 1
fi fi
# Запускаем команду после всех exec $CMD
exec "${CMD[@]}"