Skip to content

Commit

Permalink
fix(provider): improve container removal logic
Browse files Browse the repository at this point in the history
(cherry picked from commit a5fc9f5)

# Conflicts:
#	core/provider/digitalocean/task.go
  • Loading branch information
Zygimantass authored and mergify[bot] committed Nov 7, 2024
1 parent 8394032 commit 359c913
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
13 changes: 13 additions & 0 deletions core/provider/digitalocean/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,20 @@ func (p *Provider) RunCommandWhileStopped(ctx context.Context, taskName string,
return "", "", 0, err
}

<<<<<<< HEAD

Check failure on line 402 in core/provider/digitalocean/task.go

View workflow job for this annotation

GitHub Actions / golangci-lint (core)

expected statement, found '<<' (typecheck)
defer dockerClient.ContainerRemove(ctx, createdContainer.ID, types.ContainerRemoveOptions{Force: true})
=======

Check failure on line 404 in core/provider/digitalocean/task.go

View workflow job for this annotation

GitHub Actions / golangci-lint (core)

expected statement, found '==' (typecheck)
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))
}
}()
>>>>>>> a5fc9f5 (fix(provider): improve container removal logic)

Check failure on line 415 in core/provider/digitalocean/task.go

View workflow job for this annotation

GitHub Actions / golangci-lint (core)

expected statement, found '>>' (typecheck)

err = dockerClient.ContainerStart(ctx, createdContainer.ID, types.ContainerStartOptions{})

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 @@ -16,6 +16,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 @@ -243,11 +248,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 @@ -330,7 +339,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 @@ -410,8 +422,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 @@ -427,7 +439,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 359c913

Please sign in to comment.