Skip to content

Commit

Permalink
Add comments and move delay calc to TaskRunner
Browse files Browse the repository at this point in the history
  • Loading branch information
schmichael committed Sep 11, 2017
1 parent c774977 commit a0a0d1d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
7 changes: 4 additions & 3 deletions client/task_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -1716,10 +1716,11 @@ func (r *TaskRunner) Restart(source, reason string, failure bool) {
}
}

// RestartDelay returns the value of the delay for this task's restart policy
// for use by the healtcheck watcher.
// RestartDelay returns the *max* value of the delay for this task's restart
// policy for use by the healtcheck watcher.
func (r *TaskRunner) RestartDelay() time.Duration {
return r.alloc.Job.LookupTaskGroup(r.alloc.TaskGroup).RestartPolicy.Delay
delay := r.alloc.Job.LookupTaskGroup(r.alloc.TaskGroup).RestartPolicy.Delay
return delay + time.Duration(float64(delay)*jitter)
}

// Signal will send a signal to the task
Expand Down
17 changes: 12 additions & 5 deletions command/agent/consul/check_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@ const (
defaultPollFreq = 900 * time.Millisecond
)

type ConsulChecks interface {
// ChecksAPI is the part of the Consul API the checkWatcher requires.
type ChecksAPI interface {
// Checks returns a list of all checks.
Checks() (map[string]*api.AgentCheck, error)
}

// TaskRestarter allows the checkWatcher to restart tasks and how long the
// grace period should be afterward.
type TaskRestarter interface {
RestartDelay() time.Duration
Restart(source, reason string, failure bool)
Expand Down Expand Up @@ -96,19 +100,22 @@ func (c *checkRestart) update(now time.Time, status string) {
const failure = true
c.task.Restart("healthcheck", fmt.Sprintf("check %q unhealthy", c.checkName), failure)

// Reset grace time to grace + restart.delay + (restart.delay * 25%) (the max jitter)
c.graceUntil = now.Add(c.grace + c.restartDelay + time.Duration(float64(c.restartDelay)*0.25))
// Reset grace time to grace + restart.delay
c.graceUntil = now.Add(c.grace + c.restartDelay)
c.unhealthyStart = time.Time{}
}
}

// checkWatcher watches Consul checks and restarts tasks when they're
// unhealthy.
type checkWatcher struct {
consul ConsulChecks
consul ChecksAPI

// pollFreq is how often to poll the checks API and defaults to
// defaultPollFreq
pollFreq time.Duration

// watchCh is how watches (and removals) are sent to the main watching loop
watchCh chan *checkRestart

// done is closed when Run has exited
Expand All @@ -122,7 +129,7 @@ type checkWatcher struct {
}

// newCheckWatcher creates a new checkWatcher but does not call its Run method.
func newCheckWatcher(logger *log.Logger, consul ConsulChecks) *checkWatcher {
func newCheckWatcher(logger *log.Logger, consul ChecksAPI) *checkWatcher {
return &checkWatcher{
consul: consul,
pollFreq: defaultPollFreq,
Expand Down

0 comments on commit a0a0d1d

Please sign in to comment.