Skip to content

Commit

Permalink
Merge pull request #134 from skip-mev/zygis/cleanup-logic
Browse files Browse the repository at this point in the history
fix(provider): improve container removal logic
  • Loading branch information
Zygimantass authored Nov 7, 2024
2 parents 922fe25 + a5fc9f5 commit df94de2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
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

0 comments on commit df94de2

Please sign in to comment.