No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 

45 líneas
1.4 KiB

  1. package server
  2. import (
  3. "context"
  4. "strings"
  5. "time"
  6. "github.com/AFASystems/presence/internal/pkg/common/appcontext"
  7. "github.com/segmentio/kafka-go"
  8. "gorm.io/gorm"
  9. )
  10. const healthCheckTimeout = 3 * time.Second
  11. // CheckKafkaHealth dials the broker and returns ServiceStatus. Uses first broker from kafkaURL (comma-separated).
  12. func CheckKafkaHealth(ctx context.Context, kafkaURL string) appcontext.ServiceStatus {
  13. if kafkaURL == "" {
  14. return appcontext.ServiceStatus{Status: "down", Message: "no broker URL"}
  15. }
  16. broker := strings.TrimSpace(strings.Split(kafkaURL, ",")[0])
  17. tctx, cancel := context.WithTimeout(ctx, healthCheckTimeout)
  18. defer cancel()
  19. dialer := &kafka.Dialer{Timeout: healthCheckTimeout}
  20. conn, err := dialer.DialContext(tctx, "tcp", broker)
  21. if err != nil {
  22. return appcontext.ServiceStatus{Status: "down", Message: err.Error()}
  23. }
  24. _ = conn.Close()
  25. return appcontext.ServiceStatus{Status: "up"}
  26. }
  27. // CheckDBHealth pings the database and returns ServiceStatus.
  28. func CheckDBHealth(ctx context.Context, db *gorm.DB) appcontext.ServiceStatus {
  29. sqlDB, err := db.DB()
  30. if err != nil {
  31. return appcontext.ServiceStatus{Status: "down", Message: err.Error()}
  32. }
  33. tctx, cancel := context.WithTimeout(ctx, healthCheckTimeout)
  34. defer cancel()
  35. if err := sqlDB.PingContext(tctx); err != nil {
  36. return appcontext.ServiceStatus{Status: "down", Message: err.Error()}
  37. }
  38. return appcontext.ServiceStatus{Status: "up"}
  39. }