Skip to content

Commit

Permalink
internal/docker: wait for exec to finish before reading exit code
Browse files Browse the repository at this point in the history
  • Loading branch information
cezarsa committed Sep 14, 2018
1 parent 4cd5fa9 commit a9e9bb1
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions internal/docker/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@
package docker

import (
"errors"
"fmt"
"time"

"github.com/fsouza/go-dockerclient"
"github.com/tsuru/tsuru/exec"
)

const (
timeoutExecWait = time.Minute
)

// Executor uses docker exec to execute a command in a running docker container
type Executor struct {
ContainerID string
Expand Down Expand Up @@ -56,12 +62,30 @@ func (d *Executor) ExecuteAsUser(user string, opts exec.ExecuteOptions) error {
if err != nil {
return err
}
execData, err := d.Client.api.InspectExec(e.ID)
exitCode, err := waitExecStatus(d.Client.api, e.ID)
if err != nil {
return err
}
if execData.ExitCode != 0 {
return fmt.Errorf("unexpected exit code %#+v while running %v", execData.ExitCode, cmd)
if exitCode != 0 {
return fmt.Errorf("unexpected exit code %#+v while running %v", exitCode, cmd)
}
return nil
}

func waitExecStatus(client *docker.Client, execID string) (int, error) {
timeout := time.After(timeoutExecWait)
for {
execData, err := client.InspectExec(execID)
if err != nil {
return 0, err
}
if !execData.Running {
return execData.ExitCode, nil
}
select {
case <-timeout:
return 0, errors.New("timeout waiting for exec to finish")
case <-time.After(200 * time.Millisecond):
}
}
}

0 comments on commit a9e9bb1

Please sign in to comment.