Browse code

return prune data when context canceled

Signed-off-by: allencloud <allen.sun@daocloud.io>

allencloud authored on 2017/07/06 17:47:21
Showing 4 changed files
... ...
@@ -70,7 +70,7 @@ func (b *Backend) Build(ctx context.Context, config backend.BuildConfig) (string
70 70
 
71 71
 // PruneCache removes all cached build sources
72 72
 func (b *Backend) PruneCache(ctx context.Context) (*types.BuildCachePruneReport, error) {
73
-	size, err := b.fsCache.Prune()
73
+	size, err := b.fsCache.Prune(ctx)
74 74
 	if err != nil {
75 75
 		return nil, errors.Wrap(err, "failed to prune build cache")
76 76
 	}
... ...
@@ -154,8 +154,8 @@ func (fsc *FSCache) DiskUsage() (int64, error) {
154 154
 }
155 155
 
156 156
 // Prune allows manually cleaning up the cache
157
-func (fsc *FSCache) Prune() (uint64, error) {
158
-	return fsc.store.Prune()
157
+func (fsc *FSCache) Prune(ctx context.Context) (uint64, error) {
158
+	return fsc.store.Prune(ctx)
159 159
 }
160 160
 
161 161
 // Close stops the gc and closes the persistent db
... ...
@@ -396,12 +396,19 @@ func (s *fsCacheStore) DiskUsage() (int64, error) {
396 396
 }
397 397
 
398 398
 // Prune allows manually cleaning up the cache
399
-func (s *fsCacheStore) Prune() (uint64, error) {
399
+func (s *fsCacheStore) Prune(ctx context.Context) (uint64, error) {
400 400
 	s.mu.Lock()
401 401
 	defer s.mu.Unlock()
402 402
 	var size uint64
403 403
 
404 404
 	for id, snap := range s.sources {
405
+		select {
406
+		case <-ctx.Done():
407
+			logrus.Debugf("Cache prune operation cancelled, pruned size: %d", size)
408
+			// when the context is cancelled, only return current size and nil
409
+			return size, nil
410
+		default:
411
+		}
405 412
 		if len(snap.refs) == 0 {
406 413
 			ss, err := snap.getSize()
407 414
 			if err != nil {
... ...
@@ -97,7 +97,7 @@ func TestFSCache(t *testing.T) {
97 97
 	assert.Equal(t, s, int64(8))
98 98
 
99 99
 	// prune deletes everything
100
-	released, err := fscache.Prune()
100
+	released, err := fscache.Prune(context.TODO())
101 101
 	assert.Nil(t, err)
102 102
 	assert.Equal(t, released, uint64(8))
103 103
 
... ...
@@ -74,8 +74,8 @@ func (daemon *Daemon) ContainersPrune(ctx context.Context, pruneFilters filters.
74 74
 	for _, c := range allContainers {
75 75
 		select {
76 76
 		case <-ctx.Done():
77
-			logrus.Warnf("ContainersPrune operation cancelled: %#v", *rep)
78
-			return rep, ctx.Err()
77
+			logrus.Debugf("ContainersPrune operation cancelled: %#v", *rep)
78
+			return rep, nil
79 79
 		default:
80 80
 		}
81 81
 
... ...
@@ -121,7 +121,7 @@ func (daemon *Daemon) VolumesPrune(ctx context.Context, pruneFilters filters.Arg
121 121
 	pruneVols := func(v volume.Volume) error {
122 122
 		select {
123 123
 		case <-ctx.Done():
124
-			logrus.Warnf("VolumesPrune operation cancelled: %#v", *rep)
124
+			logrus.Debugf("VolumesPrune operation cancelled: %#v", *rep)
125 125
 			return ctx.Err()
126 126
 		default:
127 127
 		}
... ...
@@ -153,6 +153,9 @@ func (daemon *Daemon) VolumesPrune(ctx context.Context, pruneFilters filters.Arg
153 153
 	}
154 154
 
155 155
 	err = daemon.traverseLocalVolumes(pruneVols)
156
+	if err == context.Canceled {
157
+		return rep, nil
158
+	}
156 159
 
157 160
 	return rep, err
158 161
 }
... ...
@@ -303,8 +306,7 @@ deleteImagesLoop:
303 303
 	}
304 304
 
305 305
 	if canceled {
306
-		logrus.Warnf("ImagesPrune operation cancelled: %#v", *rep)
307
-		return nil, ctx.Err()
306
+		logrus.Debugf("ImagesPrune operation cancelled: %#v", *rep)
308 307
 	}
309 308
 
310 309
 	return rep, nil
... ...
@@ -320,6 +322,7 @@ func (daemon *Daemon) localNetworksPrune(ctx context.Context, pruneFilters filte
320 320
 	l := func(nw libnetwork.Network) bool {
321 321
 		select {
322 322
 		case <-ctx.Done():
323
+			// context cancelled
323 324
 			return true
324 325
 		default:
325 326
 		}
... ...
@@ -370,7 +373,7 @@ func (daemon *Daemon) clusterNetworksPrune(ctx context.Context, pruneFilters fil
370 370
 	for _, nw := range networks {
371 371
 		select {
372 372
 		case <-ctx.Done():
373
-			return rep, ctx.Err()
373
+			return rep, nil
374 374
 		default:
375 375
 			if nw.Ingress {
376 376
 				// 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
427 427
 
428 428
 	select {
429 429
 	case <-ctx.Done():
430
-		logrus.Warnf("NetworksPrune operation cancelled: %#v", *rep)
431
-		return nil, ctx.Err()
430
+		logrus.Debugf("NetworksPrune operation cancelled: %#v", *rep)
431
+		return rep, nil
432 432
 	default:
433 433
 	}
434 434