2016-04-18 12:48:13 +03:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2017-11-14 00:31:28 +03:00
|
|
|
"sync"
|
|
|
|
|
2016-09-06 21:18:12 +03:00
|
|
|
"github.com/docker/docker/api/types"
|
2017-07-27 00:42:13 +03:00
|
|
|
"github.com/sirupsen/logrus"
|
2016-04-18 12:48:13 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Health holds the current container health-check state
|
|
|
|
type Health struct {
|
|
|
|
types.Health
|
|
|
|
stop chan struct{} // Write struct{} to stop the monitor
|
2017-11-14 00:31:28 +03:00
|
|
|
mu sync.Mutex
|
2016-04-18 12:48:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// String returns a human-readable description of the health-check state
|
|
|
|
func (s *Health) String() string {
|
2017-11-16 06:28:36 +03:00
|
|
|
status := s.Status()
|
2016-07-21 23:02:12 +03:00
|
|
|
|
2017-11-16 06:28:36 +03:00
|
|
|
switch status {
|
2016-04-18 12:48:13 +03:00
|
|
|
case types.Starting:
|
|
|
|
return "health: starting"
|
|
|
|
default: // Healthy and Unhealthy are clear on their own
|
2017-11-16 06:28:36 +03:00
|
|
|
return s.Health.Status
|
2016-04-18 12:48:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-16 06:28:36 +03:00
|
|
|
// Status returns the current health status.
|
|
|
|
//
|
|
|
|
// Note that this takes a lock and the value may change after being read.
|
|
|
|
func (s *Health) Status() string {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
|
|
|
// This happens when the monitor has yet to be setup.
|
|
|
|
if s.Health.Status == "" {
|
|
|
|
return types.Unhealthy
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.Health.Status
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetStatus writes the current status to the underlying health structure,
|
|
|
|
// obeying the locking semantics.
|
|
|
|
//
|
|
|
|
// Status may be set directly if another lock is used.
|
|
|
|
func (s *Health) SetStatus(new string) {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
|
|
|
s.Health.Status = new
|
|
|
|
}
|
|
|
|
|
2017-11-14 00:31:28 +03:00
|
|
|
// OpenMonitorChannel creates and returns a new monitor channel. If there
|
|
|
|
// already is one, it returns nil.
|
2016-04-18 12:48:13 +03:00
|
|
|
func (s *Health) OpenMonitorChannel() chan struct{} {
|
2017-11-14 00:31:28 +03:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
2016-04-18 12:48:13 +03:00
|
|
|
if s.stop == nil {
|
2016-06-11 23:16:55 +03:00
|
|
|
logrus.Debug("OpenMonitorChannel")
|
2016-04-18 12:48:13 +03:00
|
|
|
s.stop = make(chan struct{})
|
|
|
|
return s.stop
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CloseMonitorChannel closes any existing monitor channel.
|
|
|
|
func (s *Health) CloseMonitorChannel() {
|
2017-11-14 00:31:28 +03:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
2016-04-18 12:48:13 +03:00
|
|
|
if s.stop != nil {
|
2016-06-11 23:16:55 +03:00
|
|
|
logrus.Debug("CloseMonitorChannel: waiting for probe to stop")
|
2016-11-16 05:02:26 +03:00
|
|
|
close(s.stop)
|
2016-04-18 12:48:13 +03:00
|
|
|
s.stop = nil
|
2017-04-07 00:42:10 +03:00
|
|
|
// unhealthy when the monitor has stopped for compatibility reasons
|
2017-11-16 06:28:36 +03:00
|
|
|
s.Health.Status = types.Unhealthy
|
2016-06-11 23:16:55 +03:00
|
|
|
logrus.Debug("CloseMonitorChannel done")
|
2016-04-18 12:48:13 +03:00
|
|
|
}
|
|
|
|
}
|