1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Use WaitWithContext for WaitStop

Signed-off-by: Brian Goff <cpuguy83@gmail.com>
This commit is contained in:
Brian Goff 2016-11-16 22:08:37 -05:00
parent a27e51ea98
commit 36d6d76a41

View file

@ -177,26 +177,24 @@ func wait(waitChan <-chan struct{}, timeout time.Duration) error {
// immediately. If you want wait forever you must supply negative timeout. // immediately. If you want wait forever you must supply negative timeout.
// Returns exit code, that was passed to SetStopped // Returns exit code, that was passed to SetStopped
func (s *State) WaitStop(timeout time.Duration) (int, error) { func (s *State) WaitStop(timeout time.Duration) (int, error) {
s.Lock() ctx := context.Background()
if !s.Running { if timeout >= 0 {
exitCode := s.ExitCodeValue var cancel func()
s.Unlock() ctx, cancel = context.WithTimeout(ctx, timeout)
return exitCode, nil defer cancel()
} }
waitChan := s.waitChan if err := s.WaitWithContext(ctx); err != nil {
s.Unlock() if status, ok := err.(*StateStatus); ok {
if err := wait(waitChan, timeout); err != nil { return status.ExitCode(), nil
}
return -1, err return -1, err
} }
s.Lock() return 0, nil
defer s.Unlock()
return s.ExitCode(), nil
} }
// WaitWithContext waits for the container to stop. Optional context can be // WaitWithContext waits for the container to stop. Optional context can be
// passed for canceling the request. // passed for canceling the request.
func (s *State) WaitWithContext(ctx context.Context) error { func (s *State) WaitWithContext(ctx context.Context) error {
// todo(tonistiigi): make other wait functions use this
s.Lock() s.Lock()
if !s.Running { if !s.Running {
state := newStateStatus(s.ExitCode(), s.Error()) state := newStateStatus(s.ExitCode(), s.Error())