state.go 706 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package docker
  2. import (
  3. "fmt"
  4. "github.com/dotcloud/docker/utils"
  5. "sync"
  6. "time"
  7. )
  8. type State struct {
  9. sync.Mutex
  10. Running bool
  11. Pid int
  12. ExitCode int
  13. StartedAt time.Time
  14. Ghost bool
  15. }
  16. // String returns a human-readable description of the state
  17. func (s *State) String() string {
  18. if s.Running {
  19. if s.Ghost {
  20. return fmt.Sprintf("Ghost")
  21. }
  22. return fmt.Sprintf("Up %s", utils.HumanDuration(time.Now().Sub(s.StartedAt)))
  23. }
  24. return fmt.Sprintf("Exit %d", s.ExitCode)
  25. }
  26. func (s *State) setRunning(pid int) {
  27. s.Running = true
  28. s.ExitCode = 0
  29. s.Pid = pid
  30. s.StartedAt = time.Now()
  31. }
  32. func (s *State) setStopped(exitCode int) {
  33. s.Running = false
  34. s.Pid = 0
  35. s.ExitCode = exitCode
  36. }