Files
romm/entrypoint.sh
Georges-Antoine Assi c413c066c6 refactor(docker): supervise the container with s6-overlay (#4019)
* 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>
2026-11-24 08:29:06 -05:00

168 lines
5.4 KiB
Bash

#!/bin/bash
set -e
echo "Starting entrypoint script..."
# Create symlinks for frontend
if [[ -L /app/frontend/assets/romm/resources ]]; then
target=$(readlink "/app/frontend/assets/romm/resources")
# If the target is not the same as ${ROMM_BASE_PATH}/resources, recreate the symbolic link.
if [[ ${target} != "${ROMM_BASE_PATH}/resources" ]]; then
rm "/app/frontend/assets/romm/resources"
ln -s "${ROMM_BASE_PATH}/resources" "/app/frontend/assets/romm/resources"
fi
elif [[ ! -e /app/frontend/assets/romm/resources ]]; then
# Ensure parent directory exists before creating symbolic link
mkdir -p "/app/frontend/assets/romm"
ln -s "${ROMM_BASE_PATH}/resources" "/app/frontend/assets/romm/resources"
fi
# The ./frontend bind mount hides whatever the build wrote under /app/frontend,
# so link the image's emulator runtimes into the tree the dev server serves.
if [[ -n ${EMULATOR_ASSETS_DIR-} && -d ${EMULATOR_ASSETS_DIR} ]]; then
for runtime_dir in "${EMULATOR_ASSETS_DIR}"/*; do
target="/app/frontend/assets/$(basename "${runtime_dir}")"
mkdir -p "${target}"
for entry in "${runtime_dir}"/*; do
link="${target}/$(basename "${entry}")"
# Never replace checked-in art that shares a name with a runtime file.
if [[ -e ${link} && ! -L ${link} ]]; then
continue
fi
ln -sfn "${entry}" "${link}"
done
done
fi
# The backend reads the free RTP where the full image keeps it.
if [[ -n ${EMULATOR_ASSETS_DIR-} && -d ${EMULATOR_ASSETS_DIR}/easyrpg/rtp ]]; then
mkdir -p /var/www/html/assets/easyrpg
ln -sfn "${EMULATOR_ASSETS_DIR}/easyrpg/rtp" /var/www/html/assets/easyrpg/rtp
fi
# Define a signal handler to propagate termination signals
function handle_termination() {
echo "Terminating child processes..."
# Kill all background jobs
# trunk-ignore(shellcheck)
kill -TERM $(jobs -p) 2>/dev/null
}
# Trap SIGTERM and SIGINT signals
trap handle_termination SIGTERM SIGINT
# Set ROMM_AUTH_SECRET_KEY if not already set
if [[ -z ${ROMM_AUTH_SECRET_KEY-} ]]; then
ROMM_AUTH_SECRET_KEY=$(python3 -c "import secrets; print(secrets.token_hex(32))")
export ROMM_AUTH_SECRET_KEY
fi
# Read before the backend starts: its startup can rewrite config.yml.
streaming_worker_enabled="false"
if (cd /app/backend && uv run python -c "
import sys
from config.config_manager import config_manager
sys.exit(0 if config_manager.get_config().STREAMING_ENABLED else 1)
" >/dev/null 2>&1); then
streaming_worker_enabled="true"
fi
# Start all services in the background
echo "Starting backend..."
cd /app/backend
if [[ ${DEV_MODE:-false} == "true" ]]; then
echo "Starting backend under debugpy on :5678..."
# Add --wait-for-client after --listen to pause until VSCode attaches.
uv run python -m debugpy --listen 0.0.0.0:5678 main.py &
else
uv run python main.py &
fi
# REDIS_SSL is a boolean to the app, so "false" and "0" mean plaintext.
REDIS_USERINFO=""
REDIS_SSL_VALUE="${REDIS_SSL-}"
case "${REDIS_SSL_VALUE,,}" in
1 | true | yes | on) REDIS_SCHEME="rediss" ;;
*) REDIS_SCHEME="redis" ;;
esac
if [[ -n ${REDIS_PASSWORD-} ]]; then
REDIS_USERINFO="${REDIS_USERNAME-}:${REDIS_PASSWORD}@"
elif [[ -n ${REDIS_USERNAME-} ]]; then
REDIS_USERINFO="${REDIS_USERNAME}@"
fi
REDIS_URL="${REDIS_SCHEME}://${REDIS_USERINFO}${REDIS_HOST:-127.0.0.1}:${REDIS_PORT:-6379}/${REDIS_DB:-0}"
echo "Starting RQ cron scheduler..."
# The URL carries the password, so it goes through RQ_REDIS_URL rather than
# --url, which would put it on a world-readable command line.
PYTHONPATH="/app/backend:${PYTHONPATH-}" \
RQ_REDIS_URL="${REDIS_URL}" \
rq cron \
--path /app/backend \
--connection-class handler.redis_handler.RomMRedis \
--logging-level "${LOGLEVEL:-INFO}" \
tasks.cron_config &
# Set PYTHONPATH so RQ can find the tasks module.
# Use a worker class that drops the noisy per-sweep "cleaning registries for
# queue" log line. The maintenance interval keeps its default (~10 min) so
# orphaned STARTED jobs and stale workers are still pruned promptly.
# --with-scheduler releases delayed jobs, which is how the watcher's rescans
# wait out their delay.
start_rq_worker() {
local name="$1"
local worker_class="$2"
shift 2
PYTHONPATH="/app/backend:${PYTHONPATH-}" \
RQ_REDIS_URL="${REDIS_URL}" \
rq worker \
--path /app/backend \
--worker-class "handler.rq_worker.${worker_class}" \
--connection-class handler.redis_handler.RomMRedis \
--pid "/tmp/${name}.pid" \
--results-ttl "${TASK_RESULT_TTL:-86400}" \
--logging_level "${LOGLEVEL:-INFO}" \
--with-scheduler \
"$@" &
}
echo "Starting RQ worker..."
start_rq_worker rq_worker RomMWorker high default low
# Scans get a worker of their own, see SCAN_QUEUE_NAME.
echo "Starting RQ scan worker..."
start_rq_worker rq_scan_worker RomMWorker scans
# Streaming teardowns get a worker of their own, see STREAMING_QUEUE_NAME. It
# preloads the reaper, which runs every minute when streaming is on.
if [[ ${streaming_worker_enabled} == "true" ]]; then
echo "Starting RQ streaming worker..."
start_rq_worker rq_streaming_worker PreloadingWorker streaming
fi
echo "Starting watcher..."
watchfiles \
--target-type command \
'uv run python watcher.py' \
/app/romm/library &
if [[ ${ENABLE_SYNC_FOLDER_WATCHER:-false} == "true" ]]; then
echo "Starting sync folder watcher..."
sync_base_path="${ROMM_BASE_PATH:-/romm}/sync"
mkdir -p "${sync_base_path}"
watchfiles \
--target-type command \
'uv run python sync_watcher.py' \
"${sync_base_path}" &
fi
# Start the frontend dev server
cd /app/frontend
npm run dev &
# Wait for all background processes
wait