1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/daemon/state.go

91 lines
1.5 KiB
Go
Raw Normal View History

package daemon
2013-01-18 19:13:39 -05:00
import (
2013-03-12 08:36:37 -04:00
"fmt"
"sync"
2013-01-28 17:30:05 -05:00
"time"
"github.com/dotcloud/docker/pkg/units"
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
Paused bool
2013-10-18 17:15:24 -04:00
Pid int
ExitCode int
StartedAt time.Time
FinishedAt time.Time
2013-01-18 19:13:39 -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()
if s.Running {
if s.Paused {
return fmt.Sprintf("Up %s (Paused)", units.HumanDuration(time.Now().UTC().Sub(s.StartedAt)))
}
return fmt.Sprintf("Up %s", units.HumanDuration(time.Now().UTC().Sub(s.StartedAt)))
}
if s.FinishedAt.IsZero() {
return ""
}
return fmt.Sprintf("Exited (%d) %s ago", s.ExitCode, units.HumanDuration(time.Now().UTC().Sub(s.FinishedAt)))
}
2013-11-21 15:21:03 -05: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-18 19:13:39 -05:00
s.Running = true
s.Paused = 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
}
func (s *State) SetPaused() {
s.Lock()
defer s.Unlock()
s.Paused = true
}
func (s *State) SetUnpaused() {
s.Lock()
defer s.Unlock()
s.Paused = false
}
func (s *State) IsPaused() bool {
s.RLock()
defer s.RUnlock()
return s.Paused
}