Spaces:
Sleeping
Sleeping
Seth McKnight
Copilot
commited on
Commit
·
6338213
1
Parent(s):
241bf1b
Implement database initialization with retries (#86)
Browse files* Implement database initialization with retries
Added additional logging & retry mechanism for database initialization in run.sh.
* Update run.sh
Co-authored-by: Copilot <[email protected]>
---------
Co-authored-by: Copilot <[email protected]>
run.sh
CHANGED
|
@@ -6,10 +6,32 @@ WORKERS_VALUE="${WORKERS:-1}"
|
|
| 6 |
TIMEOUT_VALUE="${TIMEOUT:-120}"
|
| 7 |
PORT_VALUE="${PORT:-10000}"
|
| 8 |
|
| 9 |
-
# Initialize the database
|
| 10 |
echo "Initializing database..."
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
echo "Starting gunicorn on port ${PORT_VALUE} with ${WORKERS_VALUE} workers and timeout ${TIMEOUT_VALUE}s"
|
| 14 |
export PYTHONPATH="/app${PYTHONPATH:+:$PYTHONPATH}"
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
TIMEOUT_VALUE="${TIMEOUT:-120}"
|
| 7 |
PORT_VALUE="${PORT:-10000}"
|
| 8 |
|
| 9 |
+
# Initialize the database with retries (handle DB cold start)
|
| 10 |
echo "Initializing database..."
|
| 11 |
+
MAX_RETRIES="${DB_INIT_RETRIES:-10}"
|
| 12 |
+
SLEEP_BASE="${DB_INIT_SLEEP_BASE:-5}"
|
| 13 |
+
|
| 14 |
+
i=1
|
| 15 |
+
until python scripts/init_pgvector.py; do
|
| 16 |
+
if [ "$i" -ge "$MAX_RETRIES" ]; then
|
| 17 |
+
echo "Database initialization failed after $i attempts; exiting."
|
| 18 |
+
exit 1
|
| 19 |
+
fi
|
| 20 |
+
echo "init_pgvector.py failed (attempt $i/$MAX_RETRIES). Sleeping $((SLEEP_BASE ** i))s before retry..."
|
| 21 |
+
sleep $((SLEEP_BASE ** i))
|
| 22 |
+
i=$((i + 1))
|
| 23 |
+
done
|
| 24 |
|
| 25 |
echo "Starting gunicorn on port ${PORT_VALUE} with ${WORKERS_VALUE} workers and timeout ${TIMEOUT_VALUE}s"
|
| 26 |
export PYTHONPATH="/app${PYTHONPATH:+:$PYTHONPATH}"
|
| 27 |
+
|
| 28 |
+
# Use explicit logging options so any worker crash or import error is visible
|
| 29 |
+
exec gunicorn \
|
| 30 |
+
--bind 0.0.0.0:${PORT_VALUE} \
|
| 31 |
+
--workers "${WORKERS_VALUE}" \
|
| 32 |
+
--timeout "${TIMEOUT_VALUE}" \
|
| 33 |
+
--log-level debug \
|
| 34 |
+
--access-logfile - \
|
| 35 |
+
--error-logfile - \
|
| 36 |
+
--capture-output \
|
| 37 |
+
app:app
|