Skip to content

Commit

Permalink
[entrypoint] exclude the time spent on updating from sleep seconds.
Browse files Browse the repository at this point in the history
  • Loading branch information
shizunge committed Jan 19, 2024
1 parent e205d06 commit d678ce0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 12 additions & 8 deletions src/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
if ! is_number "${SLEEP_SECONDS}"; then
local INTERVAL_SECONDS="${GANTRY_SLEEP_SECONDS:-0}"
if ! is_number "${INTERVAL_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}
}
Expand Down

0 comments on commit d678ce0

Please sign in to comment.