state.go 812 B

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