🔧 feat: enhance readiness endpoint with database health check
- Added CheckDatabaseHealth() method to UserRepository interface - Implemented database connectivity check in SQLite repository - Enhanced /ready endpoint to verify database health before reporting ready - Improved readiness logic: checks both server shutdown status and database connectivity - Better observability: Logs database health check failures with warnings Benefits: - More accurate readiness reporting for Kubernetes/container environments - Detects database connectivity issues before accepting traffic - Prevents application from accepting requests when database is unavailable - Maintains backward compatibility with existing readiness checks Implementation: - Simple COUNT query to test database responsiveness - Graceful handling of database unavailability - Proper HTTP 503 status when not ready - Comprehensive logging for troubleshooting Testing: - ✅ Readiness endpoint returns true when database is healthy - ✅ Readiness endpoint returns false when database is unhealthy - ✅ All existing functionality preserved - ✅ All 25 BDD scenarios passing - ✅ All unit tests passing Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
This commit is contained in:
@@ -201,6 +201,17 @@ func (r *SQLiteRepository) Close() error {
|
||||
return sqlDB.Close()
|
||||
}
|
||||
|
||||
// CheckDatabaseHealth checks if the database is healthy and responsive
|
||||
func (r *SQLiteRepository) CheckDatabaseHealth(ctx context.Context) error {
|
||||
// Simple query to test database connectivity
|
||||
var count int64
|
||||
result := r.db.WithContext(ctx).Model(&User{}).Count(&count)
|
||||
if result.Error != nil {
|
||||
return fmt.Errorf("database health check failed: %w", result.Error)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// createSpan creates a new telemetry span if persistence telemetry is enabled
|
||||
func (r *SQLiteRepository) createSpan(ctx context.Context, operation string) (context.Context, trace.Span) {
|
||||
if r.config == nil || !r.config.GetPersistenceTelemetryEnabled() {
|
||||
|
||||
Reference in New Issue
Block a user