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

@@ -155,6 +155,7 @@ The server supports flexible configuration through environment variables with th
| Host | `DLC_SERVER_HOST` | `0.0.0.0` | Server bind address |
| Port | `DLC_SERVER_PORT` | `8080` | Server listening port |
| Shutdown Timeout | `DLC_SHUTDOWN_TIMEOUT` | `30s` | Graceful shutdown timeout |
| JSON Logging | `DLC_LOGGING_JSON` | `false` | Enable JSON format logging |
**Usage Examples:**
@@ -171,6 +172,10 @@ go run cmd/server/main.go &
# Custom shutdown timeout
export DLC_SHUTDOWN_TIMEOUT=45s
go run cmd/server/main.go &
# Enable JSON logging
export DLC_LOGGING_JSON=true
go run cmd/server/main.go &
```
**Configuration File Support:**
@@ -192,6 +197,9 @@ server:
shutdown:
timeout: 30s
logging:
json: false
```
**Configuration Loading:**