dockerize app
This commit is contained in:
55
entrypoint.sh
Normal file
55
entrypoint.sh
Normal file
@@ -0,0 +1,55 @@
|
||||
#!/bin/sh
|
||||
set -e
|
||||
|
||||
# Default values
|
||||
PORT="${PORT:-8000}"
|
||||
HOST="${HOST:-0.0.0.0}"
|
||||
WORKERS="${WORKERS:-1}"
|
||||
LOG_LEVEL="${LOG_LEVEL:-info}"
|
||||
|
||||
# Display configuration
|
||||
echo "=========================================="
|
||||
echo "Starting FastAPI application"
|
||||
echo "=========================================="
|
||||
echo "Host: ${HOST}"
|
||||
echo "Port: ${PORT}"
|
||||
echo "Workers: ${WORKERS}"
|
||||
echo "Log Level: ${LOG_LEVEL}"
|
||||
echo "=========================================="
|
||||
|
||||
# Trap signals for graceful shutdown
|
||||
cleanup() {
|
||||
echo "Received termination signal, shutting down gracefully..."
|
||||
# Send SIGTERM to uvicorn process group
|
||||
if [ -n "$UVICORN_PID" ]; then
|
||||
kill -TERM "$UVICORN_PID" 2>/dev/null || true
|
||||
# Wait for uvicorn to exit, but with a timeout
|
||||
timeout 30 sh -c "while kill -0 $UVICORN_PID 2>/dev/null; do sleep 1; done" || {
|
||||
echo "Uvicorn did not shut down gracefully, forcing exit..."
|
||||
kill -KILL "$UVICORN_PID" 2>/dev/null || true
|
||||
}
|
||||
fi
|
||||
exit 0
|
||||
}
|
||||
|
||||
# Register signal handlers
|
||||
trap cleanup SIGTERM SIGINT
|
||||
|
||||
# Start uvicorn in the background to capture PID
|
||||
uvicorn app:app \
|
||||
--host "${HOST}" \
|
||||
--port "${PORT}" \
|
||||
--workers "${WORKERS}" \
|
||||
--log-level "${LOG_LEVEL}" \
|
||||
--access-log \
|
||||
&
|
||||
|
||||
UVICORN_PID=$!
|
||||
|
||||
# Wait for uvicorn process
|
||||
wait $UVICORN_PID
|
||||
EXIT_STATUS=$?
|
||||
|
||||
# Exit with the same status as uvicorn
|
||||
echo "Uvicorn exited with status: ${EXIT_STATUS}"
|
||||
exit ${EXIT_STATUS}
|
||||
Reference in New Issue
Block a user