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

If the websocket fails to connect initially, create a whole new clien… #42

Merged
merged 3 commits into from
Sep 12, 2024
Merged
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
40 changes: 23 additions & 17 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,26 @@ var serveCmd = &cobra.Command{
log.Fatalf("Error parsing log level: %s\n", err.Error())
}

h, err := healthcheck.NewHealthcheck(uint16(viper.GetInt("healthcheck-port")), level)
if err != nil {
log.Fatalln(err.Error())
var h *healthcheck.Healthcheck

// Loop until we get a connection or cancel
// It just retries every 5 seconds to connect to the RPC server until it succeeds or the app is stopped
for {
h, err = healthcheck.NewHealthcheck(uint16(viper.GetInt("healthcheck-port")), level)
if err != nil {
log.Fatalln(err.Error())
}

err = startWebsocket(h)
if err != nil {
log.Printf("error starting websocket. Creating new client and trying again in 5 seconds: %s\n", err.Error())
time.Sleep(5 * time.Second)
continue
}

break
}

// Run this in the background, so the metrics healthz endpoint can come up while waiting for Chia
go startWebsocket(h)
go h.DNSCheckLoop()

log.Fatalln(h.StartServer())
Expand All @@ -37,17 +50,10 @@ func init() {
rootCmd.AddCommand(serveCmd)
}

func startWebsocket(h *healthcheck.Healthcheck) {
// Loop until we get a connection or cancel
// This enables starting the healthcheck app even if the chia RPC service is not up/responding
// It just retries every 5 seconds to connect to the RPC server until it succeeds or the app is stopped
for {
err := h.OpenWebsocket()
if err != nil {
log.Println(err.Error())
time.Sleep(5 * time.Second)
continue
}
break
func startWebsocket(h *healthcheck.Healthcheck) error {
err := h.OpenWebsocket()
if err != nil {
return err
}
return nil
}