Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional skew to interval #22

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ level of precision is better left to other tools.
* `interval`: *Optional.* The interval on which to report new versions. Valid
values: `60s`, `90m`, `1h`.

* `skew`: *Optional.* A maximum bound of time by which to skew the trigger
time. Valid values: `60s`, `90m`, `1h`. Must be used with `interval`.

* `location`: *Optional. Default `UTC`.* The
[location](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) in
which to interpret `start`, `stop`, and `days`.
Expand Down Expand Up @@ -95,6 +98,25 @@ jobs:
config: # ...
```

### Periodic trigger with some skewing

```yaml
resources:
- name: 5-ish-min
type: time
source:
interval: 5m
skew: 1m

jobs:
- name: something-every-4-to-6m
plan:
- get: 5-ish-min
trigger: true
- task: something
config: # ...
```

### Trigger once within time range

```yaml
Expand Down
1 change: 1 addition & 0 deletions check/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func main() {
Start: request.Source.Start,
Stop: request.Source.Stop,
Interval: request.Source.Interval,
Skew: request.Source.Skew,
Days: request.Source.Days,
}

Expand Down
8 changes: 7 additions & 1 deletion lord/time_lord.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lord

import (
"math/rand"
"time"

"github.com/concourse/time-resource/models"
Expand All @@ -12,6 +13,7 @@ type TimeLord struct {
Start *models.TimeOfDay
Stop *models.TimeOfDay
Interval *models.Interval
Skew *models.Interval
Days []models.Weekday
}

Expand All @@ -29,7 +31,11 @@ func (tl TimeLord) Check(now time.Time) bool {
}

if tl.Interval != nil {
if now.Sub(tl.PreviousTime) >= time.Duration(*tl.Interval) {
skew := time.Duration(0)
if tl.Skew != nil {
skew = time.Duration(rand.Intn(int(time.Duration(*tl.Interval).Seconds())))
}
if now.Sub(tl.PreviousTime)+skew >= time.Duration(*tl.Interval) {
return true
}
} else {
Expand Down
5 changes: 5 additions & 0 deletions models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type CheckResponse []Version

type Source struct {
Interval *Interval `json:"interval"`
Skew *Interval `json:"skew"`
Start *TimeOfDay `json:"start"`
Stop *TimeOfDay `json:"stop"`
Days []Weekday `json:"days"`
Expand All @@ -51,6 +52,10 @@ func (source Source) Validate() error {
return errors.New("must configure either 'interval' or 'start' and 'stop'")
}

if source.Interval == nil && source.Skew != nil {
return errors.New("must configure either 'interval' if 'skew' is used")
}

if source.Start != nil && source.Stop == nil {
return errors.New("must configure 'stop' if 'start' is set")
}
Expand Down