mirror of
https://github.com/rommapp/romm.git
synced 2026-12-02 18:18:47 +01:00
rq-scheduler has one release since 2023, 109 open issues, and RomM ran a
fork of it for Redis username and SSL options that upstream never took.
Everything it was here for now ships with RQ.
Its two failure modes were the reason for the workarounds in 9e9f59c04.
It reads a job's function name before taking the job out of its registry,
so one unreadable job stalls it for good; RQ's scheduler removes every id
it looked at whether or not the job could be read. And cancelling a
delayed job needed both `Scheduler.cancel` and `Job.cancel`, because the
first left the status alone and the second left the registry entry, which
the scheduler then queued anyway; `Job.cancel` on its own now clears the
scheduled registry.
Delayed jobs, which is how the watcher defers a rescan, go onto the queue
with `Queue.enqueue_in` and are released by the worker running with
`--with-scheduler`. Periodic tasks are declared in `tasks/cron_config.py`
and loaded by an `rq cron` process, replacing `rqscheduler`. A task is
registered only when it is enabled and has a cron string, so the
schedule-then-unschedule dance around the env toggles is gone, and with
it `PeriodicTask.init/schedule/unschedule`.
Jobs are enqueued by task name through `run_task_by_name`, resolved
against a catalog in `tasks/registry.py`. Nothing pickles a task instance
into Redis any more, which is what made a payload unreadable across
upgrades in the first place. The catalog also gives the tasks endpoint
one home for what a task is, rather than a list of its own.
Scan discovery keys off the type a job carries rather than the scheduled
rescan's function name, since every task now shares one entry point. That
also drops SCAN_LIBRARY_TASK_FUNC, which only existed to name a function
across an import cycle.
The watcher's dedupe worked on `job.args[0]` while scans are enqueued with
keyword arguments, so it never matched and every filesystem change
scheduled another scan. It reads `platform_ids` from the payload now, and
deliberately ignores a scan already running: that one may have walked past
the folder that just changed.
Startup clears what the old scheduler left in Redis, once.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
108 lines
3.4 KiB
Bash
108 lines
3.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
|
|
|
|
# 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
|
|
|
|
# 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
|
|
|
|
# Build Redis URL properly
|
|
if [[ -n ${REDIS_PASSWORD-} ]]; then
|
|
REDIS_URL="redis${REDIS_SSL:+s}://${REDIS_USERNAME-}:${REDIS_PASSWORD}@${REDIS_HOST:-127.0.0.1}:${REDIS_PORT:-6379}/${REDIS_DB:-0}"
|
|
elif [[ -n ${REDIS_USERNAME-} ]]; then
|
|
REDIS_URL="redis${REDIS_SSL:+s}://${REDIS_USERNAME}@${REDIS_HOST:-127.0.0.1}:${REDIS_PORT:-6379}/${REDIS_DB:-0}"
|
|
else
|
|
REDIS_URL="redis${REDIS_SSL:+s}://${REDIS_HOST:-127.0.0.1}:${REDIS_PORT:-6379}/${REDIS_DB:-0}"
|
|
fi
|
|
|
|
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 \
|
|
--logging-level "${LOGLEVEL:-INFO}" \
|
|
tasks.cron_config &
|
|
|
|
echo "Starting RQ worker..."
|
|
# 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.
|
|
PYTHONPATH="/app/backend:${PYTHONPATH-}" \
|
|
RQ_REDIS_URL="${REDIS_URL}" \
|
|
rq worker \
|
|
--path /app/backend \
|
|
--worker-class handler.rq_worker.RomMWorker \
|
|
--pid /tmp/rq_worker.pid \
|
|
--logging_level "${LOGLEVEL:-INFO}" \
|
|
--with-scheduler \
|
|
high default low &
|
|
|
|
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
|