scripts: improve panic helpers (#3155)

This commit is contained in:
Georg Lauterbach 2023-03-06 10:06:50 +01:00 committed by GitHub
parent b5fc40eb7a
commit dab70709d9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 53 additions and 34 deletions

View file

@ -22,9 +22,10 @@ function _exit_with_error
# PANIC_SCOPE => Optionally provide a string for debugging to better identify/locate the source of the panic.
function dms_panic
{
local PANIC_TYPE=${1}
local PANIC_INFO=${2}
local PANIC_SCOPE=${3} #optional
local PANIC_TYPE=${1:-}
local PANIC_INFO=${2:-}
local PANIC_SCOPE=${3-} # optional, must not be :- but just -
local PANIC_STRATEGY=${4:-} # optional
local SHUTDOWN_MESSAGE
@ -49,6 +50,10 @@ function dms_panic
SHUTDOWN_MESSAGE="Invalid value for ${PANIC_INFO}!"
;;
( 'general' )
SHUTDOWN_MESSAGE=${PANIC_INFO}
;;
( * ) # `dms_panic` was called directly without a valid PANIC_TYPE
SHUTDOWN_MESSAGE='Something broke :('
;;
@ -56,27 +61,38 @@ function dms_panic
if [[ -n ${PANIC_SCOPE:-} ]]
then
_shutdown "${PANIC_SCOPE} | ${SHUTDOWN_MESSAGE}"
_shutdown "${PANIC_SCOPE} | ${SHUTDOWN_MESSAGE}" "${PANIC_STRATEGY}"
else
_shutdown "${SHUTDOWN_MESSAGE}"
_shutdown "${SHUTDOWN_MESSAGE}" "${PANIC_STRATEGY}"
fi
}
# Convenience wrappers based on type:
function dms_panic__fail_init { dms_panic 'fail-init' "${1}" "${2}"; }
function dms_panic__no_env { dms_panic 'no-env' "${1}" "${2}"; }
function dms_panic__no_file { dms_panic 'no-file' "${1}" "${2}"; }
function dms_panic__misconfigured { dms_panic 'misconfigured' "${1}" "${2}"; }
function dms_panic__invalid_value { dms_panic 'invalid-value' "${1}" "${2}"; }
function _dms_panic__fail_init { dms_panic 'fail-init' "${1:-}" "${2:-}" "${3:-}" ; }
function _dms_panic__no_env { dms_panic 'no-env' "${1:-}" "${2:-}" "${3:-}" ; }
function _dms_panic__no_file { dms_panic 'no-file' "${1:-}" "${2:-}" "${3:-}" ; }
function _dms_panic__misconfigured { dms_panic 'misconfigured' "${1:-}" "${2:-}" "${3:-}" ; }
function _dms_panic__invalid_value { dms_panic 'invalid-value' "${1:-}" "${2:-}" "${3:-}" ; }
function _dms_panic__general { dms_panic 'general' "${1:-}" "${2:-}" "${3:-}" ; }
# Call this method when you want to panic (i.e. emit an 'ERROR' log, and exit uncleanly).
# `dms_panic` methods should be preferred if your failure type is supported.
function _shutdown
{
_log 'error' "${1}"
_log 'error' "${1:-_shutdown called without message}"
_log 'error' 'Shutting down'
sleep 1
kill 1
exit 1
if [[ ${2:-wait} == 'immediate' ]]
then
# In case the user requested an immediate exit, he ensure he is not in a subshell
# call and exiting the whole script is safe. This way, we make the shutdown quicker.
exit 1
else
# We can simply wait until Supervisord has terminated all processes; this way,
# we do not return from a subshell call and continue as if nothing happened.
sleep 1000
fi
}