|
1 | 1 | #!/bin/bash |
2 | | - |
3 | 2 | set -e |
4 | 3 |
|
| 4 | +# Function to gracefully shut down Gunicorn |
| 5 | +shutdown() { |
| 6 | + echo "Shutdown signal received. Stopping Gunicorn gracefully..." |
| 7 | + |
| 8 | + # Gracefully stop Gunicorn workers (stops accepting new requests) |
| 9 | + kill -SIGTERM $GUNICORN_PID |
| 10 | + |
| 11 | + # Allow some time for in-progress requests to complete |
| 12 | + echo "Waiting for ongoing requests to finish..." |
| 13 | + sleep 10 # Adjust time as needed |
| 14 | + |
| 15 | + # Force kill Gunicorn if it's still running after the grace period |
| 16 | + kill -9 $GUNICORN_PID 2>/dev/null || true |
| 17 | + |
| 18 | + echo "Shutdown complete." |
| 19 | + exit 0 |
| 20 | +} |
| 21 | + |
| 22 | +# Trap SIGTERM and SIGINT signals (container stop or AWS shutdown notice) |
| 23 | +trap shutdown SIGTERM SIGINT |
| 24 | + |
5 | 25 | # Apply database migrations |
6 | 26 | echo "Applying database migrations..." |
7 | 27 | python manage.py migrate |
8 | 28 |
|
9 | | -# Start supervisord |
10 | | -echo "Starting supervisord..." |
11 | | -exec supervisord -c /etc/supervisor/conf.d/supervisord.conf |
| 29 | +# Set up email templates with the --force option |
| 30 | +echo "Setting up email templates..." |
| 31 | +python manage.py setup_email_templates --force |
| 32 | + |
| 33 | +# Start Gunicorn in the background with graceful timeout |
| 34 | +echo "Starting Gunicorn..." |
| 35 | +gunicorn --bind 0.0.0.0:8000 setup.wsgi:application --workers 4 --timeout 90 & |
| 36 | + |
| 37 | +# Store the PID of the Gunicorn process |
| 38 | +GUNICORN_PID=$! |
| 39 | + |
| 40 | +# Poll AWS metadata service for spot termination notice |
| 41 | +while true; do |
| 42 | + TERMINATION_INFO=$(curl -s http://169.254.169.254/latest/meta-data/spot/instance-action || true) |
| 43 | + if [ -n "$TERMINATION_INFO" ]; then |
| 44 | + echo "AWS Spot termination notice detected! Gracefully shutting down..." |
| 45 | + shutdown |
| 46 | + fi |
| 47 | + sleep 5 |
| 48 | +done |
| 49 | + |
| 50 | +# Wait for Gunicorn to exit |
| 51 | +wait $GUNICORN_PID |
0 commit comments