2014-04-17 21:43:01 +00:00
|
|
|
package daemon
|
2013-01-19 00:13:39 +00:00
|
|
|
|
|
|
|
import (
|
2013-03-12 12:36:37 +00:00
|
|
|
"fmt"
|
2013-04-09 16:09:54 +00:00
|
|
|
"sync"
|
2013-01-28 22:30:05 +00:00
|
|
|
"time"
|
2014-05-12 23:40:19 +00:00
|
|
|
|
|
|
|
"github.com/dotcloud/docker/pkg/units"
|
2013-01-19 00:13:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type State struct {
|
2013-11-21 20:21:03 +00:00
|
|
|
sync.RWMutex
|
2013-10-18 21:15:24 +00:00
|
|
|
Running bool
|
|
|
|
Pid int
|
|
|
|
ExitCode int
|
|
|
|
StartedAt time.Time
|
2013-10-10 18:46:26 +00:00
|
|
|
FinishedAt time.Time
|
2013-01-19 00:13:39 +00:00
|
|
|
}
|
|
|
|
|
2013-01-27 23:42:42 +00:00
|
|
|
// String returns a human-readable description of the state
|
|
|
|
func (s *State) String() string {
|
2013-11-21 20:21:03 +00:00
|
|
|
s.RLock()
|
|
|
|
defer s.RUnlock()
|
|
|
|
|
2013-01-27 23:42:42 +00:00
|
|
|
if s.Running {
|
2014-05-12 23:40:19 +00:00
|
|
|
return fmt.Sprintf("Up %s", units.HumanDuration(time.Now().UTC().Sub(s.StartedAt)))
|
2013-01-27 23:42:42 +00:00
|
|
|
}
|
2014-03-19 03:05:54 +00:00
|
|
|
if s.FinishedAt.IsZero() {
|
|
|
|
return ""
|
|
|
|
}
|
2014-05-12 23:40:19 +00:00
|
|
|
return fmt.Sprintf("Exited (%d) %s ago", s.ExitCode, units.HumanDuration(time.Now().UTC().Sub(s.FinishedAt)))
|
2013-01-27 23:42:42 +00:00
|
|
|
}
|
|
|
|
|
2013-11-21 20:21:03 +00:00
|
|
|
func (s *State) IsRunning() bool {
|
|
|
|
s.RLock()
|
|
|
|
defer s.RUnlock()
|
|
|
|
|
|
|
|
return s.Running
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *State) GetExitCode() int {
|
|
|
|
s.RLock()
|
|
|
|
defer s.RUnlock()
|
|
|
|
|
|
|
|
return s.ExitCode
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *State) SetRunning(pid int) {
|
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
|
2013-01-19 00:13:39 +00:00
|
|
|
s.Running = true
|
|
|
|
s.ExitCode = 0
|
|
|
|
s.Pid = pid
|
2013-11-22 00:41:41 +00:00
|
|
|
s.StartedAt = time.Now().UTC()
|
2013-01-19 00:13:39 +00:00
|
|
|
}
|
|
|
|
|
2013-11-21 20:21:03 +00:00
|
|
|
func (s *State) SetStopped(exitCode int) {
|
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
|
2013-01-19 00:13:39 +00:00
|
|
|
s.Running = false
|
|
|
|
s.Pid = 0
|
2013-11-22 00:41:41 +00:00
|
|
|
s.FinishedAt = time.Now().UTC()
|
2013-01-19 00:13:39 +00:00
|
|
|
s.ExitCode = exitCode
|
|
|
|
}
|