-
-
Notifications
You must be signed in to change notification settings - Fork 653
Expand file tree
/
Copy pathinit
More file actions
executable file
·372 lines (315 loc) · 10.9 KB
/
Copy pathinit
File metadata and controls
executable file
·372 lines (315 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#!/usr/bin/env bash
set -o errexit # treat errors as fatal
set -o nounset # treat unset variables as an error
set -o pipefail # treat errors in pipes as fatal
shopt -s inherit_errexit # inherit errexit
LOGLEVEL="${LOGLEVEL:="INFO"}"
# make it possible to disable the inotify watcher process
ENABLE_RESCAN_ON_FILESYSTEM_CHANGE="${ENABLE_RESCAN_ON_FILESYSTEM_CHANGE:="false"}"
ENABLE_SCHEDULED_RESCAN="${ENABLE_SCHEDULED_RESCAN:="false"}"
ENABLE_SCHEDULED_UPDATE_LAUNCHBOX_METADATA="${ENABLE_SCHEDULED_UPDATE_LAUNCHBOX_METADATA:="false"}"
ENABLE_SCHEDULED_UPDATE_SWITCH_TITLEDB="${ENABLE_SCHEDULED_UPDATE_SWITCH_TITLEDB:="false"}"
ENABLE_SYNC_FOLDER_WATCHER="${ENABLE_SYNC_FOLDER_WATCHER:="false"}"
# if REDIS_HOST is set, we assume that an external redis is used
REDIS_HOST="${REDIS_HOST:=""}"
# 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'
print_banner() {
local version
version=$(python3 -c "exec(open('__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 ""
}
debug_log() {
# print debug log output if enabled
if [[ ${LOGLEVEL} == "DEBUG" ]]; then
echo -e "${LIGHTMAGENTA}DEBUG: ${BLUE}[RomM]${LIGHTMAGENTA}[init]${CYAN}[$(date +"%Y-%m-%d %T")]${RESET}" "${@}" || true
fi
}
info_log() {
echo -e "${GREEN}INFO: ${BLUE}[RomM]${LIGHTMAGENTA}[init]${CYAN}[$(date +"%Y-%m-%d %T")]${RESET}" "${@}" || true
}
warn_log() {
echo -e "${YELLOW}WARNING: ${BLUE}[RomM]${LIGHTMAGENTA}[init]${CYAN}[$(date +"%Y-%m-%d %T")]${RESET}" "${@}" || true
}
error_log() {
echo -e "${RED}ERROR: ${BLUE}[RomM]${LIGHTMAGENTA}[init]${CYAN}[$(date +"%Y-%m-%d %T")]${RESET}" "${@}" || true
exit 1
}
# Commands to run initial startup tasks
run_startup() {
if ! PYTHONPATH="/backend:${PYTHONPATH-}" opentelemetry-instrument \
--service_name "${OTEL_SERVICE_NAME_PREFIX-}startup" \
python3 /backend/startup.py; then
error_log "Startup script failed, exiting"
fi
}
wait_for_gunicorn_socket() {
debug_log "Waiting for gunicorn socket file..."
local wait_seconds=${WEB_SERVER_GUNICORN_WAIT_SECONDS:=30}
local retries=$((wait_seconds * 2))
while [[ ! -S /tmp/gunicorn.sock && retries -gt 0 ]]; do
sleep 0.5
((retries--))
done
if [[ -S /tmp/gunicorn.sock ]]; then
debug_log "Gunicorn socket file found"
else
warn_log "Gunicorn socket file not found after waiting ${wait_seconds}s!"
fi
}
# Function that runs our main process and creates a corresponding PID file
start_bin_gunicorn() {
# cleanup potentially leftover socket
rm /tmp/gunicorn.sock -f
# Wire LOGLEVEL into Gunicorn's logging config so it respects the env var.
local gunicorn_level="${LOGLEVEL^^}"
local gunicorn_log_config="/tmp/gunicorn/logging.conf"
# Copy the config to a writable runtime path so this works on read-only root filesystems.
mkdir -p /tmp/gunicorn
cp /etc/gunicorn/logging.conf "${gunicorn_log_config}"
sed -i '/\[logger_gunicorn\]/,/^\[/ s/^level=.*/level='"${gunicorn_level}"'/' "${gunicorn_log_config}"
# commands to start our main application and store its PID to check for crashes
info_log "Starting backend"
export PYTHONUNBUFFERED=1
export PYTHONDONTWRITEBYTECODE=1
opentelemetry-instrument \
--service_name "${OTEL_SERVICE_NAME_PREFIX-}api" \
gunicorn \
--bind=0.0.0.0:"${DEV_PORT:-5000}" \
--bind=unix:/tmp/gunicorn.sock \
--pid=/tmp/gunicorn.pid \
--forwarded-allow-ips="*" \
--worker-class uvicorn_worker.UvicornWorker \
--workers "${WEB_SERVER_CONCURRENCY:-1}" \
--timeout "${WEB_SERVER_TIMEOUT:-300}" \
--keep-alive "${WEB_SERVER_KEEPALIVE:-2}" \
--max-requests "${WEB_SERVER_MAX_REQUESTS:-1000}" \
--max-requests-jitter "${WEB_SERVER_MAX_REQUESTS_JITTER:-100}" \
--worker-connections "${WEB_SERVER_WORKER_CONNECTIONS:-1000}" \
--error-logfile - \
--log-config "${gunicorn_log_config}" \
main:app &
}
# Commands to start nginx (handling PID creation internally)
start_bin_nginx() {
wait_for_gunicorn_socket
info_log "Starting nginx"
if [[ ${EUID} -ne 0 ]]; then
nginx
else
# if container runs as root, drop permissions
nginx -g 'user romm;'
fi
: "${ROMM_BASE_URL:=http://0.0.0.0:8080}"
info_log "🚀 RomM is now available at ${ROMM_BASE_URL}"
}
# Commands to start valkey-server (handling PID creation internally)
start_bin_valkey-server() {
info_log "Starting internal valkey"
if [[ -f /usr/local/etc/valkey/valkey.conf ]]; then
if [[ ${LOGLEVEL} == "DEBUG" ]]; then
valkey-server /usr/local/etc/valkey/valkey.conf &
else
valkey-server /usr/local/etc/valkey/valkey.conf >/dev/null 2>&1 &
fi
else
if [[ ${LOGLEVEL} == "DEBUG" ]]; then
valkey-server --dir /redis-data &
else
valkey-server --dir /redis-data >/dev/null 2>&1 &
fi
fi
VALKEY_PID=$!
echo "${VALKEY_PID}" >/tmp/valkey-server.pid
local host="127.0.0.1"
local port="6379"
local max_retries=120
local retry=0
debug_log "Waiting for internal valkey to be ready..."
# Temporarily disable errexit for this part of the script
set +o errexit
while ((retry < max_retries)); do
# Attempt to check if valkey TCP port is open
if (echo >/dev/tcp/"${host}"/"${port}") 2>/dev/null; then
debug_log "Internal valkey is ready and accepting connections"
set -o errexit # Re-enable errexit after success
return 0
fi
sleep 0.5
((retry++))
done
error_log "Internal valkey did not become ready after $((max_retries * 500))ms"
}
# Commands to start RQ scheduler
start_bin_rq_scheduler() {
info_log "Starting RQ scheduler"
RQ_REDIS_HOST=${REDIS_HOST:-127.0.0.1} \
RQ_REDIS_PORT=${REDIS_PORT:-6379} \
RQ_REDIS_USERNAME=${REDIS_USERNAME:-""} \
RQ_REDIS_PASSWORD=${REDIS_PASSWORD:-""} \
RQ_REDIS_DB=${REDIS_DB:-0} \
RQ_REDIS_SSL=${REDIS_SSL:-0} \
rqscheduler \
--path /backend \
--pid /tmp/rq_scheduler.pid &
}
# Commands to start RQ worker
start_bin_rq_worker() {
info_log "Starting RQ worker"
# Build Redis URL properly
local redis_url
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
# Set PYTHONPATH so RQ can find the tasks module
PYTHONPATH="/backend:${PYTHONPATH-}" rq worker \
--path /backend \
--pid /tmp/rq_worker.pid \
--url "${redis_url}" \
--results-ttl "${TASK_RESULT_TTL:-86400}" \
--logging_level "${LOGLEVEL}" \
high default low &
}
start_bin_watcher() {
info_log "Starting watcher"
if [[ ${OTEL_SDK_DISABLED:-false} == "true" ]]; then
# Skip opentelemetry-instrument when OTEL is disabled to ensure
# WATCHFILES_CHANGES env var is properly passed to watcher.py
watchfiles \
--target-type command \
"python3 watcher.py" \
/romm/library &
else
watchfiles \
--target-type command \
"opentelemetry-instrument --service_name '${OTEL_SERVICE_NAME_PREFIX-}watcher' python3 watcher.py" \
/romm/library &
fi
WATCHER_PID=$!
echo "${WATCHER_PID}" >/tmp/watcher.pid
}
start_bin_sync_watcher() {
info_log "Starting sync folder watcher"
if [[ ${OTEL_SDK_DISABLED:-false} == "true" ]]; then
watchfiles \
--target-type command \
"python3 sync_watcher.py" \
/romm/sync &
else
watchfiles \
--target-type command \
"opentelemetry-instrument --service_name '${OTEL_SERVICE_NAME_PREFIX-}sync_watcher' python3 sync_watcher.py" \
/romm/sync &
fi
SYNC_WATCHER_PID=$!
echo "${SYNC_WATCHER_PID}" >/tmp/sync_watcher.pid
}
watchdog_process_pid() {
PROCESS=$1
if [[ -f "/tmp/${PROCESS}.pid" ]]; then
# Check if the pid we last wrote to our state file is actually active
PID=$(cat "/tmp/${PROCESS}.pid") || true
if [[ ! -d "/proc/${PID}" ]]; then
start_bin_"${PROCESS}"
fi
else
# Start process if we dont have a corresponding PID file
start_bin_"${PROCESS}"
fi
}
stop_process_pid() {
PROCESS=$1
if [[ -f "/tmp/${PROCESS}.pid" ]]; then
PID=$(cat "/tmp/${PROCESS}.pid") || true
if [[ -d "/proc/${PID}" ]]; then
info_log "Stopping ${PROCESS}"
kill "${PID}" || true
# wait for process exit
while [[ -e "/proc/${PID}" ]]; do sleep 0.1; done
fi
fi
}
shutdown() {
# shutdown in reverse order
stop_process_pid rq_worker
stop_process_pid rq_scheduler
stop_process_pid sync_watcher
stop_process_pid watcher
stop_process_pid nginx
stop_process_pid gunicorn
stop_process_pid valkey-server
}
# switch to backend directory
cd /backend || { error_log "/backend directory doesn't seem to exist"; }
print_banner
# setup trap handler
exited=0
trap 'exited=1 && shutdown' SIGINT SIGTERM EXIT
# clear any leftover PID files
rm /tmp/*.pid -f
# Disable OpenTelemetry if no OTEL_ prefixed environment variables are set
if ! printenv | grep -q '^OTEL_'; then
info_log "No OpenTelemetry environment variables found, disabling OpenTelemetry SDK"
export OTEL_SDK_DISABLED=true
fi
# 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))")
info_log "ROMM_AUTH_SECRET_KEY not set, generating random secret key"
export ROMM_AUTH_SECRET_KEY
fi
# Start Valkey server if REDIS_HOST is not set (which would mean user is using an external Redis/Valkey)
if [[ -z ${REDIS_HOST} ]]; then
watchdog_process_pid valkey-server
else
info_log "REDIS_HOST is set, not starting internal valkey-server"
fi
# Run needed database migrations once at startup
info_log "Running database migrations"
if alembic upgrade head; then
info_log "Database migrations succeeded"
else
error_log "Failed to run database migrations"
fi
# Startup process requires database and cache to be already available
run_startup
# main loop
while ! ((exited)); do
watchdog_process_pid gunicorn
# only start the scheduler if enabled
if [[ ${ENABLE_SCHEDULED_RESCAN} == "true" || ${ENABLE_SCHEDULED_UPDATE_SWITCH_TITLEDB} == "true" || ${ENABLE_SCHEDULED_UPDATE_LAUNCHBOX_METADATA} == "true" ]]; then
watchdog_process_pid rq_scheduler
fi
watchdog_process_pid rq_worker
# only start the watcher if enabled
if [[ ${ENABLE_RESCAN_ON_FILESYSTEM_CHANGE} == "true" ]]; then
watchdog_process_pid watcher
fi
# only start the sync folder watcher if enabled
if [[ ${ENABLE_SYNC_FOLDER_WATCHER} == "true" ]]; then
watchdog_process_pid sync_watcher
fi
watchdog_process_pid nginx
# check for died processes every 5 seconds
sleep 5
done