58 lines
2 KiB
Bash
Executable file
58 lines
2 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
# Container entrypoint (runs under tini). Gets the source and venv ready, then supervises
|
|
# uvicorn: SIGHUP restarts the app on the current checkout (that's how `update` swaps code
|
|
# in without restarting the container); SIGTERM stops it. If the app dies on its own, the
|
|
# container exits and Docker's restart policy takes over.
|
|
#
|
|
# With arguments, it runs them in the source dir after setup instead, e.g.
|
|
# docker compose run --rm app pytest
|
|
set -uo pipefail
|
|
. /usr/local/lib/app/lib.sh
|
|
|
|
load_env
|
|
ensure_source || exit 1
|
|
sync_deps || die "installing requirements failed"
|
|
cd "$SRC"
|
|
|
|
if [ $# -gt 0 ]; then
|
|
export PATH="$VENV/bin:$PATH"
|
|
exec "$@"
|
|
fi
|
|
|
|
child=
|
|
stopping=0
|
|
restart=0
|
|
trap 'stopping=1; [ -n "$child" ] && kill -TERM "$child" 2>/dev/null' TERM INT
|
|
trap 'restart=1; [ -n "$child" ] && kill -TERM "$child" 2>/dev/null' HUP
|
|
echo $$ > "$PIDFILE"
|
|
|
|
start_app() {
|
|
local args=(app.main:app --host 0.0.0.0 --port "$PORT"
|
|
--proxy-headers --forwarded-allow-ips "${FORWARDED_ALLOW_IPS:-127.0.0.1}"
|
|
--no-server-header --timeout-graceful-shutdown 10)
|
|
[ "${DEV:-}" = 1 ] && args+=(--reload)
|
|
export APP_VERSION
|
|
APP_VERSION=$(current_version)
|
|
log "starting app at $APP_VERSION"
|
|
env "${APP_ENV_UNSET[@]}" "$VENV/bin/uvicorn" "${args[@]}" &
|
|
child=$!
|
|
}
|
|
|
|
while :; do
|
|
start_app
|
|
wait "$child"; rc=$?
|
|
# A trapped signal interrupts `wait` while the child is still shutting down.
|
|
while kill -0 "$child" 2>/dev/null; do wait "$child"; rc=$?; done
|
|
[ "$stopping" = 1 ] && exit 0
|
|
if [ "$restart" = 1 ]; then restart=0; continue; fi
|
|
log "app exited with status $rc"
|
|
# New code that crashes at startup must not take the container down mid-update, or
|
|
# `update` dies with it and can't roll back. Wait for its restart signal instead.
|
|
if ! flock -n /tmp/app-update.lock true; then
|
|
log "an update is running; waiting for it to restart the app"
|
|
while [ "$restart" = 0 ] && [ "$stopping" = 0 ]; do sleep 1 & wait $!; done
|
|
[ "$stopping" = 1 ] && exit 0
|
|
restart=0; continue
|
|
fi
|
|
exit "$rc"
|
|
done
|