health.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package container
  2. import (
  3. "github.com/Sirupsen/logrus"
  4. "github.com/docker/engine-api/types"
  5. )
  6. // Health holds the current container health-check state
  7. type Health struct {
  8. types.Health
  9. stop chan struct{} // Write struct{} to stop the monitor
  10. }
  11. // String returns a human-readable description of the health-check state
  12. func (s *Health) String() string {
  13. // This happens when the container is being shutdown and the monitor has stopped
  14. // or the monitor has yet to be setup.
  15. if s.stop == nil {
  16. return types.Unhealthy
  17. }
  18. switch s.Status {
  19. case types.Starting:
  20. return "health: starting"
  21. default: // Healthy and Unhealthy are clear on their own
  22. return s.Status
  23. }
  24. }
  25. // OpenMonitorChannel creates and returns a new monitor channel. If there already is one,
  26. // it returns nil.
  27. func (s *Health) OpenMonitorChannel() chan struct{} {
  28. if s.stop == nil {
  29. logrus.Debug("OpenMonitorChannel")
  30. s.stop = make(chan struct{})
  31. return s.stop
  32. }
  33. return nil
  34. }
  35. // CloseMonitorChannel closes any existing monitor channel.
  36. func (s *Health) CloseMonitorChannel() {
  37. if s.stop != nil {
  38. logrus.Debug("CloseMonitorChannel: waiting for probe to stop")
  39. // This channel does not buffer. Once the write succeeds, the monitor
  40. // has read the stop request and will not make any further updates
  41. // to c.State.Health.
  42. s.stop <- struct{}{}
  43. s.stop = nil
  44. logrus.Debug("CloseMonitorChannel done")
  45. }
  46. }