diff --git a/api/server/backend/build/backend.go b/api/server/backend/build/backend.go index b5a758c387..f93fba93b7 100644 --- a/api/server/backend/build/backend.go +++ b/api/server/backend/build/backend.go @@ -70,7 +70,7 @@ func (b *Backend) Build(ctx context.Context, config backend.BuildConfig) (string // PruneCache removes all cached build sources func (b *Backend) PruneCache(ctx context.Context) (*types.BuildCachePruneReport, error) { - size, err := b.fsCache.Prune() + size, err := b.fsCache.Prune(ctx) if err != nil { return nil, errors.Wrap(err, "failed to prune build cache") } diff --git a/builder/fscache/fscache.go b/builder/fscache/fscache.go index 802db96de0..63331091a6 100644 --- a/builder/fscache/fscache.go +++ b/builder/fscache/fscache.go @@ -154,8 +154,8 @@ func (fsc *FSCache) DiskUsage() (int64, error) { } // Prune allows manually cleaning up the cache -func (fsc *FSCache) Prune() (uint64, error) { - return fsc.store.Prune() +func (fsc *FSCache) Prune(ctx context.Context) (uint64, error) { + return fsc.store.Prune(ctx) } // Close stops the gc and closes the persistent db @@ -396,12 +396,19 @@ func (s *fsCacheStore) DiskUsage() (int64, error) { } // Prune allows manually cleaning up the cache -func (s *fsCacheStore) Prune() (uint64, error) { +func (s *fsCacheStore) Prune(ctx context.Context) (uint64, error) { s.mu.Lock() defer s.mu.Unlock() var size uint64 for id, snap := range s.sources { + select { + case <-ctx.Done(): + logrus.Debugf("Cache prune operation cancelled, pruned size: %d", size) + // when the context is cancelled, only return current size and nil + return size, nil + default: + } if len(snap.refs) == 0 { ss, err := snap.getSize() if err != nil { diff --git a/builder/fscache/fscache_test.go b/builder/fscache/fscache_test.go index c7c0531f27..2532a218c8 100644 --- a/builder/fscache/fscache_test.go +++ b/builder/fscache/fscache_test.go @@ -97,7 +97,7 @@ func TestFSCache(t *testing.T) { assert.Equal(t, s, int64(8)) // prune deletes everything - released, err := fscache.Prune() + released, err := fscache.Prune(context.TODO()) assert.Nil(t, err) assert.Equal(t, released, uint64(8)) diff --git a/daemon/prune.go b/daemon/prune.go index 1d8686b36f..9f8a545e7c 100644 --- a/daemon/prune.go +++ b/daemon/prune.go @@ -74,8 +74,8 @@ func (daemon *Daemon) ContainersPrune(ctx context.Context, pruneFilters filters. for _, c := range allContainers { select { case <-ctx.Done(): - logrus.Warnf("ContainersPrune operation cancelled: %#v", *rep) - return rep, ctx.Err() + logrus.Debugf("ContainersPrune operation cancelled: %#v", *rep) + return rep, nil default: } @@ -121,7 +121,7 @@ func (daemon *Daemon) VolumesPrune(ctx context.Context, pruneFilters filters.Arg pruneVols := func(v volume.Volume) error { select { case <-ctx.Done(): - logrus.Warnf("VolumesPrune operation cancelled: %#v", *rep) + logrus.Debugf("VolumesPrune operation cancelled: %#v", *rep) return ctx.Err() default: } @@ -153,6 +153,9 @@ func (daemon *Daemon) VolumesPrune(ctx context.Context, pruneFilters filters.Arg } err = daemon.traverseLocalVolumes(pruneVols) + if err == context.Canceled { + return rep, nil + } return rep, err } @@ -303,8 +306,7 @@ deleteImagesLoop: } if canceled { - logrus.Warnf("ImagesPrune operation cancelled: %#v", *rep) - return nil, ctx.Err() + logrus.Debugf("ImagesPrune operation cancelled: %#v", *rep) } return rep, nil @@ -320,6 +322,7 @@ func (daemon *Daemon) localNetworksPrune(ctx context.Context, pruneFilters filte l := func(nw libnetwork.Network) bool { select { case <-ctx.Done(): + // context cancelled return true default: } @@ -370,7 +373,7 @@ func (daemon *Daemon) clusterNetworksPrune(ctx context.Context, pruneFilters fil for _, nw := range networks { select { case <-ctx.Done(): - return rep, ctx.Err() + return rep, nil default: if nw.Ingress { // Routing-mesh network removal has to be explicitly invoked by user @@ -427,8 +430,8 @@ func (daemon *Daemon) NetworksPrune(ctx context.Context, pruneFilters filters.Ar select { case <-ctx.Done(): - logrus.Warnf("NetworksPrune operation cancelled: %#v", *rep) - return nil, ctx.Err() + logrus.Debugf("NetworksPrune operation cancelled: %#v", *rep) + return rep, nil default: }