Skip to content

Commit

Permalink
feat(delay): Add initial delay
Browse files Browse the repository at this point in the history
  • Loading branch information
merkata committed Aug 19, 2024
1 parent ca88b8d commit b00ddda
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
10 changes: 10 additions & 0 deletions internals/overlord/checkstate/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ func (m *CheckManager) doPerformCheck(task *state.Task, tomb *tombpkg.Tomb) erro
return fmt.Errorf("cannot get check details for perform-check task %q: %v", task.ID(), err)
}

if !details.HasInitialDelay && config.Delay.Value > 0 {
details.HasInitialDelay = true
logger.Debugf("Initial delay for check %q: %v", config.Name, config.Delay.Value)
select {
case <-time.After(config.Delay.Value):
case <-tomb.Dying():
return checkStopped(config.Name, task.Kind(), tomb.Err())
}
}

logger.Debugf("Performing check %q with period %v", details.Name, config.Period.Value)
ticker := time.NewTicker(config.Period.Value)
defer ticker.Stop()
Expand Down
3 changes: 2 additions & 1 deletion internals/overlord/checkstate/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ type checkDetails struct {
Name string `json:"name"`
Failures int `json:"failures"`
// Whether to proceed to next check type when change is ready
Proceed bool `json:"proceed,omitempty"`
Proceed bool `json:"proceed,omitempty"`
HasInitialDelay bool `json:"hasinitialdelay,omitempty"`
}

type performConfigKey struct {
Expand Down
9 changes: 9 additions & 0 deletions internals/plan/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ const (
defaultCheckPeriod = 10 * time.Second
defaultCheckTimeout = 3 * time.Second
defaultCheckThreshold = 3

defaultDelayTimeout = 0 * time.Second
)

type Plan struct {
Expand Down Expand Up @@ -311,6 +313,7 @@ type Check struct {
Period OptionalDuration `yaml:"period,omitempty"`
Timeout OptionalDuration `yaml:"timeout,omitempty"`
Threshold int `yaml:"threshold,omitempty"`
Delay OptionalDuration `yaml:"delay,omitempty"`

// Type-specific check settings (only one of these can be set)
HTTP *HTTPCheck `yaml:"http,omitempty"`
Expand Down Expand Up @@ -347,6 +350,9 @@ func (c *Check) Merge(other *Check) {
if other.Threshold != 0 {
c.Threshold = other.Threshold
}
if other.Delay.IsSet {
c.Delay = other.Delay
}
if other.HTTP != nil {
if c.HTTP == nil {
c.HTTP = &HTTPCheck{}
Expand Down Expand Up @@ -677,6 +683,9 @@ func CombineLayers(layers ...*Layer) (*Layer, error) {
// what it's worth, Kubernetes probes uses a default of 3 too.
check.Threshold = defaultCheckThreshold
}
if !check.Delay.IsSet {
check.Delay.Value = defaultDelayTimeout
}
}

return combined, nil
Expand Down

0 comments on commit b00ddda

Please sign in to comment.