state.go
a27b4b8c
 package docker
 
 import (
45a8bba1
 	"fmt"
2e69e172
 	"github.com/dotcloud/docker/utils"
7c2b085d
 	"sync"
333abbf8
 	"time"
a27b4b8c
 )
 
 type State struct {
73d72654
 	Running   bool
 	Pid       int
 	ExitCode  int
64fc86fb
 	StartedAt time.Time
7c2b085d
 	l         *sync.Mutex
313d13ea
 	Ghost     bool
a27b4b8c
 }
 
904b0ab5
 // String returns a human-readable description of the state
 func (s *State) String() string {
 	if s.Running {
313d13ea
 		if s.Ghost {
b1fbebb4
 			return fmt.Sprintf("Ghost")
313d13ea
 		}
2e69e172
 		return fmt.Sprintf("Up %s", utils.HumanDuration(time.Now().Sub(s.StartedAt)))
904b0ab5
 	}
bcfe2aa2
 	return fmt.Sprintf("Exit %d", s.ExitCode)
904b0ab5
 }
 
a27b4b8c
 func (s *State) setRunning(pid int) {
 	s.Running = true
 	s.ExitCode = 0
 	s.Pid = pid
64fc86fb
 	s.StartedAt = time.Now()
a27b4b8c
 }
 
 func (s *State) setStopped(exitCode int) {
 	s.Running = false
 	s.Pid = 0
 	s.ExitCode = exitCode
 }
7c2b085d
 
 func (s *State) initLock() {
 	s.l = &sync.Mutex{}
 }
 
 func (s *State) lock() {
 	s.l.Lock()
 }
 
 func (s *State) unlock() {
 	s.l.Unlock()
 }