Skip to content

Commit

Permalink
Stop a task when a step failed.
Browse files Browse the repository at this point in the history
  • Loading branch information
gnutix committed Nov 16, 2023
1 parent 126119a commit 75de342
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions provisioner/internal/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ func RunContainer(
networkId := netResp.ID
defer cleanup(
"network",
func() error { return docker.NetworkRemove(context.Background(), networkId) },
func() error {
return docker.NetworkRemove(context.Background(), networkId)
},
)

// Initialize workspace
Expand All @@ -58,7 +60,9 @@ func RunContainer(
}
defer cleanup(
"workspace",
func() error { return taskFs.Delete("/") },
func() error {
return taskFs.Delete("/")
},
)

for _, dir := range []string{"output", "shared"} {
Expand Down Expand Up @@ -130,8 +134,7 @@ func RunContainer(
defer cleanup(
"service container",
func() error {
return docker.ContainerRemove(context.Background(), resp.ID,
types.ContainerRemoveOptions{RemoveVolumes: true, Force: true})
return docker.ContainerRemove(context.Background(), resp.ID, types.ContainerRemoveOptions{RemoveVolumes: true, Force: true})
},
"service", service.Name,
)
Expand Down Expand Up @@ -169,6 +172,7 @@ func RunContainer(
// Always wait 1 second before running the health check, and potentially more between retries
time.Sleep(lo.Ternary(i > 0, interval, 1*time.Second))

healthCheckLog := serviceLog.With(slog.Group("retry", "attempt", i+1, "interval", interval))
healthCheckCmd := append([]string{service.Health.Cmd}, service.Health.Args...)

exec, err := docker.ContainerExecCreate(ctx, containerId, types.ExecConfig{
Expand All @@ -183,7 +187,7 @@ func RunContainer(

execCtx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
serviceLog.Debug("Running health check", "cmd", healthCheckCmd, "attempt", i+1, "interval", interval)
healthCheckLog.Debug("Running health check", "cmd", healthCheckCmd)
attach, err := docker.ContainerExecAttach(execCtx, exec.ID, types.ExecStartCheck{})
if err != nil {
serviceErrors <- fmt.Errorf("failed to attach docker exec for service '%s': %w", service.Name, err)
Expand All @@ -209,13 +213,13 @@ func RunContainer(
return
}
if inspect.ExitCode == 0 {
serviceLog.Debug("Service is ready")
healthCheckLog.Debug("Service is ready")
return
}

serviceLog.Debug("Service health check unsuccessful, retrying...", "exitcode", inspect.ExitCode)
healthCheckLog.Debug("Service health check unsuccessful, retrying...", "exitcode", inspect.ExitCode)
} else {
serviceLog.Debug("Service health check timed out, retrying...")
healthCheckLog.Debug("Service health check timed out, retrying...")
}
}

Expand Down Expand Up @@ -293,8 +297,7 @@ func RunContainer(
defer cleanup(
"step container",
func() error {
return docker.ContainerRemove(context.Background(), resp.ID,
types.ContainerRemoveOptions{RemoveVolumes: true, Force: true})
return docker.ContainerRemove(context.Background(), resp.ID, types.ContainerRemoveOptions{RemoveVolumes: true, Force: true})
},
)

Expand All @@ -318,7 +321,7 @@ func RunContainer(
case status = <-wait:
// Container is done
if status.StatusCode != 0 {
break
return fmt.Errorf("step %d failed with status: %d", stepIndex, status.StatusCode)
}
case err := <-errChan:
return fmt.Errorf("failed while waiting for docker container for step %d: %w", stepIndex, err)
Expand Down

0 comments on commit 75de342

Please sign in to comment.