Skip to content

Commit

Permalink
return prune data when context canceled
Browse files Browse the repository at this point in the history
Signed-off-by: allencloud <[email protected]>
  • Loading branch information
allencloud committed Jul 10, 2017
1 parent ced0b6c commit 87b4dc2
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
2 changes: 1 addition & 1 deletion api/server/backend/build/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
13 changes: 10 additions & 3 deletions builder/fscache/fscache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion builder/fscache/fscache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
19 changes: 11 additions & 8 deletions daemon/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
}

Expand Down Expand Up @@ -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:
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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:
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
}

Expand Down

0 comments on commit 87b4dc2

Please sign in to comment.