|
- package server
-
- import (
- "context"
- "strings"
- "time"
-
- "github.com/AFASystems/presence/internal/pkg/common/appcontext"
- "github.com/segmentio/kafka-go"
- "gorm.io/gorm"
- )
-
- const healthCheckTimeout = 3 * time.Second
-
- // CheckKafkaHealth dials the broker and returns ServiceStatus. Uses first broker from kafkaURL (comma-separated).
- func CheckKafkaHealth(ctx context.Context, kafkaURL string) appcontext.ServiceStatus {
- if kafkaURL == "" {
- return appcontext.ServiceStatus{Status: "down", Message: "no broker URL"}
- }
- broker := strings.TrimSpace(strings.Split(kafkaURL, ",")[0])
- tctx, cancel := context.WithTimeout(ctx, healthCheckTimeout)
- defer cancel()
- dialer := &kafka.Dialer{Timeout: healthCheckTimeout}
- conn, err := dialer.DialContext(tctx, "tcp", broker)
- if err != nil {
- return appcontext.ServiceStatus{Status: "down", Message: err.Error()}
- }
- _ = conn.Close()
- return appcontext.ServiceStatus{Status: "up"}
- }
-
- // CheckDBHealth pings the database and returns ServiceStatus.
- func CheckDBHealth(ctx context.Context, db *gorm.DB) appcontext.ServiceStatus {
- sqlDB, err := db.DB()
- if err != nil {
- return appcontext.ServiceStatus{Status: "down", Message: err.Error()}
- }
- tctx, cancel := context.WithTimeout(ctx, healthCheckTimeout)
- defer cancel()
- if err := sqlDB.PingContext(tctx); err != nil {
- return appcontext.ServiceStatus{Status: "down", Message: err.Error()}
- }
- return appcontext.ServiceStatus{Status: "up"}
- }
|