func (p *PauseCommand) Run(ctx context.Context, random bool) error {
	log.Debug("pausing all matching containers")
	log.WithFields(log.Fields{
		"names":    p.names,
		"pattern":  p.pattern,
		"labels":   p.labels,
		"duration": p.duration,
		"limit":    p.limit,
		"random":   random,
	}).Debug("listing matching containers")
	containers, err := container.ListNContainers(ctx, p.client, p.names, p.pattern, p.labels, p.limit)
	if err != nil {
		return err
	}
	if len(containers) == 0 {
		log.Warning("no containers to stop")
		return nil
	}
	if random {
		if c := container.RandomContainer(containers); c != nil {
			containers = []*container.Container{c}
		}
	}
	pausedContainers := []*container.Container{}
	for _, container := range containers {
		log.WithFields(log.Fields{
			"container": container,
			"duration":  p.duration,
		}).Debug("pausing container for duration")
		c := container
		err = p.client.PauseContainer(ctx, c, p.dryRun)
		if err != nil {
			log.WithError(err).Warn("failed to pause container")
			break
		}
		pausedContainers = append(pausedContainers, container)
	}
	if len(pausedContainers) > 0 {
		select {
		case <-ctx.Done():
			log.Debug("unpause containers by stop event")
			err = p.unpauseContainers(context.Background(), pausedContainers)
		case <-time.After(p.duration):
			log.WithField("duration", p.duration).Debug("unpause containers after duration")
			err = p.unpauseContainers(ctx, pausedContainers)
		}
	}
	return err
}
Run pause command
package docker
import (
	"context"
	"time"
	"github.com/alexei-led/pumba/pkg/chaos"
	"github.com/alexei-led/pumba/pkg/container"
	"github.com/alexei-led/pumba/pkg/util"
	"github.com/pkg/errors"
	log "github.com/sirupsen/logrus"
)
type PauseCommand struct {
	client   container.Client
	names    []string
	pattern  string
	labels   []string
	duration time.Duration
	limit    int
	dryRun   bool
}
func NewPauseCommand(client container.Client, names []string, pattern string, labels []string, intervalStr, durationStr string, limit int, dryRun bool) (chaos.Command, error) {
	interval, err := util.GetIntervalValue(intervalStr)
	if err != nil {
		return nil, err
	}
	duration, err := util.GetDurationValue(durationStr, interval)
	if err != nil {
		return nil, err
	}
	return &PauseCommand{client, names, pattern, labels, duration, limit, dryRun}, nil
}