diff --git a/internal/util/apiclient/error.go b/internal/util/apiclient/error.go index 6d58607b72..780325b08f 100644 --- a/internal/util/apiclient/error.go +++ b/internal/util/apiclient/error.go @@ -9,7 +9,7 @@ import ( ) func ErrHealthCheckFailed(healthUrl string) error { - return fmt.Errorf("failed to check server health at: %s. Make sure Daytona is running on the appropriate port", healthUrl) + return fmt.Errorf("failed to check server health at: %s. Make sure all Daytona services are running on the appropriate ports", healthUrl) } func IsHealthCheckFailed(err error) bool { diff --git a/pkg/api/controllers/health/health.go b/pkg/api/controllers/health/health.go index 3f15a3df8d..57526c2682 100644 --- a/pkg/api/controllers/health/health.go +++ b/pkg/api/controllers/health/health.go @@ -4,8 +4,10 @@ package health import ( + "fmt" "net/http" + "github.com/daytonaio/daytona/pkg/server" "github.com/gin-gonic/gin" ) @@ -18,6 +20,27 @@ import ( // @Router /health [get] // // @id HealthCheck + func HealthCheck(ctx *gin.Context) { - ctx.JSON(http.StatusOK, gin.H{"status": "ok"}) + cfg, err := server.GetConfig() + if err != nil { + ctx.JSON(http.StatusInternalServerError, gin.H{"status": "error"}) + return + } + + services := []uint32{cfg.HeadscalePort, cfg.LocalBuilderRegistryPort} + for _, port := range services { + if _, err := http.Get(fmt.Sprintf("http://localhost:%d", port)); err != nil { + ctx.JSON(http.StatusServiceUnavailable, gin.H{"status": "error"}) + return + } + } + + select { + case <-server.ProvidersRegisteredChan: + ctx.JSON(http.StatusOK, gin.H{"status": "ok"}) + return + default: + ctx.JSON(http.StatusServiceUnavailable, gin.H{"status": "error"}) + } } diff --git a/pkg/server/providers.go b/pkg/server/providers.go index 7c55961b47..9404844262 100644 --- a/pkg/server/providers.go +++ b/pkg/server/providers.go @@ -13,6 +13,8 @@ import ( log "github.com/sirupsen/logrus" ) +var ProvidersRegisteredChan = make(chan bool) + func (s *Server) downloadDefaultProviders() error { manifest, err := s.ProviderManager.GetProvidersManifest() if err != nil { @@ -122,6 +124,8 @@ func (s *Server) registerProviders() error { } log.Info("Providers registered") + ProvidersRegisteredChan <- true + close(ProvidersRegisteredChan) return nil }