AwaitHealthy waits for the ServiceManager to become healthy. Returns nil, if manager is healthy, error otherwise (eg. manager is in a state in which it cannot get healthy anymore).
(ctx context.Context)
| 114 | // AwaitHealthy waits for the ServiceManager to become healthy. Returns nil, if manager is healthy, error otherwise (eg. manager |
| 115 | // is in a state in which it cannot get healthy anymore). |
| 116 | func (m *Manager) AwaitHealthy(ctx context.Context) error { |
| 117 | select { |
| 118 | case <-ctx.Done(): |
| 119 | return ctx.Err() |
| 120 | case <-m.healthyCh: |
| 121 | } |
| 122 | |
| 123 | m.mu.Lock() |
| 124 | defer m.mu.Unlock() |
| 125 | |
| 126 | if m.state != healthy { |
| 127 | terminated := len(m.byState[Terminated]) |
| 128 | |
| 129 | var failedReasons []string |
| 130 | for _, s := range m.byState[Failed] { |
| 131 | err := s.FailureCase() |
| 132 | if err != nil { |
| 133 | // err is never nil for a failed service. |
| 134 | failedReasons = append(failedReasons, err.Error()) |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | return fmt.Errorf("not healthy, %d terminated, %d failed: %v", terminated, len(failedReasons), failedReasons) |
| 139 | } |
| 140 | return nil |
| 141 | } |
| 142 | |
| 143 | // IsStopped returns true if all services are in terminal state (Terminated or Failed) |
| 144 | func (m *Manager) IsStopped() bool { |