Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#2997] More resilient uWSGI process/worker management #1589

Merged
merged 1 commit into from
Feb 3, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 53 additions & 21 deletions bin/docker_start.sh
Original file line number Diff line number Diff line change
@@ -12,29 +12,61 @@ ${SCRIPTPATH}/wait_for_db.sh

# fixtures_dir=${FIXTURES_DIR:-/app/fixtures}

uwsgi_port=${UWSGI_PORT:-8000}
uwsgi_processes=${UWSGI_PROCESSES:-4}
uwsgi_threads=${UWSGI_THREADS:-8}
uwsgi_http_timeout=${UWSGI_HTTP_TIMEOUT:-120}

# Apply database migrations
>&2 echo "Apply database migrations"
python src/manage.py migrate

# Start server
##
# uWSGI settings
##

# --- Basic Application Configuration
export UWSGI_MODULE="open_inwoner.wsgi"
export UWSGI_CHDIR="src"
export UWSGI_STATIC_MAP="/static=/app/static /media=/app/media"

# --- Process Management
export UWSGI_MASTER=true
export UWSGI_PROCESSES=${UWSGI_PROCESSES:-4}
export UWSGI_THREADS=${UWSGI_THREADS:-8}

# Shutdown gracefully on SIGTERM (common with supervisord and k8s)
export UWSGI_DIE_ON_TERM=true

# We're not hosting multiple apps, so no isolation required
export UWSGI_SINGLE_INTERPRETER=true

# Enable Python threading
export UWSGI_ENABLE_THREADS=true

# --- HTTP Server Settings
export UWSGI_HTTP=${UWSGI_PORT:-8000}
export UWSGI_HTTP_TIMEOUT=${UWSGI_HTTP_TIMEOUT:-120}
export UWSGI_HTTP_KEEPALIVE=true

# --- Request Handling
# Buffer size for POST requests
export UWSGI_POST_BUFFERING=8192

# Internal buffer size
export UWSGI_BUFFER_SIZE=65535

# --- Worker Lifecycle
# Make UWSGI_MAX_REQUESTS explicitly opt-in
if [ -n "${UWSGI_MAX_REQUESTS+x}" ]; then
if [ "$UWSGI_MAX_REQUESTS" -gt 1 ] 2>/dev/null; then
export UWSGI_MAX_REQUESTS
else
echo "Warning: UWSGI_MAX_REQUESTS must be greater than 1. The variable will be unset."
unset UWSGI_MAX_REQUESTS
fi
else
unset UWSGI_MAX_REQUESTS
fi

# Hard-kill requests after 60 seconds
export UWSGI_HARAKIRI=${UWSGI_HARAKIRI:-60}

# Start Server
>&2 echo "Starting server"
exec uwsgi \
--die-on-term \
--http :$uwsgi_port \
--http-keepalive \
--http-timeout $uwsgi_http_timeout \
--module open_inwoner.wsgi \
--static-map /static=/app/static \
--static-map /media=/app/media \
--chdir src \
--enable-threads \
--processes $uwsgi_processes \
--threads $uwsgi_threads \
--post-buffering=8192 \
--buffer-size=65535
# processes & threads are needed for concurrency without nginx sitting inbetween
exec uwsgi --show-config --strict