Skip to content

Commit

Permalink
If the websocket fails to connect initially, create a whole new clien…
Browse files Browse the repository at this point in the history
…t (so certs are reloaded) in case we have the wrong certs

(this solves an edge case in operator where we grab certs between inits)
  • Loading branch information
cmmarslender committed Sep 12, 2024
1 parent 3636588 commit f19b65a
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,27 @@ 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
}

go h.DNSCheckLoop()
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 +49,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 {
func startWebsocket(h *healthcheck.Healthcheck) error {
err := h.OpenWebsocket()
if err != nil {
log.Println(err.Error())
time.Sleep(5 * time.Second)
continue
return err
}
break
}
return nil
}

0 comments on commit f19b65a

Please sign in to comment.