2023-05-09 04:31:22 +00:00
|
|
|
#! /usr/bin/env bash
|
|
|
|
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
|
|
|
|
USER_HOME=$HOME
|
|
|
|
[[ -n "${SUDO_USER}" ]] && USER_HOME="$(eval "echo ~${SUDO_USER}")"
|
2023-07-12 03:17:21 +00:00
|
|
|
. ${SHRC_D:-$SCRIPT_DIR}/01_util.functions
|
2023-05-09 04:31:22 +00:00
|
|
|
|
|
|
|
set -e
|
|
|
|
|
|
|
|
count_unfinished_jobs() {
|
|
|
|
midclt call core.get_jobs | jq -r '.[] | select( .time_finished == null )' | wc -l
|
|
|
|
}
|
|
|
|
|
|
|
|
wait_for_idle_queue() {
|
2023-06-01 02:56:18 +00:00
|
|
|
local TIMEOUT="${1:-${TIMEOUT:-120}}" START="$(date +%s)"
|
2023-05-09 04:31:22 +00:00
|
|
|
while [[ "$( count_unfinished_jobs )" -gt 0 ]]; do
|
|
|
|
printf '.'
|
2023-06-01 02:56:18 +00:00
|
|
|
|
|
|
|
if (( TIMEOUT > 0 && ($(date +%s) - START) > TIMEOUT )); then
|
|
|
|
printf 'TIMEOUT %s reached.\n' "$TIMEOUT" >&2
|
|
|
|
printf '\n'
|
|
|
|
return 1
|
|
|
|
break;
|
|
|
|
fi
|
|
|
|
|
2023-05-09 04:31:22 +00:00
|
|
|
sleep 2
|
|
|
|
done
|
|
|
|
printf '\n'
|
|
|
|
}
|
|
|
|
|
2023-06-01 02:56:18 +00:00
|
|
|
abort_unfinished_jobs() {
|
|
|
|
midclt call core.get_jobs \
|
|
|
|
| jq -r '.[] | select( .time_finished == null ) | .id ' \
|
|
|
|
| xargs -rn1 midclt call core.job_abort
|
|
|
|
}
|
|
|
|
|
|
|
|
wait_for_idle_queue || if [[ -z "$NO_AUTO_ABORT" ]]; then
|
|
|
|
abort_unfinished_jobs
|
|
|
|
fi
|
|
|
|
|