func setupHealthCheckEndpoint(healthPort int, log logging.Logger) { setupHealthCheckOnce.Do(func() { http.HandleFunc("/", serviceHealthCheck) err := http.ListenAndServe(fmt.Sprintf(":%d", healthPort), nil) if err != nil { log.Panic("Unable to start health check listener", err) } }) }
func waitForHealthy(healthCheckName string, health *api.Health, log logging.Logger) { for { checks, _, err := health.State("passing", nil) if err != nil { log.Panic("Unable to read health check status", err) } for _, check := range checks { if check.Name == healthCheckName { return } } time.Sleep(250 * time.Millisecond) } }
func registerSession(healthPort int, healthCheckName string, client *api.Client, log logging.Logger) string { go setupHealthCheckEndpoint(healthPort, log) checkReg := &api.AgentCheckRegistration{ Name: healthCheckName, } checkReg.AgentServiceCheck.HTTP = fmt.Sprintf("http://localhost:%d", healthPort) checkReg.AgentServiceCheck.Interval = "1s" err := client.Agent().CheckRegister(checkReg) if err != nil { log.Panic("Failed to register health check", err) } waitForHealthy(healthCheckName, client.Health(), log) sessionEntry := &api.SessionEntry{ Checks: []string{checkReg.Name}, } session, _, err := client.Session().Create(sessionEntry, nil) if err != nil { log.Panic("Unable to create session", err) } for { entry, _, err := client.Session().Info(session, nil) if err != nil { log.Panic("Unable to read session info", err) } if entry != nil { break } } return session }
func verifyUrl(URL string, log logging.Logger) { u, _ := url.Parse(URL) if u == nil || u.Host == "" { log.Panicf("Invalid URL: %s", URL) } }