Skip redundant restart for containers docker_update.sh already rebuilt

docker_update.sh rebuilds (stop+recreate) any container whose image
changed, in every mode — but for daily/weekly that was always followed
by the restart script's own unconditional pass, stopping and starting
the same container twice back to back. docker_update.sh now records
which containers it rebuilt this run to a file; docker_daily_restart.sh
and docker_weekly_restart.sh read it and skip those specifically,
still restarting everything else as before. A file older than
DOCKER_UPDATE_REBUILT_STALE_HOURS (default 12) is discarded rather
than trusted, so a missed or failed update run can't suppress a
restart indefinitely.
This commit is contained in:
Gmer4Lfe
2026-07-19 16:09:55 -04:00
parent 7252d4aad3
commit 9425d16c19
4 changed files with 110 additions and 3 deletions
+39 -2
View File
@@ -87,6 +87,11 @@
# Gives the process time to initialise before verify_running samples the state.
# (default: 3)
#
# DOCKER_UPDATE_REBUILT_DAILY_FILE / DOCKER_UPDATE_REBUILT_STALE_HOURS
# List of containers docker_update.sh already rebuilt onto a new image this run —
# read here so they're not restarted a second time. Discarded as stale (and every
# container restarts normally) if older than DOCKER_UPDATE_REBUILT_STALE_HOURS.
#
# ==============================================================================================
# RUNTIME MODES
# ==============================================================================================
@@ -175,10 +180,34 @@ START=$(date +%s)
FAILED=()
RESTARTED=()
SKIPPED=()
ALREADY_UPDATED=()
# Build dependency-safe restart order
build_restart_order DAILY_RESTART_CONTAINERS
# ── Load containers docker_update.sh already rebuilt this run ───────────────────────────────────
# docker_update.sh's rebuild (stop+recreate onto a new image) already restarted anything whose
# image changed today — doing a plain restart on it again here is redundant. A file older than
# DOCKER_UPDATE_REBUILT_STALE_HOURS means docker_update.sh either didn't run today or this is way
# out of sync with it, so it's discarded rather than trusted, and every container restarts as
# normal — same as if the file had never existed.
declare -A ALREADY_REBUILT_MAP
if [[ -n "${DOCKER_UPDATE_REBUILT_DAILY_FILE:-}" && -f "$DOCKER_UPDATE_REBUILT_DAILY_FILE" ]]; then
_rebuilt_age=$(( $(date +%s) - $(stat -c %Y "$DOCKER_UPDATE_REBUILT_DAILY_FILE" 2>/dev/null || echo 0) ))
_rebuilt_stale_seconds=$(( ${DOCKER_UPDATE_REBUILT_STALE_HOURS:-12} * 3600 ))
if [[ "$_rebuilt_age" -gt "$_rebuilt_stale_seconds" ]]; then
warn "Rebuilt-container list is stale ($(( _rebuilt_age / 3600 ))h old) — discarding, restarting all"
rm -f "$DOCKER_UPDATE_REBUILT_DAILY_FILE"
else
while IFS= read -r _c; do
[[ -n "$_c" ]] && ALREADY_REBUILT_MAP["$_c"]=1
done < "$DOCKER_UPDATE_REBUILT_DAILY_FILE"
[[ "${#ALREADY_REBUILT_MAP[@]}" -gt 0 ]] && \
log "Already rebuilt today by docker_update.sh, skipping restart: ${!ALREADY_REBUILT_MAP[*]}"
fi
unset _rebuilt_age _rebuilt_stale_seconds
fi
LAST_RESTARTED=""
for container in "${ORDERED_RESTART[@]}"; do
@@ -196,6 +225,13 @@ for container in "${ORDERED_RESTART[@]}"; do
case "$STATUS" in
true)
if [[ -n "${ALREADY_REBUILT_MAP[$container]:-}" ]]; then
log "$ICON_RUNNING $container already rebuilt onto new image by docker_update.sh — skipping redundant restart"
ALREADY_UPDATED+=("$container")
LAST_RESTARTED="$container" # it did restart, just moments ago via the rebuild
continue
fi
log "$ICON_RUNNING $container is running — restarting..."
# Wait if this container depends on the last one restarted
@@ -258,8 +294,9 @@ fi
echo "━━━━━ $ICON_SUMMARY DAILY RESTART SUMMARY ━━━━━"
echo "$ICON_HOST Identity: $MY_ID ($LOCAL_SERVER_NAME)"
echo "$ICON_TIME Duration: $(format_duration $((END - START)))"
echo "$ICON_CONTAINERS Scope: ${#RESTARTED[@]} restarted, ${#SKIPPED[@]} skipped, ${#FAILED[@]} failed"
echo "$ICON_CONTAINERS Scope: ${#RESTARTED[@]} restarted, ${#ALREADY_UPDATED[@]} already updated, ${#SKIPPED[@]} skipped, ${#FAILED[@]} failed"
[[ ${#RESTARTED[@]} -gt 0 ]] && log "$ICON_STARTED Restarted: ${RESTARTED[*]}"
[[ ${#ALREADY_UPDATED[@]} -gt 0 ]] && log "$ICON_DONE Already updated (skipped): ${ALREADY_UPDATED[*]}"
[[ ${#SKIPPED[@]} -gt 0 ]] && log "$ICON_NOT_RUNNING Skipped: ${SKIPPED[*]}"
[[ ${#FAILED[@]} -gt 0 ]] && echo "$ICON_ERROR Failed: ${FAILED[*]}"
echo "$ICON_SYNC Pruned: ${PRUNED_SUMMARY:-none}"
@@ -268,7 +305,7 @@ if [[ "$DRY_RUN" == true ]]; then
warn "DRY RUN — no changes made"
elif [[ ${#FAILED[@]} -eq 0 ]]; then
echo "$ICON_DONE Status: ALL DONE ✅"
notify "Daily restart complete — ${#RESTARTED[@]} restarted, ${#SKIPPED[@]} skipped on $(hostname)" "Docker Daily Restart" "normal"
notify "Daily restart complete — ${#RESTARTED[@]} restarted, ${#ALREADY_UPDATED[@]} already updated, ${#SKIPPED[@]} skipped on $(hostname)" "Docker Daily Restart" "normal"
else
echo "$ICON_ERROR Status: ${#FAILED[@]} container(s) failed"
notify "Daily restart completed with errors on $(hostname) — failed: ${FAILED[*]}" "Docker Daily Restart" "warning"