exit code and failure states rework
Signed-off-by: fbt <fbt@fleshless.org>
This commit is contained in:
parent
7a3bb1a5f0
commit
623bf57bdb
41
ssm
41
ssm
|
@ -9,7 +9,7 @@ usage() {
|
||||||
cat <<- EOF
|
cat <<- EOF
|
||||||
Usage: ssm <service> <function>
|
Usage: ssm <service> <function>
|
||||||
EOF
|
EOF
|
||||||
}
|
}; readonly usage;
|
||||||
|
|
||||||
var() {
|
var() {
|
||||||
declare var_function=$1; shift
|
declare var_function=$1; shift
|
||||||
|
@ -82,6 +82,7 @@ var() {
|
||||||
(is_fs_object) [[ -e \"\$_var\" ]];;
|
(is_fs_object) [[ -e \"\$_var\" ]];;
|
||||||
(is_file) [[ -f \"\$_var\" ]];;
|
(is_file) [[ -f \"\$_var\" ]];;
|
||||||
(is_dir|is_directory) [[ -d \"\$_var\" ]];;
|
(is_dir|is_directory) [[ -d \"\$_var\" ]];;
|
||||||
|
(is_empty) [[ -z \"\$_var\" ]];;
|
||||||
|
|
||||||
(*) die 71 \"Syntax error in ${var_function}!\";;
|
(*) die 71 \"Syntax error in ${var_function}!\";;
|
||||||
esac
|
esac
|
||||||
|
@ -128,7 +129,7 @@ spawn() {
|
||||||
else
|
else
|
||||||
exec "$@" >"$service_logfile_out" 2>"$service_logfile_err"
|
exec "$@" >"$service_logfile_out" 2>"$service_logfile_err"
|
||||||
fi
|
fi
|
||||||
}
|
}; readonly spawn;
|
||||||
|
|
||||||
## Run the command and wait for it to die
|
## Run the command and wait for it to die
|
||||||
svc() {
|
svc() {
|
||||||
|
@ -170,7 +171,7 @@ svc() {
|
||||||
# We need to wait for the service to write down its pidfile
|
# We need to wait for the service to write down its pidfile
|
||||||
until service_pidfile is file; do
|
until service_pidfile is file; do
|
||||||
(( counter >= service_pidfile_timeout*10 )) && {
|
(( counter >= service_pidfile_timeout*10 )) && {
|
||||||
printf 'No pidfile' > "$service_failed_flag"
|
printf '127' > "$service_exit_file"
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
counter++
|
counter++
|
||||||
|
@ -189,10 +190,10 @@ svc() {
|
||||||
else
|
else
|
||||||
job_success = 0
|
job_success = 0
|
||||||
fail_counter++
|
fail_counter++
|
||||||
|
|
||||||
printf '%s' "$job_exit" > "$service_failed_flag"
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
printf '%s' "$job_exit" > "$service_exit_file"
|
||||||
|
|
||||||
# Back off if the service exits too much AND too quickly.
|
# Back off if the service exits too much AND too quickly.
|
||||||
service_respawn_force || {
|
service_respawn_force || {
|
||||||
if (( fail_counter >= 3 )); then
|
if (( fail_counter >= 3 )); then
|
||||||
|
@ -209,9 +210,6 @@ svc() {
|
||||||
(on-failure) job_success && break;;
|
(on-failure) job_success && break;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
# Remove the failed flag, we're going to attempt a restart.
|
|
||||||
rm -f "$service_failed_flag"
|
|
||||||
|
|
||||||
# Record the time every time we restart the loop
|
# Record the time every time we restart the loop
|
||||||
printf -v last_respawn '%(%s)T'
|
printf -v last_respawn '%(%s)T'
|
||||||
done
|
done
|
||||||
|
@ -257,6 +255,10 @@ timer() {
|
||||||
return 0
|
return 0
|
||||||
}; readonly -f timer
|
}; readonly -f timer
|
||||||
|
|
||||||
|
## Set a service's ready flag.
|
||||||
|
set_ready() { printf '1' > "$service_ready_flag"; }
|
||||||
|
readonly -f set_ready
|
||||||
|
|
||||||
## Is a service ready?
|
## Is a service ready?
|
||||||
is_ready() { service_ready_flag is file; }
|
is_ready() { service_ready_flag is file; }
|
||||||
readonly -f is_ready
|
readonly -f is_ready
|
||||||
|
@ -359,7 +361,7 @@ start() {
|
||||||
if service_oneshot; then
|
if service_oneshot; then
|
||||||
spawn "${service_command[@]}"; res=$?
|
spawn "${service_command[@]}"; res=$?
|
||||||
(( res )) && {
|
(( res )) && {
|
||||||
printf '%s' "$res" > "$service_failed_flag"
|
printf '%s' "$res" > "$service_exit_file"
|
||||||
return "$res"
|
return "$res"
|
||||||
}
|
}
|
||||||
printf '1' > "$service_enabled_flag"
|
printf '1' > "$service_enabled_flag"
|
||||||
|
@ -367,7 +369,7 @@ start() {
|
||||||
svc "${service_command[@]}" &
|
svc "${service_command[@]}" &
|
||||||
|
|
||||||
if timer "$service_ready_timeout" ready; then
|
if timer "$service_ready_timeout" ready; then
|
||||||
printf '1' > "$service_ready_flag"
|
set_ready
|
||||||
else
|
else
|
||||||
return 5
|
return 5
|
||||||
fi
|
fi
|
||||||
|
@ -488,6 +490,7 @@ status() {
|
||||||
service_enabled && return 0
|
service_enabled && return 0
|
||||||
service_stopped && return 7
|
service_stopped && return 7
|
||||||
service_failed && return 9
|
service_failed && return 9
|
||||||
|
service_exit_last is empty || return 11
|
||||||
|
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
@ -499,7 +502,7 @@ qstatus() { nullexec status; }
|
||||||
ready() { :; }
|
ready() { :; }
|
||||||
|
|
||||||
## Reset failes
|
## Reset failes
|
||||||
reset-failed() { rm -f "$service_failed_flag"; }
|
reset-failed() { rm -f "$service_exit_file"; }
|
||||||
|
|
||||||
# Main code
|
# Main code
|
||||||
## Empty declarations
|
## Empty declarations
|
||||||
|
@ -517,7 +520,8 @@ var service_pid \
|
||||||
service_ready_flag \
|
service_ready_flag \
|
||||||
service_enabled_flag \
|
service_enabled_flag \
|
||||||
service_stopped_flag \
|
service_stopped_flag \
|
||||||
service_failed_flag \
|
service_exit_file \
|
||||||
|
service_exit_last \
|
||||||
service_cgroup_name \
|
service_cgroup_name \
|
||||||
service_cgroup_procs \
|
service_cgroup_procs \
|
||||||
service_cgroup_path \
|
service_cgroup_path \
|
||||||
|
@ -688,7 +692,7 @@ service_logfile_err := "$service_logfile_out"
|
||||||
service_ready_flag := "$rundir/$service_name.ready"
|
service_ready_flag := "$rundir/$service_name.ready"
|
||||||
service_enabled_flag := "$rundir/$service_name.enabled"
|
service_enabled_flag := "$rundir/$service_name.enabled"
|
||||||
service_stopped_flag := "$rundir/$service_name.stopped"
|
service_stopped_flag := "$rundir/$service_name.stopped"
|
||||||
service_failed_flag := "$rundir/$service_name.failed"
|
service_exit_file := "$rundir/$service_name.exit"
|
||||||
service_cgroup_name := "$service_name"
|
service_cgroup_name := "$service_name"
|
||||||
service_cgroup_path := "$cgroup_home/$service_name"
|
service_cgroup_path := "$cgroup_home/$service_name"
|
||||||
service_success_exit := 0
|
service_success_exit := 0
|
||||||
|
@ -721,10 +725,14 @@ if service_stopped_flag is file; then
|
||||||
service_stopped = 1
|
service_stopped = 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Maybe it has failed?
|
# Check if the service has failed
|
||||||
if service_failed_flag is file; then
|
if service_exit_file is file; then
|
||||||
|
read -r service_exit_last < "$service_exit_file"
|
||||||
|
|
||||||
|
if ! service_success_exit u "$service_exit_last"; then
|
||||||
# :(
|
# :(
|
||||||
service_failed = 1
|
service_failed = 1
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Check cgroups, if enabled
|
# Check cgroups, if enabled
|
||||||
|
@ -800,7 +808,8 @@ case "$2" in
|
||||||
0 "$service_name is running" \
|
0 "$service_name is running" \
|
||||||
1 "$service_name is not running" \
|
1 "$service_name is not running" \
|
||||||
7 "$service_name was stopped" \
|
7 "$service_name was stopped" \
|
||||||
9 "$service_name has failed"
|
9 "$service_name has failed with code: $service_exit_last" \
|
||||||
|
11 "$service_name has exited with code: $service_exit_last"
|
||||||
fi
|
fi
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
|
Loading…
Reference in New Issue
Block a user