Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove container if workflow is cancelled #634

Merged
merged 1 commit into from
Jul 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions internal/dag/executor/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ func (e *docker) Kill(_ os.Signal) error {
}

func (e *docker) Run() error {
ctx, fn := context.WithCancel(context.Background())
ctx, cancelFunc := context.WithCancel(context.Background())
e.context = ctx
e.cancel = fn
e.cancel = cancelFunc

cli, err := client.NewClientWithOpts(
client.FromEnv, client.WithAPIVersionNegotiation(),
Expand Down Expand Up @@ -82,19 +82,32 @@ func (e *docker) Run() error {
return err
}

removing := false
removeContainer := func() {
if !e.autoRemove || removing {
return
}
removing = true
err := cli.ContainerRemove(
ctx, resp.ID, types.ContainerRemoveOptions{
Force: true,
},
)
util.LogErr("docker executor: remove container", err)
}

defer removeContainer()
e.cancel = func() {
removeContainer()
cancelFunc()
}

if err := cli.ContainerStart(
ctx, resp.ID, types.ContainerStartOptions{},
); err != nil {
return err
}

defer func() {
if e.autoRemove {
err := cli.ContainerRemove(ctx, resp.ID, types.ContainerRemoveOptions{})
util.LogErr("docker executor: remove container", err)
}
}()

out, err := cli.ContainerLogs(
ctx, resp.ID, types.ContainerLogsOptions{
ShowStdout: true,
Expand Down
Loading