Skip to content

Commit

Permalink
Remove container if workflow is cancelled (#634)
Browse files Browse the repository at this point in the history
  • Loading branch information
x4204 authored Jul 28, 2024
1 parent a20bd1d commit 0ad4f5b
Showing 1 changed file with 22 additions and 9 deletions.
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

0 comments on commit 0ad4f5b

Please sign in to comment.