Skip to content

Commit

Permalink
upgrade-2.x: move error reporting to exit handler
Browse files Browse the repository at this point in the history
By using the exit handler and checking for the error code the script
will always attempt to report a failed update.

Change-type: patch
Signed-off-by: Alex Gonzalez <[email protected]>
  • Loading branch information
alexgg committed Apr 26, 2024
1 parent 4ba5f95 commit 2ce06a4
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions upgrade-2.x.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,22 @@ LOCKFILE="/var/lock/resinhup.lock"
LOCKFD=99
## Private functions
_lock() { flock "-$1" $LOCKFD; }
_exit_handler() {
_exit_status=$?
if [ "${_exit_status}" -ne 0 ]; then
log "Exit on error ${_exit_status}"
if ! report_update_failed > /dev/null 2>&1; then
log "Failed to report progress on exit with status $?"
fi
if [ "${_exit_status}" -eq 9 ]; then
log "No concurrent updates allowed - lock file in place."
fi
fi
_no_more_locking
log "Lock removed - end."
}
_no_more_locking() { _lock u; _lock xn && rm -f $LOCKFILE;rm -f "${outfifo}";rm -f "${errfifo}"; }
_prepare_locking() { eval "exec $LOCKFD>\"$LOCKFILE\""; trap _no_more_locking EXIT; }
_prepare_locking() { eval "exec $LOCKFD>\"$LOCKFILE\""; trap _exit_handler EXIT; }
# Public functions
exlock_now() { _lock xn; } # obtain an exclusive lock immediately or fail

Expand Down Expand Up @@ -121,6 +135,7 @@ function report_update_failed() {
if resin-device-progress --percentage "${perc}" --state "${state}"; then
continue
fi
log WARN "Retrying failure report - try $c"
sleep 60
done
}
Expand Down Expand Up @@ -148,7 +163,6 @@ function log {
endtime=$(date +%s)
if [ "$loglevel" == "ERROR" ]; then
printf "[%09d%s%s\n" "$((endtime - starttime))" "][$loglevel]" "$1" >> /dev/stderr
report_update_failed
exit 1
else
printf "[%09d%s%s\n" "$((endtime - starttime))" "][$loglevel]" "$1"
Expand Down Expand Up @@ -1103,7 +1117,21 @@ DELTA_ENDPOINT=$(jq -r '.deltaEndpoint' $CONFIGJSON)
[ -z "${API_ENDPOINT}" ] && log "Error parsing config.json" && exit 1
[ -z "${DELTA_ENDPOINT}" ] && log "Error parsing config.json" && exit 1

trap 'report_update_failed;exit 1' ERR INT TERM
_err_handler(){
log ERROR "Interrupted on error"
}

_int_handler(){
log ERROR "Interrupted"
}

_term_handler(){
log ERROR "Terminated"
}

trap '_err_handler' ERR
trap '_int_handler' INT
trap '_term_handler' TERM

# redirect all logs to the logfile, but also stderr to console (proxy)
outfifo=$(mktemp -u)
Expand Down

0 comments on commit 2ce06a4

Please sign in to comment.