mirror of
https://github.com/rommapp/romm.git
synced 2026-11-28 17:48:31 +01:00
* refactor(docker): supervise the container with s6-overlay The init script was a hand-rolled supervisor: PID files, /proc polling, a 5s watchdog loop and a trap-based shutdown. Its shutdown wait was unbounded, so a process that refused to exit stranded PID 1 and left the container up and serving nothing, which no restart policy can detect. s6-rc replaces it with a declared service graph. Ordering and readiness gates that were sequential bash become dependencies, and the failure modes become configuration: - timeout-kill bounds SIGTERM before SIGKILL, and flag-timeout-killpg escalates to the whole process group. - S6_BEHAVIOUR_IF_STAGE2_FAILS=2 exits the container when a startup step fails, so the restart policy retries instead of the container wedging. - valkey gets --shutdown-on-sigterm force, so it stops refusing to exit when the snapshot it writes on shutdown cannot be written. A crashed service is now restarted on its own instead of taking the whole container down. Its leftover children are swept first, because children reparented from a hard-killed master keep holding its listening sockets and would make every replacement fail to bind. Optional services have no conditional mechanism in s6-rc, so they start, check their environment variable, and take themselves down with s6-svc -O. Running with a read-only root filesystem now needs /run mounted exec, since s6 executes its generated stage scripts from there. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix(docker): always start the RQ scheduler under s6 The s6 service carried over the four-flag ENABLE_SCHEDULED_* gate from the init script, which #4033 has since removed on master. Since this branch deletes that script, the fix would be lost on rebase. Nothing else respects the condition: startup.py registers the netplay, upload-tmp and zip-cache cleanups unconditionally, three more flags gate periodic tasks absent from the list, and the watcher defers its rescans through the scheduler. Any of those leaves jobs in the scheduler registry with no process to run them, and a stuck delayed scan_platforms entry reads to the concurrent scan guard as a scan already queued, refusing every manual scan. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix(docker): port master's init changes to the s6 services Master reworked docker/init_scripts/init while this branch replaced it, so carry those changes into the s6-rc services: - Replace rq-scheduler with `rq cron` (romm-rq-cron) on tasks.cron_config. - Add romm-rq-scan-worker (scans queue) and romm-rq-streaming-worker (streaming queue, down unless config.yml enables streaming). - Run every worker with --with-scheduler and the RomMRedis connection class, passing the Redis URL through RQ_REDIS_URL so the password stays off the command line, and read REDIS_SSL as a boolean. - Bring gunicorn's flags in line: --no-control-socket, a private-range FORWARDED_ALLOW_IPS default, 4 workers and a 65s keep-alive. - Check the sync folder and SSH keys path in romm-init, and switch the features off through the container environment when they're unusable. - Accept the backend's truthy values for the watcher flags. - Point the serving-layer guard tests at the gunicorn run script and drop the PID-file tests, which guarded the bash watchdog. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test(docker): guard the s6-overlay service tree Check that every service is in the user bundle, depends on something that exists, and that every longrun has its finish sweep, kill timeout and process-group flag. Also check that the dev entrypoint starts the same RQ workers and cron scheduler as the image, since it keeps its own launcher. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(dev): pass the image's RQ flags in the dev entrypoint The dev image's RQ cron scheduler and workers ran without the RomMRedis connection class, and the workers without --results-ttl, so they kept redis-py's maintenance notification probe and RQ's default result TTL. The s6 guard test now compares each rq command's flags with the image's. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(docker): address Greptile review on the s6 service tree - Cut the s6 script comments to two lines, keeping the reason each helper exists. - Compare the values of the dev entrypoint's RQ options with the image's, not only their names, allowing for the dev image's path, PID files and LOGLEVEL default. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * ci(docker): check every s6 service stays up after boot The heartbeat only proves gunicorn and nginx came up. Enable both watchers and require each longrun in the s6 tree to report up for 15s straight, so a crashing or restart-looping worker, cron, valkey or watcher fails the job. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * ci(docker): enable streaming so its worker is checked too The streaming worker turns itself off when streaming.enabled is false in config.yml, which the services check read as a failure. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
143 lines
4.1 KiB
Bash
143 lines
4.1 KiB
Bash
#!/usr/bin/env bash
|
|
# Shared helpers for the s6-rc service scripts. Sourced, never executed.
|
|
# Callers set ROMM_LOG_TAG before sourcing to label their log lines.
|
|
|
|
LOGLEVEL="${LOGLEVEL:="INFO"}"
|
|
ROMM_LOG_TAG="${ROMM_LOG_TAG:="init"}"
|
|
|
|
# logger colors
|
|
RED='\033[0;31m'
|
|
LIGHTMAGENTA='\033[0;95m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[0;33m'
|
|
BLUE='\033[0;34m'
|
|
CYAN='\033[0;36m'
|
|
RESET='\033[0;00m'
|
|
|
|
_log() {
|
|
local level_color=$1 level_text=$2
|
|
shift 2
|
|
echo -e "${level_color}${level_text}${BLUE}[RomM]${LIGHTMAGENTA}[${ROMM_LOG_TAG}]${CYAN}[$(date +"%Y-%m-%d %T")]${RESET}" "${@}" || true
|
|
}
|
|
|
|
debug_log() {
|
|
if [[ ${LOGLEVEL^^} == "DEBUG" ]]; then
|
|
_log "${LIGHTMAGENTA}" "DEBUG: " "${@}"
|
|
fi
|
|
}
|
|
|
|
info_log() {
|
|
_log "${GREEN}" "INFO: " "${@}"
|
|
}
|
|
|
|
warn_log() {
|
|
_log "${YELLOW}" "WARNING: " "${@}"
|
|
}
|
|
|
|
# Fatal: S6_BEHAVIOUR_IF_STAGE2_FAILS=2 turns a failed oneshot into a container
|
|
# exit, so the restart policy retries instead of leaving it half-started.
|
|
error_log() {
|
|
_log "${RED}" "ERROR: " "${@}"
|
|
exit 1
|
|
}
|
|
|
|
print_banner() {
|
|
local version
|
|
version=$(python3 -c "exec(open('/backend/__version__.py').read()); print(__version__)")
|
|
info_log " _____ __ __ "
|
|
info_log ' | __ \ | \/ |'
|
|
info_log ' | |__) |___ _ __ ___ | \ / |'
|
|
info_log " | _ // _ \\| '_ \` _ \\| |\\/| |"
|
|
info_log ' | | \ \ (_) | | | | | | | | |'
|
|
info_log ' |_| \_\___/|_| |_| |_|_| |_|'
|
|
info_log ""
|
|
info_log "The beautiful, powerful, self-hosted Rom manager and player"
|
|
info_log ""
|
|
info_log "Version: ${version}"
|
|
info_log ""
|
|
}
|
|
|
|
# Fill array "$1" with the opentelemetry-instrument prefix for service "$2", or
|
|
# leave it empty when OTEL is off. Embed it in a command string with "${arr[*]@Q}".
|
|
otel_prefix() {
|
|
local -n out_arr="$1"
|
|
if [[ ${OTEL_SDK_DISABLED:-false} == "true" ]]; then return 0; fi
|
|
if ! command -v opentelemetry-instrument >/dev/null 2>&1; then
|
|
warn_log "opentelemetry-instrument not found, starting $2 without OpenTelemetry instrumentation"
|
|
return 0
|
|
fi
|
|
# shellcheck disable=SC2034 # nameref binds out_arr to caller variable
|
|
out_arr=(opentelemetry-instrument --service_name "${OTEL_SERVICE_NAME_PREFIX-}$2")
|
|
}
|
|
|
|
# Poll a TCP port until it accepts a connection. Returns non-zero on timeout.
|
|
wait_for_tcp() {
|
|
local host=$1 port=$2 timeout_seconds=$3
|
|
local retries=$((timeout_seconds * 2))
|
|
while ((retries > 0)); do
|
|
if (echo >"/dev/tcp/${host}/${port}") 2>/dev/null; then
|
|
return 0
|
|
fi
|
|
sleep 0.5
|
|
retries=$((retries - 1))
|
|
done
|
|
return 1
|
|
}
|
|
|
|
# s6-supervise gives each service its own session, so its pid is the group id
|
|
# ./finish sweeps. Call before the run script changes directory or execs.
|
|
record_process_group() {
|
|
mkdir -p /run/romm
|
|
echo "$$" >"/run/romm/$(basename "${PWD}").pgid"
|
|
}
|
|
|
|
# s6-rc has no conditional services, so one that configuration switches off
|
|
# starts, then tells its supervisor not to bring it back.
|
|
disable_service() {
|
|
s6-svc -O .
|
|
exit 0
|
|
}
|
|
|
|
# The values the backend's safe_str_to_bool reads as true, matched lowercased.
|
|
is_truthy() {
|
|
[[ ${1,,} =~ ^(1|true|yes|on)$ ]]
|
|
}
|
|
|
|
# The URL carries the password, so callers pass it through RQ_REDIS_URL rather
|
|
# than --url, which would put it on a world-readable command line.
|
|
build_redis_url() {
|
|
local scheme="redis" userinfo=""
|
|
|
|
if is_truthy "${REDIS_SSL-}"; then
|
|
scheme="rediss"
|
|
fi
|
|
|
|
if [[ -n ${REDIS_PASSWORD-} ]]; then
|
|
userinfo="${REDIS_USERNAME-}:${REDIS_PASSWORD}@"
|
|
elif [[ -n ${REDIS_USERNAME-} ]]; then
|
|
userinfo="${REDIS_USERNAME}@"
|
|
fi
|
|
|
|
echo "${scheme}://${userinfo}${REDIS_HOST:-127.0.0.1}:${REDIS_PORT:-6379}/${REDIS_DB:-0}"
|
|
}
|
|
|
|
# Exec an RQ worker of class "$1" on the queues that follow. --with-scheduler
|
|
# releases delayed jobs, which is how the watcher's rescans wait out their delay.
|
|
exec_rq_worker() {
|
|
local worker_class="$1"
|
|
shift
|
|
|
|
RQ_REDIS_URL="$(build_redis_url)"
|
|
export RQ_REDIS_URL
|
|
export PYTHONPATH="/backend:${PYTHONPATH-}"
|
|
|
|
exec rq worker \
|
|
--path /backend \
|
|
--worker-class "handler.rq_worker.${worker_class}" \
|
|
--connection-class handler.redis_handler.RomMRedis \
|
|
--results-ttl "${TASK_RESULT_TTL:-86400}" \
|
|
--logging_level "${LOGLEVEL}" \
|
|
--with-scheduler \
|
|
"$@"
|
|
}
|