container/health.go
b6c7becb
 package container
 
 import (
5b55747a
 	"sync"
 
91e197d6
 	"github.com/docker/docker/api/types"
1009e6a4
 	"github.com/sirupsen/logrus"
b6c7becb
 )
 
 // Health holds the current container health-check state
 type Health struct {
 	types.Health
 	stop chan struct{} // Write struct{} to stop the monitor
5b55747a
 	mu   sync.Mutex
b6c7becb
 }
 
 // String returns a human-readable description of the health-check state
 func (s *Health) String() string {
7db30ab0
 	status := s.Status()
4016038b
 
7db30ab0
 	switch status {
b6c7becb
 	case types.Starting:
 		return "health: starting"
 	default: // Healthy and Unhealthy are clear on their own
7db30ab0
 		return s.Health.Status
b6c7becb
 	}
 }
 
7db30ab0
 // 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
 }
 
5b55747a
 // OpenMonitorChannel creates and returns a new monitor channel. If there
 // already is one, it returns nil.
b6c7becb
 func (s *Health) OpenMonitorChannel() chan struct{} {
5b55747a
 	s.mu.Lock()
 	defer s.mu.Unlock()
 
b6c7becb
 	if s.stop == nil {
a72b45db
 		logrus.Debug("OpenMonitorChannel")
b6c7becb
 		s.stop = make(chan struct{})
 		return s.stop
 	}
 	return nil
 }
 
 // CloseMonitorChannel closes any existing monitor channel.
 func (s *Health) CloseMonitorChannel() {
5b55747a
 	s.mu.Lock()
 	defer s.mu.Unlock()
 
b6c7becb
 	if s.stop != nil {
a72b45db
 		logrus.Debug("CloseMonitorChannel: waiting for probe to stop")
89b12347
 		close(s.stop)
b6c7becb
 		s.stop = nil
04bd768a
 		// unhealthy when the monitor has stopped for compatibility reasons
7db30ab0
 		s.Health.Status = types.Unhealthy
a72b45db
 		logrus.Debug("CloseMonitorChannel done")
b6c7becb
 	}
 }