2013-01-19 04:13:39 +04:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
2013-01-28 03:42:42 +04:00
|
|
|
"fmt"
|
|
|
|
"github.com/dotcloud/docker/future"
|
2013-01-29 02:30:05 +04:00
|
|
|
"sync"
|
|
|
|
"time"
|
2013-01-19 04:13:39 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
type State struct {
|
2013-01-23 05:30:37 +04:00
|
|
|
Running bool
|
|
|
|
Pid int
|
|
|
|
ExitCode int
|
2013-01-23 03:03:27 +04:00
|
|
|
StartedAt time.Time
|
2013-01-19 04:13:39 +04:00
|
|
|
|
|
|
|
stateChangeLock *sync.Mutex
|
|
|
|
stateChangeCond *sync.Cond
|
|
|
|
}
|
|
|
|
|
|
|
|
func newState() *State {
|
|
|
|
lock := new(sync.Mutex)
|
|
|
|
return &State{
|
|
|
|
stateChangeLock: lock,
|
|
|
|
stateChangeCond: sync.NewCond(lock),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-28 03:42:42 +04:00
|
|
|
// String returns a human-readable description of the state
|
|
|
|
func (s *State) String() string {
|
|
|
|
if s.Running {
|
|
|
|
return fmt.Sprintf("Running for %s", future.HumanDuration(time.Now().Sub(s.StartedAt)))
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("Exited with %d", s.ExitCode)
|
|
|
|
}
|
|
|
|
|
2013-01-19 04:13:39 +04:00
|
|
|
func (s *State) setRunning(pid int) {
|
|
|
|
s.Running = true
|
|
|
|
s.ExitCode = 0
|
|
|
|
s.Pid = pid
|
2013-01-23 03:03:27 +04:00
|
|
|
s.StartedAt = time.Now()
|
2013-01-19 04:13:39 +04:00
|
|
|
s.broadcast()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *State) setStopped(exitCode int) {
|
|
|
|
s.Running = false
|
|
|
|
s.Pid = 0
|
|
|
|
s.ExitCode = exitCode
|
|
|
|
s.broadcast()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *State) broadcast() {
|
|
|
|
s.stateChangeLock.Lock()
|
|
|
|
s.stateChangeCond.Broadcast()
|
|
|
|
s.stateChangeLock.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *State) wait() {
|
|
|
|
s.stateChangeLock.Lock()
|
|
|
|
s.stateChangeCond.Wait()
|
|
|
|
s.stateChangeLock.Unlock()
|
|
|
|
}
|