68 lines
2.2 KiB
Bash
68 lines
2.2 KiB
Bash
#!/usr/bin/env bash
|
|
set -Eeuo pipefail
|
|
|
|
postgres_pid=""
|
|
app_pid=""
|
|
shutdown_requested=0
|
|
terminate_children() {
|
|
[[ -z "$app_pid" ]] || kill -TERM "$app_pid" 2>/dev/null || true
|
|
[[ -z "$postgres_pid" ]] || kill -TERM "$postgres_pid" 2>/dev/null || true
|
|
for _ in $(seq 1 10); do
|
|
if { [[ -z "$app_pid" ]] || ! kill -0 "$app_pid" 2>/dev/null; } &&
|
|
{ [[ -z "$postgres_pid" ]] || ! kill -0 "$postgres_pid" 2>/dev/null; }; then
|
|
break
|
|
fi
|
|
sleep 1
|
|
done
|
|
[[ -z "$app_pid" ]] || ! kill -0 "$app_pid" 2>/dev/null || kill -KILL "$app_pid" 2>/dev/null || true
|
|
[[ -z "$postgres_pid" ]] || ! kill -0 "$postgres_pid" 2>/dev/null || kill -KILL "$postgres_pid" 2>/dev/null || true
|
|
[[ -z "$app_pid" ]] || wait "$app_pid" 2>/dev/null || true
|
|
[[ -z "$postgres_pid" ]] || wait "$postgres_pid" 2>/dev/null || true
|
|
}
|
|
shutdown() {
|
|
shutdown_requested=1
|
|
trap - TERM INT
|
|
terminate_children
|
|
}
|
|
trap shutdown TERM INT
|
|
|
|
runtime_owner="${PUID:-1654}:${PGID:-1654}"
|
|
for app_directory in /app/data /app/cache /app/exports; do
|
|
current_owner="$(stat -c '%u:%g' "$app_directory")"
|
|
if [[ "$current_owner" != "$runtime_owner" ]]; then
|
|
chown -R "$runtime_owner" "$app_directory"
|
|
fi
|
|
done
|
|
/usr/local/bin/docker-entrypoint.sh postgres &
|
|
postgres_pid=$!
|
|
for attempt in $(seq 1 60); do
|
|
if pg_isready --host=127.0.0.1 --username="${POSTGRES_USER:-ludarium}" --dbname="${POSTGRES_DB:-ludarium}" >/dev/null 2>&1; then break; fi
|
|
if ! kill -0 "$postgres_pid" 2>/dev/null; then wait "$postgres_pid"; exit $?; fi
|
|
if [[ "$attempt" == "60" ]]; then echo "PostgreSQL did not become ready within 60 seconds" >&2; exit 1; fi
|
|
sleep 1
|
|
done
|
|
|
|
/usr/local/bin/repair-postgres-collation.sh
|
|
|
|
gosu "${PUID:-1654}:${PGID:-1654}" /usr/share/dotnet/dotnet /app/Ludarium.Api.dll &
|
|
app_pid=$!
|
|
set +e
|
|
wait -n "$postgres_pid" "$app_pid"
|
|
first_status=$?
|
|
set -e
|
|
|
|
if [[ "$shutdown_requested" == "1" ]]; then
|
|
exit 0
|
|
fi
|
|
|
|
if ! kill -0 "$postgres_pid" 2>/dev/null; then
|
|
echo "PostgreSQL exited unexpectedly; stopping the Ludarium API." >&2
|
|
terminate_children
|
|
[[ "$first_status" == "0" ]] && exit 1
|
|
exit "$first_status"
|
|
fi
|
|
|
|
echo "The Ludarium API exited; stopping embedded PostgreSQL." >&2
|
|
terminate_children
|
|
exit "$first_status"
|