2014-04-18 01:43:01 +04:00
|
|
|
package daemon
|
2013-01-19 04:13:39 +04:00
|
|
|
|
|
|
|
import (
|
2013-03-12 16:36:37 +04:00
|
|
|
"fmt"
|
2013-04-09 20:09:54 +04:00
|
|
|
"sync"
|
2013-01-29 02:30:05 +04:00
|
|
|
"time"
|
2014-05-13 03:40:19 +04:00
|
|
|
|
2014-07-25 02:19:50 +04:00
|
|
|
"github.com/docker/docker/pkg/units"
|
2013-01-19 04:13:39 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
type State struct {
|
2013-11-22 00:21:03 +04:00
|
|
|
sync.RWMutex
|
2013-10-19 01:15:24 +04:00
|
|
|
Running bool
|
2014-05-22 01:06:18 +04:00
|
|
|
Paused bool
|
2014-08-11 22:07:37 +04:00
|
|
|
Restarting bool
|
2013-10-19 01:15:24 +04:00
|
|
|
Pid int
|
|
|
|
ExitCode int
|
|
|
|
StartedAt time.Time
|
2013-10-10 22:46:26 +04:00
|
|
|
FinishedAt time.Time
|
2014-06-06 15:28:12 +04:00
|
|
|
waitChan chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewState() *State {
|
|
|
|
return &State{
|
|
|
|
waitChan: make(chan struct{}),
|
|
|
|
}
|
2013-01-19 04:13:39 +04:00
|
|
|
}
|
|
|
|
|
2013-01-28 03:42:42 +04:00
|
|
|
// String returns a human-readable description of the state
|
|
|
|
func (s *State) String() string {
|
2013-11-22 00:21:03 +04:00
|
|
|
s.RLock()
|
|
|
|
defer s.RUnlock()
|
|
|
|
|
2013-01-28 03:42:42 +04:00
|
|
|
if s.Running {
|
2014-05-22 01:06:18 +04:00
|
|
|
if s.Paused {
|
|
|
|
return fmt.Sprintf("Up %s (Paused)", units.HumanDuration(time.Now().UTC().Sub(s.StartedAt)))
|
|
|
|
}
|
2014-08-11 22:35:18 +04:00
|
|
|
if s.Restarting {
|
|
|
|
return fmt.Sprintf("Restarting (%d) %s ago", s.ExitCode, units.HumanDuration(time.Now().UTC().Sub(s.FinishedAt)))
|
|
|
|
}
|
2014-08-11 22:07:37 +04:00
|
|
|
|
2014-05-13 03:40:19 +04:00
|
|
|
return fmt.Sprintf("Up %s", units.HumanDuration(time.Now().UTC().Sub(s.StartedAt)))
|
2013-01-28 03:42:42 +04:00
|
|
|
}
|
2014-08-11 22:07:37 +04:00
|
|
|
|
2014-03-19 07:05:54 +04:00
|
|
|
if s.FinishedAt.IsZero() {
|
|
|
|
return ""
|
|
|
|
}
|
2014-08-11 22:07:37 +04:00
|
|
|
|
2014-05-13 03:40:19 +04:00
|
|
|
return fmt.Sprintf("Exited (%d) %s ago", s.ExitCode, units.HumanDuration(time.Now().UTC().Sub(s.FinishedAt)))
|
2013-01-28 03:42:42 +04:00
|
|
|
}
|
|
|
|
|
2014-06-06 15:28:12 +04:00
|
|
|
func wait(waitChan <-chan struct{}, timeout time.Duration) error {
|
|
|
|
if timeout < 0 {
|
|
|
|
<-waitChan
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-time.After(timeout):
|
|
|
|
return fmt.Errorf("Timed out: %v", timeout)
|
|
|
|
case <-waitChan:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WaitRunning waits until state is running. If state already running it returns
|
|
|
|
// immediatly. If you want wait forever you must supply negative timeout.
|
|
|
|
// Returns pid, that was passed to SetRunning
|
|
|
|
func (s *State) WaitRunning(timeout time.Duration) (int, error) {
|
|
|
|
s.RLock()
|
|
|
|
if s.IsRunning() {
|
|
|
|
pid := s.Pid
|
|
|
|
s.RUnlock()
|
|
|
|
return pid, nil
|
|
|
|
}
|
|
|
|
waitChan := s.waitChan
|
|
|
|
s.RUnlock()
|
|
|
|
if err := wait(waitChan, timeout); err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
return s.GetPid(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// WaitStop waits until state is stopped. If state already stopped it returns
|
|
|
|
// immediatly. If you want wait forever you must supply negative timeout.
|
2014-06-06 15:30:04 +04:00
|
|
|
// Returns exit code, that was passed to SetStopped
|
2014-06-06 15:28:12 +04:00
|
|
|
func (s *State) WaitStop(timeout time.Duration) (int, error) {
|
|
|
|
s.RLock()
|
|
|
|
if !s.Running {
|
|
|
|
exitCode := s.ExitCode
|
|
|
|
s.RUnlock()
|
|
|
|
return exitCode, nil
|
|
|
|
}
|
|
|
|
waitChan := s.waitChan
|
|
|
|
s.RUnlock()
|
|
|
|
if err := wait(waitChan, timeout); err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
return s.GetExitCode(), nil
|
|
|
|
}
|
|
|
|
|
2013-11-22 00:21:03 +04:00
|
|
|
func (s *State) IsRunning() bool {
|
|
|
|
s.RLock()
|
2014-06-06 15:28:12 +04:00
|
|
|
res := s.Running
|
|
|
|
s.RUnlock()
|
|
|
|
return res
|
|
|
|
}
|
2013-11-22 00:21:03 +04:00
|
|
|
|
2014-06-06 15:28:12 +04:00
|
|
|
func (s *State) GetPid() int {
|
|
|
|
s.RLock()
|
|
|
|
res := s.Pid
|
|
|
|
s.RUnlock()
|
|
|
|
return res
|
2013-11-22 00:21:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *State) GetExitCode() int {
|
|
|
|
s.RLock()
|
2014-06-06 15:28:12 +04:00
|
|
|
res := s.ExitCode
|
|
|
|
s.RUnlock()
|
|
|
|
return res
|
2013-11-22 00:21:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *State) SetRunning(pid int) {
|
|
|
|
s.Lock()
|
2014-07-29 11:14:10 +04:00
|
|
|
s.Running = true
|
|
|
|
s.Paused = false
|
|
|
|
s.ExitCode = 0
|
|
|
|
s.Pid = pid
|
|
|
|
s.StartedAt = time.Now().UTC()
|
|
|
|
close(s.waitChan) // fire waiters for start
|
|
|
|
s.waitChan = make(chan struct{})
|
2014-06-06 15:28:12 +04:00
|
|
|
s.Unlock()
|
2013-01-19 04:13:39 +04:00
|
|
|
}
|
|
|
|
|
2013-11-22 00:21:03 +04:00
|
|
|
func (s *State) SetStopped(exitCode int) {
|
|
|
|
s.Lock()
|
2014-07-29 11:14:10 +04:00
|
|
|
s.Running = false
|
|
|
|
s.Pid = 0
|
|
|
|
s.FinishedAt = time.Now().UTC()
|
|
|
|
s.ExitCode = exitCode
|
|
|
|
close(s.waitChan) // fire waiters for stop
|
|
|
|
s.waitChan = make(chan struct{})
|
2014-06-06 15:28:12 +04:00
|
|
|
s.Unlock()
|
2013-01-19 04:13:39 +04:00
|
|
|
}
|
2014-05-22 01:06:18 +04:00
|
|
|
|
2014-08-11 22:07:37 +04:00
|
|
|
// SetRestarting is when docker hanldes the auto restart of containers when they are
|
|
|
|
// in the middle of a stop and being restarted again
|
|
|
|
func (s *State) SetRestarting(exitCode int) {
|
|
|
|
s.Lock()
|
|
|
|
if s.Running {
|
2014-08-11 22:35:18 +04:00
|
|
|
// we should consider the container running when it is restarting because of
|
|
|
|
// all the checks in docker around rm/stop/etc
|
|
|
|
s.Running = true
|
|
|
|
s.Restarting = true
|
2014-08-11 22:07:37 +04:00
|
|
|
s.Pid = 0
|
|
|
|
s.FinishedAt = time.Now().UTC()
|
|
|
|
s.ExitCode = exitCode
|
|
|
|
close(s.waitChan) // fire waiters for stop
|
|
|
|
s.waitChan = make(chan struct{})
|
|
|
|
}
|
|
|
|
s.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *State) IsRestarting() bool {
|
|
|
|
s.RLock()
|
|
|
|
res := s.Restarting
|
|
|
|
s.RUnlock()
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2014-05-22 01:06:18 +04:00
|
|
|
func (s *State) SetPaused() {
|
|
|
|
s.Lock()
|
|
|
|
s.Paused = true
|
2014-06-06 15:28:12 +04:00
|
|
|
s.Unlock()
|
2014-05-22 01:06:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *State) SetUnpaused() {
|
|
|
|
s.Lock()
|
|
|
|
s.Paused = false
|
2014-06-06 15:28:12 +04:00
|
|
|
s.Unlock()
|
2014-05-22 01:06:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *State) IsPaused() bool {
|
|
|
|
s.RLock()
|
2014-06-06 15:28:12 +04:00
|
|
|
res := s.Paused
|
|
|
|
s.RUnlock()
|
|
|
|
return res
|
2014-05-22 01:06:18 +04:00
|
|
|
}
|