2016-04-18 09:48:13 +00:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2016-09-06 18:18:12 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
2017-07-26 21:42:13 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2016-04-18 09:48:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Health holds the current container health-check state
|
|
|
|
type Health struct {
|
|
|
|
types.Health
|
|
|
|
stop chan struct{} // Write struct{} to stop the monitor
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns a human-readable description of the health-check state
|
|
|
|
func (s *Health) String() string {
|
2017-04-06 21:42:10 +00:00
|
|
|
// This happens when the monitor has yet to be setup.
|
|
|
|
if s.Status == "" {
|
2016-07-21 20:02:12 +00:00
|
|
|
return types.Unhealthy
|
2016-04-18 09:48:13 +00:00
|
|
|
}
|
2016-07-21 20:02:12 +00:00
|
|
|
|
2016-04-18 09:48:13 +00:00
|
|
|
switch s.Status {
|
|
|
|
case types.Starting:
|
|
|
|
return "health: starting"
|
|
|
|
default: // Healthy and Unhealthy are clear on their own
|
|
|
|
return s.Status
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// OpenMonitorChannel creates and returns a new monitor channel. If there already is one,
|
|
|
|
// it returns nil.
|
|
|
|
func (s *Health) OpenMonitorChannel() chan struct{} {
|
|
|
|
if s.stop == nil {
|
2016-06-11 20:16:55 +00:00
|
|
|
logrus.Debug("OpenMonitorChannel")
|
2016-04-18 09:48:13 +00:00
|
|
|
s.stop = make(chan struct{})
|
|
|
|
return s.stop
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CloseMonitorChannel closes any existing monitor channel.
|
|
|
|
func (s *Health) CloseMonitorChannel() {
|
|
|
|
if s.stop != nil {
|
2016-06-11 20:16:55 +00:00
|
|
|
logrus.Debug("CloseMonitorChannel: waiting for probe to stop")
|
2016-11-16 02:02:26 +00:00
|
|
|
close(s.stop)
|
2016-04-18 09:48:13 +00:00
|
|
|
s.stop = nil
|
2017-04-06 21:42:10 +00:00
|
|
|
// unhealthy when the monitor has stopped for compatibility reasons
|
|
|
|
s.Status = types.Unhealthy
|
2016-06-11 20:16:55 +00:00
|
|
|
logrus.Debug("CloseMonitorChannel done")
|
2016-04-18 09:48:13 +00:00
|
|
|
}
|
|
|
|
}
|