Files

46 lines
1019 B
Go

package main
import (
"context"
"fmt"
"net/http"
"time"
"github.com/spf13/cobra"
)
func newHealthcheckCmd() *cobra.Command {
var port int
cmd := &cobra.Command{
Use: "healthcheck",
Short: "Check the scheduler health endpoint",
Hidden: true,
RunE: func(cmd *cobra.Command, args []string) error {
ctx, cancel := context.WithTimeout(cmd.Context(), 3*time.Second)
defer cancel()
url := fmt.Sprintf("http://127.0.0.1:%d/healthz", port)
request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return fmt.Errorf("build health request: %w", err)
}
response, err := http.DefaultClient.Do(request)
if err != nil {
return fmt.Errorf("health request failed: %w", err)
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return fmt.Errorf("health check returned status %d", response.StatusCode)
}
return nil
},
}
cmd.Flags().IntVar(&port, "port", 8080, "Health check server port")
return cmd
}