| 45 | } |
| 46 | |
| 47 | func (in ContainerInstance) BestEffortWaitForHealthy(privatePort uint16) error { |
| 48 | port := in.publicPort(privatePort) |
| 49 | if len(port) == 0 { |
| 50 | return nil |
| 51 | } |
| 52 | checkACL := func(body []byte) error { |
| 53 | // Zero returns OK as response |
| 54 | if string(body) == "OK" { |
| 55 | return nil |
| 56 | } |
| 57 | |
| 58 | const eef string = `"ee_features"` |
| 59 | const acl string = `"acl"` |
| 60 | if !bytes.Contains(body, []byte(eef)) { |
| 61 | return errors.New("EE features are not enabled yet") |
| 62 | } |
| 63 | if bytes.Contains(body, []byte(acl)) { |
| 64 | return in.bestEffortTryLogin() |
| 65 | } |
| 66 | return nil |
| 67 | } |
| 68 | |
| 69 | tryWith := func(host string) error { |
| 70 | maxAttempts := 60 |
| 71 | for attempt := range maxAttempts { |
| 72 | resp, err := http.Get("http://" + host + ":" + port + "/health") |
| 73 | var body []byte |
| 74 | if resp != nil && resp.Body != nil { |
| 75 | body, _ = io.ReadAll(resp.Body) |
| 76 | _ = resp.Body.Close() |
| 77 | } |
| 78 | if err == nil && resp.StatusCode == http.StatusOK { |
| 79 | if aerr := checkACL(body); aerr == nil { |
| 80 | return nil |
| 81 | } else { |
| 82 | if attempt > 30 { |
| 83 | fmt.Printf("waiting for login to work: %v\n", aerr) |
| 84 | } |
| 85 | time.Sleep(time.Second) |
| 86 | continue |
| 87 | } |
| 88 | } |
| 89 | if attempt > 20 { |
| 90 | fmt.Printf("Health check %d for %s failed: %v. Response: %q. Retrying...\n", attempt, in, err, body) |
| 91 | } |
| 92 | time.Sleep(500 * time.Millisecond) |
| 93 | } |
| 94 | return fmt.Errorf("did not pass health check on %s", "http://"+host+":"+port+"/health\n") |
| 95 | } |
| 96 | |
| 97 | err := tryWith("0.0.0.0") |
| 98 | if err == nil { |
| 99 | return nil |
| 100 | } |
| 101 | |
| 102 | err = tryWith("localhost") |
| 103 | if err == nil { |
| 104 | return nil |