implement gunicorn WSGI
This commit is contained in:
+39
-27
@@ -1,36 +1,48 @@
|
||||
#!/bin/bash
|
||||
#!/usr/bin/env sh
|
||||
set -e
|
||||
|
||||
# Default to sqlite if not specified
|
||||
DB_TYPE="${DB_TYPE:-sqlite}"
|
||||
# Ensure Flask CLI finds your app
|
||||
export FLASK_APP=run.py
|
||||
export FLASK_ENV=production
|
||||
|
||||
# Wait for PostgreSQL if needed
|
||||
if [ "$DB_TYPE" = "postgresql" ]; then
|
||||
MAX_WAIT=180
|
||||
WAIT_INTERVAL=2
|
||||
WAITED=0
|
||||
echo "Database type is PostgreSQL. Waiting for database to be ready..."
|
||||
until pg_isready -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" > /dev/null 2>&1; do
|
||||
if [ $WAITED -ge $MAX_WAIT ]; then
|
||||
echo "Timeout: Could not connect to the database at $DB_HOST:$DB_PORT within $MAX_WAIT seconds."
|
||||
# --- Migrations with simple retry (no pg_isready needed) ---
|
||||
if [ "${RUN_DB_MIGRATIONS:-1}" = "1" ]; then
|
||||
ATTEMPTS="${MIGRATION_MAX_ATTEMPTS:-30}" # ~90s default with 3s sleep
|
||||
SLEEP_SECS="${MIGRATION_RETRY_SLEEP:-3}"
|
||||
i=1
|
||||
echo "Applying migrations (up to $ATTEMPTS attempts)..."
|
||||
until flask db upgrade; do
|
||||
if [ "$i" -ge "$ATTEMPTS" ]; then
|
||||
echo "ERROR: Migrations failed after $ATTEMPTS attempts."
|
||||
exit 1
|
||||
fi
|
||||
echo "Waiting for database at $DB_HOST:$DB_PORT... ($WAITED/$MAX_WAIT seconds)"
|
||||
sleep $WAIT_INTERVAL
|
||||
WAITED=$((WAITED + WAIT_INTERVAL))
|
||||
echo "DB not ready yet; retrying in ${SLEEP_SECS}s ($i/$ATTEMPTS)…"
|
||||
sleep "$SLEEP_SECS"
|
||||
i=$((i+1))
|
||||
done
|
||||
echo "Database is ready."
|
||||
else
|
||||
echo "Database type is SQLite. Skipping PostgreSQL readiness check."
|
||||
fi
|
||||
|
||||
# Apply migrations
|
||||
echo "Applying database migrations..."
|
||||
flask db upgrade
|
||||
# --- Start the image import worker exactly once per container ---
|
||||
if [ "${RUN_IMAGE_IMPORTER:-1}" = "1" ]; then
|
||||
echo "Starting image import worker (background)…"
|
||||
python image_import_worker.py &
|
||||
fi
|
||||
|
||||
# Start periodic image import in background
|
||||
echo "import worker waiting for flag"
|
||||
python image_import_worker.py &
|
||||
# --- Start Gunicorn ---
|
||||
GUNICORN_BIND="${GUNICORN_BIND:-0.0.0.0:5000}"
|
||||
GUNICORN_WORKERS="${GUNICORN_WORKERS:-2}"
|
||||
GUNICORN_THREADS="${GUNICORN_THREADS:-4}"
|
||||
GUNICORN_TIMEOUT="${GUNICORN_TIMEOUT:-120}"
|
||||
GUNICORN_OPTS="${GUNICORN_OPTS:-}"
|
||||
|
||||
# Start the Flask app
|
||||
echo "Starting Flask app..."
|
||||
exec flask run --host=0.0.0.0 --port=5000
|
||||
echo "Starting Gunicorn on ${GUNICORN_BIND} …"
|
||||
exec gunicorn \
|
||||
--bind "${GUNICORN_BIND}" \
|
||||
--workers "${GUNICORN_WORKERS}" \
|
||||
--worker-class gthread \
|
||||
--threads "${GUNICORN_THREADS}" \
|
||||
--timeout "${GUNICORN_TIMEOUT}" \
|
||||
--access-logfile - \
|
||||
--error-logfile - \
|
||||
${GUNICORN_OPTS} \
|
||||
run:app
|
||||
|
||||
Reference in New Issue
Block a user