2013-01-18 19:13:39 -05:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
2013-03-12 08:36:37 -04:00
|
|
|
"fmt"
|
2013-05-14 18:37:35 -04:00
|
|
|
"github.com/dotcloud/docker/utils"
|
2013-04-09 12:09:54 -04:00
|
|
|
"sync"
|
2013-01-28 17:30:05 -05:00
|
|
|
"time"
|
2013-01-18 19:13:39 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type State struct {
|
2013-11-21 15:21:03 -05:00
|
|
|
sync.RWMutex
|
2013-10-18 17:15:24 -04:00
|
|
|
Running bool
|
|
|
|
Pid int
|
|
|
|
ExitCode int
|
|
|
|
StartedAt time.Time
|
2013-10-10 14:46:26 -04:00
|
|
|
FinishedAt time.Time
|
2013-10-18 17:15:24 -04:00
|
|
|
Ghost bool
|
2013-01-18 19:13:39 -05:00
|
|
|
}
|
|
|
|
|
2013-01-27 18:42:42 -05:00
|
|
|
// String returns a human-readable description of the state
|
|
|
|
func (s *State) String() string {
|
2013-11-21 15:21:03 -05:00
|
|
|
s.RLock()
|
|
|
|
defer s.RUnlock()
|
|
|
|
|
2013-01-27 18:42:42 -05:00
|
|
|
if s.Running {
|
2013-04-11 12:26:17 -04:00
|
|
|
if s.Ghost {
|
2013-04-12 13:39:08 -04:00
|
|
|
return fmt.Sprintf("Ghost")
|
2013-04-11 12:26:17 -04:00
|
|
|
}
|
2013-11-21 19:41:41 -05:00
|
|
|
return fmt.Sprintf("Up %s", utils.HumanDuration(time.Now().UTC().Sub(s.StartedAt)))
|
2013-01-27 18:42:42 -05:00
|
|
|
}
|
2013-01-29 06:18:07 -05:00
|
|
|
return fmt.Sprintf("Exit %d", s.ExitCode)
|
2013-01-27 18:42:42 -05:00
|
|
|
}
|
|
|
|
|
2013-11-21 15:21:03 -05:00
|
|
|
func (s *State) IsRunning() bool {
|
|
|
|
s.RLock()
|
|
|
|
defer s.RUnlock()
|
|
|
|
|
|
|
|
return s.Running
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *State) IsGhost() bool {
|
|
|
|
s.RLock()
|
|
|
|
defer s.RUnlock()
|
|
|
|
|
|
|
|
return s.Ghost
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *State) GetExitCode() int {
|
|
|
|
s.RLock()
|
|
|
|
defer s.RUnlock()
|
|
|
|
|
|
|
|
return s.ExitCode
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *State) SetGhost(val bool) {
|
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
|
|
|
|
s.Ghost = val
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *State) SetRunning(pid int) {
|
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
|
2013-01-18 19:13:39 -05:00
|
|
|
s.Running = true
|
2013-07-09 13:48:33 -04:00
|
|
|
s.Ghost = false
|
2013-01-18 19:13:39 -05:00
|
|
|
s.ExitCode = 0
|
|
|
|
s.Pid = pid
|
2013-11-21 19:41:41 -05:00
|
|
|
s.StartedAt = time.Now().UTC()
|
2013-01-18 19:13:39 -05:00
|
|
|
}
|
|
|
|
|
2013-11-21 15:21:03 -05:00
|
|
|
func (s *State) SetStopped(exitCode int) {
|
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
|
2013-01-18 19:13:39 -05:00
|
|
|
s.Running = false
|
|
|
|
s.Pid = 0
|
2013-11-21 19:41:41 -05:00
|
|
|
s.FinishedAt = time.Now().UTC()
|
2013-01-18 19:13:39 -05:00
|
|
|
s.ExitCode = exitCode
|
|
|
|
}
|