state.go
a27b4b8c
 package docker
 
 import (
45a8bba1
 	"fmt"
2e69e172
 	"github.com/dotcloud/docker/utils"
7c2b085d
 	"sync"
333abbf8
 	"time"
a27b4b8c
 )
 
 type State struct {
33e70864
 	sync.RWMutex
89fb51f6
 	Running    bool
 	Pid        int
 	ExitCode   int
 	StartedAt  time.Time
2eb404ab
 	FinishedAt time.Time
89fb51f6
 	Ghost      bool
a27b4b8c
 }
 
904b0ab5
 // String returns a human-readable description of the state
 func (s *State) String() string {
33e70864
 	s.RLock()
 	defer s.RUnlock()
 
904b0ab5
 	if s.Running {
313d13ea
 		if s.Ghost {
b1fbebb4
 			return fmt.Sprintf("Ghost")
313d13ea
 		}
806abe90
 		return fmt.Sprintf("Up %s", utils.HumanDuration(time.Now().UTC().Sub(s.StartedAt)))
904b0ab5
 	}
bcfe2aa2
 	return fmt.Sprintf("Exit %d", s.ExitCode)
904b0ab5
 }
 
33e70864
 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()
 
a27b4b8c
 	s.Running = true
91520838
 	s.Ghost = false
a27b4b8c
 	s.ExitCode = 0
 	s.Pid = pid
806abe90
 	s.StartedAt = time.Now().UTC()
a27b4b8c
 }
 
33e70864
 func (s *State) SetStopped(exitCode int) {
 	s.Lock()
 	defer s.Unlock()
 
a27b4b8c
 	s.Running = false
 	s.Pid = 0
806abe90
 	s.FinishedAt = time.Now().UTC()
a27b4b8c
 	s.ExitCode = exitCode
 }