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

fix(provider): improve container removal logic #134

Merged
merged 1 commit into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
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
12 changes: 10 additions & 2 deletions core/provider/digitalocean/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,16 @@ func (p *Provider) RunCommandWhileStopped(ctx context.Context, taskName string,
return "", "", 0, err
}

// nolint
defer dockerClient.ContainerRemove(ctx, createdContainer.ID, types.ContainerRemoveOptions{Force: true})
defer func() {
if _, err := dockerClient.ContainerInspect(ctx, createdContainer.ID); err != nil && dockerclient.IsErrNotFound(err) {
// auto-removed, but not detected as autoremoved
return
}

if err := dockerClient.ContainerRemove(ctx, createdContainer.ID, types.ContainerRemoveOptions{Force: true}); err != nil {
p.logger.Error("failed to remove container", zap.Error(err), zap.String("taskName", taskName), zap.String("id", createdContainer.ID))
}
}()

if err := startContainerWithBlock(ctx, dockerClient, createdContainer.ID); err != nil {
p.logger.Error("failed to start container", zap.Error(err), zap.String("taskName", taskName))
Expand Down
30 changes: 23 additions & 7 deletions core/provider/docker/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/volume"
"github.com/docker/docker/client"

"github.com/skip-mev/petri/core/v2/provider"
)
Expand Down Expand Up @@ -124,11 +125,15 @@ func (p *Provider) WriteFile(ctx context.Context, id, relPath string, content []
return
}

// nolint // will fix later
if _, err := p.dockerClient.ContainerInspect(ctx, cc.ID); err != nil && client.IsErrNotFound(err) {
// auto-removed, but not detected as autoremoved
return
}

if err := p.dockerClient.ContainerRemove(ctx, cc.ID, types.ContainerRemoveOptions{
Force: true,
}); err != nil {
// TODO fix logging
logger.Error("failed to remove writefile container", zap.String("id", cc.ID), zap.Error(err))
}
}()

Expand Down Expand Up @@ -242,11 +247,15 @@ func (p *Provider) ReadFile(ctx context.Context, id, relPath string) ([]byte, er
logger.Debug("created getfile container", zap.String("id", cc.ID))

defer func() {
if _, err := p.dockerClient.ContainerInspect(ctx, cc.ID); err != nil && client.IsErrNotFound(err) {
// auto-removed, but not detected as autoremoved
return
}

if err := p.dockerClient.ContainerRemove(ctx, cc.ID, types.ContainerRemoveOptions{
Force: true,
}); err != nil {
logger.Error("failed cleaning up the getfile container", zap.Error(err))
// todo fix logging
}
}()

Expand Down Expand Up @@ -328,7 +337,10 @@ func (p *Provider) DownloadDir(ctx context.Context, id, relPath, localPath strin
}

defer func() {
// nolint // will fix later
if _, err := p.dockerClient.ContainerInspect(ctx, cc.ID); err != nil && client.IsErrNotFound(err) {
return
}

if err := p.dockerClient.ContainerRemove(ctx, cc.ID, types.ContainerRemoveOptions{
Force: true,
}); err != nil {
Expand Down Expand Up @@ -408,8 +420,8 @@ func (p *Provider) SetVolumeOwner(ctx context.Context, volumeName, uid, gid stri
User: "0",
},
&container.HostConfig{
Binds: []string{volumeName + ":" + mountPath},
// AutoRemove: true,
Binds: []string{volumeName + ":" + mountPath},
AutoRemove: true,
},
nil, // No networking necessary.
nil,
Expand All @@ -425,7 +437,11 @@ func (p *Provider) SetVolumeOwner(ctx context.Context, volumeName, uid, gid stri
return
}

// nolint // will fix later
if _, err := p.dockerClient.ContainerInspect(ctx, cc.ID); err != nil && client.IsErrNotFound(err) {
// auto-removed, but not detected as autoremoved
return
}

if err := p.dockerClient.ContainerRemove(ctx, cc.ID, types.ContainerRemoveOptions{
Force: true,
}); err != nil {
Expand Down
Loading