state.go 772 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. FinishedAt time.Time
  15. Ghost bool
  16. }
  17. // String returns a human-readable description of the state
  18. func (s *State) String() string {
  19. if s.Running {
  20. if s.Ghost {
  21. return fmt.Sprintf("Ghost")
  22. }
  23. return fmt.Sprintf("Up %s", utils.HumanDuration(time.Now().Sub(s.StartedAt)))
  24. }
  25. return fmt.Sprintf("Exit %d", s.ExitCode)
  26. }
  27. func (s *State) setRunning(pid int) {
  28. s.Running = true
  29. s.Ghost = false
  30. s.ExitCode = 0
  31. s.Pid = pid
  32. s.StartedAt = time.Now()
  33. }
  34. func (s *State) setStopped(exitCode int) {
  35. s.Running = false
  36. s.Pid = 0
  37. s.FinishedAt = time.Now()
  38. s.ExitCode = exitCode
  39. }