2016-03-18 14:50:19 -04:00
|
|
|
package restartmanager
|
|
|
|
|
|
|
|
import (
|
2016-04-18 14:48:28 -04:00
|
|
|
"errors"
|
2016-03-18 14:50:19 -04:00
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/engine-api/types/container"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
backoffMultiplier = 2
|
|
|
|
defaultTimeout = 100 * time.Millisecond
|
|
|
|
)
|
|
|
|
|
2016-04-18 14:48:28 -04:00
|
|
|
// ErrRestartCanceled is returned when the restart manager has been
|
|
|
|
// canceled and will no longer restart the container.
|
|
|
|
var ErrRestartCanceled = errors.New("restart canceled")
|
|
|
|
|
2016-03-18 14:50:19 -04:00
|
|
|
// RestartManager defines object that controls container restarting rules.
|
|
|
|
type RestartManager interface {
|
|
|
|
Cancel() error
|
2016-03-22 11:46:40 -04:00
|
|
|
ShouldRestart(exitCode uint32, hasBeenManuallyStopped bool) (bool, chan error, error)
|
2016-03-18 14:50:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type restartManager struct {
|
|
|
|
sync.Mutex
|
|
|
|
sync.Once
|
|
|
|
policy container.RestartPolicy
|
2016-03-22 11:46:40 -04:00
|
|
|
restartCount int
|
2016-03-18 14:50:19 -04:00
|
|
|
timeout time.Duration
|
|
|
|
active bool
|
|
|
|
cancel chan struct{}
|
|
|
|
canceled bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new restartmanager based on a policy.
|
2016-03-22 11:46:40 -04:00
|
|
|
func New(policy container.RestartPolicy, restartCount int) RestartManager {
|
|
|
|
return &restartManager{policy: policy, restartCount: restartCount, cancel: make(chan struct{})}
|
2016-03-18 14:50:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rm *restartManager) SetPolicy(policy container.RestartPolicy) {
|
|
|
|
rm.Lock()
|
|
|
|
rm.policy = policy
|
|
|
|
rm.Unlock()
|
|
|
|
}
|
|
|
|
|
2016-03-22 11:46:40 -04:00
|
|
|
func (rm *restartManager) ShouldRestart(exitCode uint32, hasBeenManuallyStopped bool) (bool, chan error, error) {
|
2016-04-14 21:40:20 -04:00
|
|
|
if rm.policy.IsNone() {
|
|
|
|
return false, nil, nil
|
|
|
|
}
|
2016-03-18 14:50:19 -04:00
|
|
|
rm.Lock()
|
|
|
|
unlockOnExit := true
|
|
|
|
defer func() {
|
|
|
|
if unlockOnExit {
|
|
|
|
rm.Unlock()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if rm.canceled {
|
2016-04-18 14:48:28 -04:00
|
|
|
return false, nil, ErrRestartCanceled
|
2016-03-18 14:50:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if rm.active {
|
|
|
|
return false, nil, fmt.Errorf("invalid call on active restartmanager")
|
|
|
|
}
|
|
|
|
|
|
|
|
if rm.timeout == 0 {
|
|
|
|
rm.timeout = defaultTimeout
|
|
|
|
} else {
|
|
|
|
rm.timeout *= backoffMultiplier
|
|
|
|
}
|
|
|
|
|
|
|
|
var restart bool
|
|
|
|
switch {
|
2016-03-22 11:46:40 -04:00
|
|
|
case rm.policy.IsAlways():
|
|
|
|
restart = true
|
|
|
|
case rm.policy.IsUnlessStopped() && !hasBeenManuallyStopped:
|
2016-03-18 14:50:19 -04:00
|
|
|
restart = true
|
|
|
|
case rm.policy.IsOnFailure():
|
|
|
|
// the default value of 0 for MaximumRetryCount means that we will not enforce a maximum count
|
2016-03-22 11:46:40 -04:00
|
|
|
if max := rm.policy.MaximumRetryCount; max == 0 || rm.restartCount < max {
|
2016-03-18 14:50:19 -04:00
|
|
|
restart = exitCode != 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !restart {
|
|
|
|
rm.active = false
|
|
|
|
return false, nil, nil
|
|
|
|
}
|
|
|
|
|
2016-03-22 11:46:40 -04:00
|
|
|
rm.restartCount++
|
|
|
|
|
2016-03-18 14:50:19 -04:00
|
|
|
unlockOnExit = false
|
|
|
|
rm.active = true
|
|
|
|
rm.Unlock()
|
|
|
|
|
|
|
|
ch := make(chan error)
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-rm.cancel:
|
2016-04-18 14:48:28 -04:00
|
|
|
ch <- ErrRestartCanceled
|
2016-03-18 14:50:19 -04:00
|
|
|
close(ch)
|
|
|
|
case <-time.After(rm.timeout):
|
|
|
|
rm.Lock()
|
|
|
|
close(ch)
|
|
|
|
rm.active = false
|
|
|
|
rm.Unlock()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return true, ch, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rm *restartManager) Cancel() error {
|
|
|
|
rm.Do(func() {
|
|
|
|
rm.Lock()
|
|
|
|
rm.canceled = true
|
|
|
|
close(rm.cancel)
|
|
|
|
rm.Unlock()
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|