Implement comprehensive graceful shutdown with JSON logging

- Added signal.NotifyContext for modern signal handling in cmd/server/main.go
- Implemented BaseContext for proper context propagation to HTTP handlers
- Added readiness drain delay before shutdown for graceful degradation
- Fixed PID detection in start-server.sh to target actual server process
- Added Logging.JSON configuration option with DLC_LOGGING_JSON environment variable
- Created comprehensive test script that validates entire server lifecycle
- Updated documentation with JSON logging configuration examples
- All shutdown logs now appear correctly in JSON format
- Server terminates gracefully on SIGTERM with proper log flushing

The graceful shutdown implementation follows VictoriaMetrics best practices:
1. Catches termination signals (SIGTERM, SIGINT)
2. Stops accepting new requests but allows ongoing requests to complete
3. Waits for active requests to finish within configured timeout
4. Releases resources and performs cleanup
5. Logs all shutdown steps for observability

Test script validates:
- Server startup and API functionality
- Graceful shutdown sequence
- JSON log format validation
- Complete log sequence verification
- Proper signal handling and context propagation

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
This commit is contained in:
Gabriel Radureau
2026-04-03 19:23:23 +02:00
parent 736ec9c996
commit 7c5e61c386
9 changed files with 317 additions and 60 deletions

View File

@@ -16,34 +16,43 @@ cd "$PROJECT_DIR" || { echo "Failed to change to project directory"; exit 1; }
start_server() {
echo "Starting DanceLessonsCoach server..."
# Kill any existing server
# Check if server is already running
if [ -f "$PID_FILE" ]; then
echo "Found existing PID file, stopping previous server..."
stop_server
if ps -p $(cat $PID_FILE) > /dev/null; then
echo "Server is already running (PID: $(cat $PID_FILE))"
return 0
else
echo "Found stale PID file, cleaning up..."
rm -f "$PID_FILE"
fi
fi
# Start server in background with colors disabled for log file
export DLC_NO_COLOR=1
echo '' > "$LOG_FILE"
nohup $SERVER_CMD > "$LOG_FILE" 2>&1 &
echo $! > "$PID_FILE"
# Wait a moment for server to start and get the actual server PID
sleep 2
# Get the PID of the process actually listening on port 8080
ACTUAL_PID=$(lsof -ti :8080 2>/dev/null || echo "")
if [ -z "$ACTUAL_PID" ]; then
echo "Failed to start server - no process listening on port 8080"
echo "Check log file for errors: $LOG_FILE"
rm -f "$PID_FILE"
unset DLC_NO_COLOR
return 1
fi
echo $ACTUAL_PID > "$PID_FILE"
unset DLC_NO_COLOR
echo "Server started with PID: $(cat $PID_FILE)"
echo "Log file: $LOG_FILE"
# Wait a moment for server to start
sleep 2
# Check if server is running
if ps -p $(cat $PID_FILE) > /dev/null; then
echo "Server is running successfully"
echo "Server address: http://localhost:8080"
else
echo "Failed to start server"
echo "Check log file for errors: $LOG_FILE"
rm -f "$PID_FILE"
return 1
fi
echo "Server address: http://localhost:8080"
echo "Server is running successfully"
}
# Function to stop the server