1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Prevent multiple identical parallel pruning operations

Signed-off-by: Kenfe-Mickael Laventure <mickael.laventure@gmail.com>
This commit is contained in:
Kenfe-Mickael Laventure 2017-04-12 14:04:49 -07:00
parent 0dee69799e
commit 3279ca3c00
2 changed files with 30 additions and 7 deletions

View file

@ -113,10 +113,7 @@ type Daemon struct {
seccompProfilePath string seccompProfilePath string
diskUsageRunning int32 diskUsageRunning int32
containersPruneRunning int32 pruneRunning int32
volumesPruneRunning int32
imagesPruneRunning int32
networksPruneRunning int32
} }
// HasExperimental returns whether the experimental features of the daemon are enabled or not // HasExperimental returns whether the experimental features of the daemon are enabled or not

View file

@ -3,10 +3,9 @@ package daemon
import ( import (
"fmt" "fmt"
"regexp" "regexp"
"sync/atomic"
"time" "time"
"golang.org/x/net/context"
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"github.com/docker/distribution/reference" "github.com/docker/distribution/reference"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
@ -19,10 +18,22 @@ import (
"github.com/docker/docker/volume" "github.com/docker/docker/volume"
"github.com/docker/libnetwork" "github.com/docker/libnetwork"
digest "github.com/opencontainers/go-digest" digest "github.com/opencontainers/go-digest"
"golang.org/x/net/context"
)
var (
// ErrPruneRunning is returned when a prune request is received while
// one is in progress
ErrPruneRunning = fmt.Errorf("a prune operation is already running")
) )
// ContainersPrune removes unused containers // ContainersPrune removes unused containers
func (daemon *Daemon) ContainersPrune(ctx context.Context, pruneFilters filters.Args) (*types.ContainersPruneReport, error) { func (daemon *Daemon) ContainersPrune(ctx context.Context, pruneFilters filters.Args) (*types.ContainersPruneReport, error) {
if !atomic.CompareAndSwapInt32(&daemon.pruneRunning, 0, 1) {
return nil, ErrPruneRunning
}
defer atomic.StoreInt32(&daemon.pruneRunning, 0)
rep := &types.ContainersPruneReport{} rep := &types.ContainersPruneReport{}
until, err := getUntilFromPruneFilters(pruneFilters) until, err := getUntilFromPruneFilters(pruneFilters)
@ -65,6 +76,11 @@ func (daemon *Daemon) ContainersPrune(ctx context.Context, pruneFilters filters.
// VolumesPrune removes unused local volumes // VolumesPrune removes unused local volumes
func (daemon *Daemon) VolumesPrune(ctx context.Context, pruneFilters filters.Args) (*types.VolumesPruneReport, error) { func (daemon *Daemon) VolumesPrune(ctx context.Context, pruneFilters filters.Args) (*types.VolumesPruneReport, error) {
if !atomic.CompareAndSwapInt32(&daemon.pruneRunning, 0, 1) {
return nil, ErrPruneRunning
}
defer atomic.StoreInt32(&daemon.pruneRunning, 0)
rep := &types.VolumesPruneReport{} rep := &types.VolumesPruneReport{}
pruneVols := func(v volume.Volume) error { pruneVols := func(v volume.Volume) error {
@ -108,6 +124,11 @@ func (daemon *Daemon) VolumesPrune(ctx context.Context, pruneFilters filters.Arg
// ImagesPrune removes unused images // ImagesPrune removes unused images
func (daemon *Daemon) ImagesPrune(ctx context.Context, pruneFilters filters.Args) (*types.ImagesPruneReport, error) { func (daemon *Daemon) ImagesPrune(ctx context.Context, pruneFilters filters.Args) (*types.ImagesPruneReport, error) {
if !atomic.CompareAndSwapInt32(&daemon.pruneRunning, 0, 1) {
return nil, ErrPruneRunning
}
defer atomic.StoreInt32(&daemon.pruneRunning, 0)
rep := &types.ImagesPruneReport{} rep := &types.ImagesPruneReport{}
danglingOnly := true danglingOnly := true
@ -331,6 +352,11 @@ func (daemon *Daemon) clusterNetworksPrune(ctx context.Context, pruneFilters fil
// NetworksPrune removes unused networks // NetworksPrune removes unused networks
func (daemon *Daemon) NetworksPrune(ctx context.Context, pruneFilters filters.Args) (*types.NetworksPruneReport, error) { func (daemon *Daemon) NetworksPrune(ctx context.Context, pruneFilters filters.Args) (*types.NetworksPruneReport, error) {
if !atomic.CompareAndSwapInt32(&daemon.pruneRunning, 0, 1) {
return nil, ErrPruneRunning
}
defer atomic.StoreInt32(&daemon.pruneRunning, 0)
if _, err := getUntilFromPruneFilters(pruneFilters); err != nil { if _, err := getUntilFromPruneFilters(pruneFilters); err != nil {
return nil, err return nil, err
} }