From 57d439aab95d51c55abf826441f82d8264ad6c22 Mon Sep 17 00:00:00 2001 From: Shizun Ge Date: Thu, 18 Jan 2024 18:37:50 -0800 Subject: [PATCH] [entrypoint] exclude the time spent on updating from sleep seconds. --- README.md | 2 +- src/entrypoint.sh | 18 +++++++++++------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 610fb5e..3ee24cc 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ You can configure the most behaviors of *Gantry* via environment variables. | GANTRY_NODE_NAME | | Add node name to logs. | | GANTRY_POST_RUN_CMD | | Command(s) to eval after each updating iteration. | | GANTRY_PRE_RUN_CMD | | Command(s) to eval before each updating iteration. | -| GANTRY_SLEEP_SECONDS | 0 | Sleep time between two updates. Set it to 0 to run *Gantry* once and then exit. | +| GANTRY_SLEEP_SECONDS | 0 | Interval between two updates. Set it to 0 to run *Gantry* once and then exit. Sleep time will exclude the time spent on updating services. | | TZ | | Set timezone for time in logs. | ### To login to registries diff --git a/src/entrypoint.sh b/src/entrypoint.sh index d3799c4..6e57b7a 100755 --- a/src/entrypoint.sh +++ b/src/entrypoint.sh @@ -129,22 +129,26 @@ main() { LOG_LEVEL="${GANTRY_LOG_LEVEL:-${LOG_LEVEL}}" NODE_NAME="${GANTRY_NODE_NAME:-${NODE_NAME}}" export LOG_LEVEL NODE_NAME - local SLEEP_SECONDS="${GANTRY_SLEEP_SECONDS:-0}" + local INTERVAL_SECONDS="${GANTRY_SLEEP_SECONDS:-0}" if ! is_number "${SLEEP_SECONDS}"; then log ERROR "GANTRY_SLEEP_SECONDS must be a number. Got \"${GANTRY_SLEEP_SECONDS}\"." return 1; fi local STACK="${1:-gantry}" local RETURN_VALUE=0 + local START_TIME PASSED_SECONDS SLEEP_SECONDS while true; do - # SC2034 (warning): LOG_SCOPE appears unused. Verify use (or export if used externally). - # shellcheck disable=SC2034 - LOG_SCOPE="${STACK}" + export LOG_SCOPE="${STACK}" + START_TIME=$(date +%s) gantry "${@}" RETURN_VALUE=$? - [ "${SLEEP_SECONDS}" -le 0 ] && break; - log INFO "Sleeping ${SLEEP_SECONDS} seconds before next update." - sleep "${SLEEP_SECONDS}" + [ "${INTERVAL_SECONDS}" -le 0 ] && break; + PASSED_SECONDS=$(difference_between "${START_TIME}" "$(date +%s)") + SLEEP_SECONDS=$((INTERVAL_SECONDS - PASSED_SECONDS)) + if [ "${SLEEP_SECONDS}" -gt 0 ]; then + log INFO "Sleeping ${SLEEP_SECONDS} seconds before next update." + sleep "${SLEEP_SECONDS}" + fi done return ${RETURN_VALUE} }