Display the full log as a readable summary before cleanup so the
complete server lifecycle is visible in test output without needing
to preserve the log file manually.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Print the full response body and status code from the /api/ready probe
that fires during the graceful shutdown window, so the 200→503 transition
is visible in test output rather than silently discarded.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Three related issues fixed together:
1. Readiness context was never cancelled during shutdown
server.Run() had a type assertion for a Cancel() method that no standard
context.Context implements, so readiness stayed "ready" through the entire
shutdown window. Added CancelableContext to pkg/server — a thin wrapper that
exposes Cancel() — and switched cmd/server/main.go to use it. Test servers
and CLI continue passing context.Background() unchanged.
2. "Server exited" log was never emitted
The test script expected it; main.go had no log after server.Run() returned.
Added log.Trace().Msg("Server exited") after the Run() call.
3. Double-SIGTERM caused non-JSON "signal: terminated" in server.log
test-graceful-shutdown.sh sent SIGTERM, then called $SERVER_CMD stop which
sent a second SIGTERM. After signal.NotifyContext is cancelled, the second
signal hits the default handler and Go prints "signal: terminated" to stderr,
breaking the all-JSON-lines assertion. Fixed by waiting for the PID to exit
ourselves instead of re-invoking the stop script.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
The logger was initialised to console format unconditionally, so the
"Config file loaded" message (and similar early logs) were always written
as human-readable text even when JSON logging was configured.
Root cause: classic chicken-and-egg — the format flag lives inside the
config that is being loaded.
Fix: add peekJSONLogging() which resolves the format before any log is
emitted by (1) checking DLC_LOGGING_JSON directly via os.Getenv, then
(2) doing a minimal throwaway Viper pre-read of the config file for the
logging.json key. The redundant format-switch block that ran after
Unmarshal() is removed.
Also add the "Logging configured" log as the very first line, and
replace the hardcoded PROJECT_DIR path in start-server.sh,
test-graceful-shutdown.sh, and test-opentelemetry.sh with a dynamic
derivation from BASH_SOURCE[0].
Closes#15
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Implement readiness endpoint (/api/ready) that returns:
- {"ready":true} (HTTP 200) during normal operation
- {"ready":false} (HTTP 503) during graceful shutdown
Key changes:
- Added readiness context to control readiness state
- Modified server.NewServer() to accept readiness context
- Implemented handleReadiness() with context-aware logic
- Updated cmd/server/main.go to manage readiness state
- Readiness set to false when shutdown signal received
- Updated test script to validate readiness behavior
- Added comprehensive documentation for readiness endpoint
This allows Kubernetes/service meshes to stop routing traffic
to the pod during graceful shutdown while allowing existing
requests to complete. Health endpoint continues to return
happy status during shutdown for proper orchestration.
- 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>