From 7e222642c9d76784ed338884108a63026e82b43e Mon Sep 17 00:00:00 2001 From: Felipe Santos Date: Sun, 4 Dec 2022 13:37:28 -0300 Subject: [PATCH] Gracefully handle shutdown --- Dockerfile | 24 ++++++++++--------- docker/rootfs/etc/cont-init.d/prepare-logs.sh | 3 ++- docker/rootfs/etc/services.d/frigate/finish | 15 +++++++++--- docker/rootfs/etc/services.d/frigate/run | 6 +++-- docker/rootfs/etc/services.d/go2rtc/finish | 3 ++- docker/rootfs/etc/services.d/go2rtc/run | 5 ++-- docker/rootfs/etc/services.d/nginx/finish | 3 ++- docker/rootfs/etc/services.d/nginx/run | 2 ++ 8 files changed, 40 insertions(+), 21 deletions(-) diff --git a/Dockerfile b/Dockerfile index c63ca3baf..9ae86956a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -155,17 +155,6 @@ ENV NVIDIA_DRIVER_CAPABILITIES="compute,video,utility" ENV PATH="/usr/lib/btbn-ffmpeg/bin:/usr/local/go2rtc/bin:/usr/local/nginx/sbin:${PATH}" -# Fails if cont-init.d fails -ENV S6_BEHAVIOUR_IF_STAGE2_FAILS=2 -# Wait indefinitely for cont-init.d to finish before starting services -ENV S6_CMD_WAIT_FOR_SERVICES=1 -ENV S6_CMD_WAIT_FOR_SERVICES_MAXTIME=0 -# Configure logging to prepend timestamps, log to stdout, keep 1 archive and rotate on 10MB -ENV S6_LOGGING_SCRIPT="T 1 n1 s10000000 T" -# TODO: remove after a new version of s6-overlay is released. See: -# https://github.com/just-containers/s6-overlay/issues/460#issuecomment-1327127006 -ENV S6_SERVICES_READYTIME=50 - # Install dependencies RUN --mount=type=bind,source=docker/install_deps.sh,target=/deps/install_deps.sh \ /deps/install_deps.sh @@ -182,6 +171,19 @@ EXPOSE 1935 EXPOSE 8554 EXPOSE 8555 +# Fails if cont-init.d fails +ENV S6_BEHAVIOUR_IF_STAGE2_FAILS=2 +# Wait indefinitely for cont-init.d to finish before starting services +ENV S6_CMD_WAIT_FOR_SERVICES=1 +ENV S6_CMD_WAIT_FOR_SERVICES_MAXTIME=0 +# Give services (including Frigate) 30 seconds to stop before killing them +ENV S6_SERVICES_GRACETIME=30000 +# Configure logging to prepend timestamps, log to stdout, keep 1 archive and rotate on 10MB +ENV S6_LOGGING_SCRIPT="T 1 n1 s10000000 T" +# TODO: remove after a new version of s6-overlay is released. See: +# https://github.com/just-containers/s6-overlay/issues/460#issuecomment-1327127006 +ENV S6_SERVICES_READYTIME=50 + ENTRYPOINT ["/init"] CMD [] diff --git a/docker/rootfs/etc/cont-init.d/prepare-logs.sh b/docker/rootfs/etc/cont-init.d/prepare-logs.sh index 48056e794..51c5ec55d 100755 --- a/docker/rootfs/etc/cont-init.d/prepare-logs.sh +++ b/docker/rootfs/etc/cont-init.d/prepare-logs.sh @@ -1,7 +1,8 @@ #!/command/with-contenv bash # shellcheck shell=bash +# Prepare the logs folder for s6-log -set -euo pipefail +set -o errexit -o nounset -o pipefail mkdir -p /dev/shm/logs chown nobody:nogroup /dev/shm/logs diff --git a/docker/rootfs/etc/services.d/frigate/finish b/docker/rootfs/etc/services.d/frigate/finish index 3a9da25ce..e219ed8b4 100755 --- a/docker/rootfs/etc/services.d/frigate/finish +++ b/docker/rootfs/etc/services.d/frigate/finish @@ -1,7 +1,16 @@ #!/command/with-contenv bash # shellcheck shell=bash -# Take down the S6 supervision tree when the process fails +# Take down the S6 supervision tree when the service exits -if [[ "${1}" -ne 0 && "${1}" -ne 256 ]]; then - exec /run/s6/basedir/bin/halt +set -o errexit -o nounset -o pipefail + +# Prepare exit code +if [[ "${1}" -eq 256 ]]; then + exit_code="$((128 + ${2}))" +else + exit_code="${1}" fi + +# Make the container exit with the same exit code as the service +echo "${exit_code}" > /run/s6-linux-init-container-results/exitcode +exec /run/s6/basedir/bin/halt diff --git a/docker/rootfs/etc/services.d/frigate/run b/docker/rootfs/etc/services.d/frigate/run index 2390bc9d2..704ac1922 100755 --- a/docker/rootfs/etc/services.d/frigate/run +++ b/docker/rootfs/etc/services.d/frigate/run @@ -1,9 +1,11 @@ -#!/command/with-contenv bash +#!/bin/bash # shellcheck shell=bash +# Start the Frigate service -set -euo pipefail +set -o errexit -o nounset -o pipefail -o xtrace cd /opt/frigate +# Replace the bash process with the Frigate process, redirecting stderr to stdout exec 2>&1 exec python3 -u -m frigate diff --git a/docker/rootfs/etc/services.d/go2rtc/finish b/docker/rootfs/etc/services.d/go2rtc/finish index 3a9da25ce..c1b77fccc 100755 --- a/docker/rootfs/etc/services.d/go2rtc/finish +++ b/docker/rootfs/etc/services.d/go2rtc/finish @@ -1,6 +1,7 @@ #!/command/with-contenv bash # shellcheck shell=bash -# Take down the S6 supervision tree when the process fails +# Take down the S6 supervision tree when the service fails, or restart it +# otherwise if [[ "${1}" -ne 0 && "${1}" -ne 256 ]]; then exec /run/s6/basedir/bin/halt diff --git a/docker/rootfs/etc/services.d/go2rtc/run b/docker/rootfs/etc/services.d/go2rtc/run index 80d80636b..9b41e517b 100755 --- a/docker/rootfs/etc/services.d/go2rtc/run +++ b/docker/rootfs/etc/services.d/go2rtc/run @@ -1,8 +1,8 @@ #!/command/with-contenv bash # shellcheck shell=bash +# Start the go2rtc service -# https://gist.github.com/mohanpedala/1e2ff5661761d3abd0385e8223e16425?permalink_comment_id=3945021 -set -euo pipefail +set -o errexit -o nounset -o pipefail if [[ -f "/config/frigate-go2rtc.yaml" ]]; then config_path="/config/frigate-go2rtc.yaml" @@ -10,5 +10,6 @@ else config_path="/usr/local/go2rtc/go2rtc.yaml" fi +# Replace the bash process with the go2rtc process, redirecting stderr to stdout exec 2>&1 exec go2rtc -config="${config_path}" diff --git a/docker/rootfs/etc/services.d/nginx/finish b/docker/rootfs/etc/services.d/nginx/finish index 3a9da25ce..c1b77fccc 100755 --- a/docker/rootfs/etc/services.d/nginx/finish +++ b/docker/rootfs/etc/services.d/nginx/finish @@ -1,6 +1,7 @@ #!/command/with-contenv bash # shellcheck shell=bash -# Take down the S6 supervision tree when the process fails +# Take down the S6 supervision tree when the service fails, or restart it +# otherwise if [[ "${1}" -ne 0 && "${1}" -ne 256 ]]; then exec /run/s6/basedir/bin/halt diff --git a/docker/rootfs/etc/services.d/nginx/run b/docker/rootfs/etc/services.d/nginx/run index 5585efaa0..f9978b6b2 100755 --- a/docker/rootfs/etc/services.d/nginx/run +++ b/docker/rootfs/etc/services.d/nginx/run @@ -1,5 +1,7 @@ #!/command/with-contenv bash # shellcheck shell=bash +# Start the NGINX service +# Replace the bash process with the NGINX process, redirecting stderr to stdout exec 2>&1 exec nginx