forked from robfig/cron
-
Notifications
You must be signed in to change notification settings - Fork 2
/
constantdelay.go
56 lines (47 loc) · 1.73 KB
/
constantdelay.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package cron
import (
"time"
)
// ConstantDelaySchedule represents a simple recurring duty cycle, e.g. "Every 5 minutes".
// It does not support jobs more frequent than once a second.
type ConstantDelaySchedule struct {
Delay time.Duration
startAt time.Time
}
// Every returns a crontab Schedule that activates once every duration.
// Delays of less than a second are not supported (will round up to 1 second).
// Any fields less than a Second are truncated.
func Every(duration time.Duration) ConstantDelaySchedule {
if duration < time.Second {
duration = time.Second
}
return ConstantDelaySchedule{
Delay: duration.Truncate(time.Second),
}
}
// StartingAt sets the start time of the simple recurring duty cycle. This is useful
// when we want the cycle to start at a specific date which makes it independent
// from the Cron start time.
func (schedule ConstantDelaySchedule) StartingAt(start time.Time) ConstantDelaySchedule {
schedule.startAt = start.Add(-time.Duration(start.Nanosecond()) * time.Nanosecond)
return schedule
}
// Next returns the next time this should be run.
// This rounds so that the next activation time will be on the second.
func (schedule ConstantDelaySchedule) Next(t time.Time) time.Time {
if !schedule.startAt.IsZero() {
if schedule.startAt.After(t) {
return schedule.startAt
}
t = schedule.startAt.Add((t.Sub(schedule.startAt) / schedule.Delay) * (schedule.Delay))
}
return t.Add(schedule.Delay).Truncate(time.Second)
}
func (schedule ConstantDelaySchedule) IsOnce() bool {
return false
}
// Prev returns the previous time this should be run.
// This rounds so that the previous activation time will be on the second.
func (schedule ConstantDelaySchedule) Prev(t time.Time) time.Time {
return time.Time{}
}