Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: healthcheck not waiting for all services to start #1419

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
21 changes: 21 additions & 0 deletions 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,25 @@ import (
// @Router /health [get]
//
// @id HealthCheck

func HealthCheck(ctx *gin.Context) {
cfg, err := server.GetConfig()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the direction you were going for here but, because all of these components are separated, we shouldn't mix them together here.

The headscale port and local builder registry port can be checked in the server command directly instead of the API here.

Furthermore, the local builder registry might not even be active (because the user disabled it) so you need to take that into account.

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
}
}

if !server.AllProviderRegistered {
ctx.JSON(http.StatusServiceUnavailable, gin.H{"status": "error"})
}

ctx.JSON(http.StatusOK, gin.H{"status": "ok"})
}
3 changes: 3 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 AllProviderRegistered = false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of this, I suggest that the provider manager has an IsInitialized function that will return true if it is initialized.

Then you can expose an endpoint in the provider controller that will return the "health" of the providers


func (s *Server) downloadDefaultProviders() error {
manifest, err := s.ProviderManager.GetProvidersManifest()
if err != nil {
Expand Down Expand Up @@ -122,6 +124,7 @@ func (s *Server) registerProviders() error {
}

log.Info("Providers registered")
AllProviderRegistered = true

return nil
}
Expand Down
Loading