health.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package container
  2. import (
  3. "github.com/docker/docker/api/types"
  4. "github.com/sirupsen/logrus"
  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 monitor has yet to be setup.
  14. if s.Status == "" {
  15. return types.Unhealthy
  16. }
  17. switch s.Status {
  18. case types.Starting:
  19. return "health: starting"
  20. default: // Healthy and Unhealthy are clear on their own
  21. return s.Status
  22. }
  23. }
  24. // OpenMonitorChannel creates and returns a new monitor channel. If there already is one,
  25. // it returns nil.
  26. func (s *Health) OpenMonitorChannel() chan struct{} {
  27. if s.stop == nil {
  28. logrus.Debug("OpenMonitorChannel")
  29. s.stop = make(chan struct{})
  30. return s.stop
  31. }
  32. return nil
  33. }
  34. // CloseMonitorChannel closes any existing monitor channel.
  35. func (s *Health) CloseMonitorChannel() {
  36. if s.stop != nil {
  37. logrus.Debug("CloseMonitorChannel: waiting for probe to stop")
  38. close(s.stop)
  39. s.stop = nil
  40. // unhealthy when the monitor has stopped for compatibility reasons
  41. s.Status = types.Unhealthy
  42. logrus.Debug("CloseMonitorChannel done")
  43. }
  44. }