Skip to content

Commit

Permalink
fix: healthcheck not waiting for all services to start
Browse files Browse the repository at this point in the history
Signed-off-by: tarunrajput <[email protected]>
  • Loading branch information
tarunrajput committed Dec 5, 2024
1 parent 817db7e commit 9b6b17e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion internal/util/apiclient/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
25 changes: 24 additions & 1 deletion pkg/api/controllers/health/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
package health

import (
"fmt"
"net/http"

"github.com/daytonaio/daytona/pkg/server"
"github.com/gin-gonic/gin"
)

Expand All @@ -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"})
}
}
4 changes: 4 additions & 0 deletions pkg/server/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -122,6 +124,8 @@ func (s *Server) registerProviders() error {
}

log.Info("Providers registered")
ProvidersRegisteredChan <- true
close(ProvidersRegisteredChan)

return nil
}
Expand Down

0 comments on commit 9b6b17e

Please sign in to comment.