diff --git a/core/provider/digitalocean/task.go b/core/provider/digitalocean/task.go index 42b8777..e0532fa 100644 --- a/core/provider/digitalocean/task.go +++ b/core/provider/digitalocean/task.go @@ -399,7 +399,20 @@ func (p *Provider) RunCommandWhileStopped(ctx context.Context, taskName string, return "", "", 0, err } +<<<<<<< HEAD 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)) + } + }() +>>>>>>> a5fc9f5 (fix(provider): improve container removal logic) err = dockerClient.ContainerStart(ctx, createdContainer.ID, types.ContainerStartOptions{}) diff --git a/core/provider/docker/volume.go b/core/provider/docker/volume.go index dc6f3af..e966c3a 100644 --- a/core/provider/docker/volume.go +++ b/core/provider/docker/volume.go @@ -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" ) @@ -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)) } }() @@ -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 } }() @@ -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 { @@ -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, @@ -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 {