forked from bigpigeon/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schedule.go
51 lines (41 loc) · 904 Bytes
/
schedule.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
package schedule
import (
"strings"
"time"
"github.com/elastic/beats/heartbeat/scheduler"
"github.com/elastic/beats/heartbeat/scheduler/schedule/cron"
)
type Schedule struct {
scheduler.Schedule
}
type intervalScheduler struct {
interval time.Duration
}
func Parse(in string) (*Schedule, error) {
every := "@every"
// add '@every' keyword
if strings.HasPrefix(in, every) {
interval := strings.TrimSpace(in[len(every):])
d, err := time.ParseDuration(interval)
if err != nil {
return nil, err
}
return &Schedule{intervalScheduler{d}}, nil
}
// fallback on cron scheduler parsers
s, err := cron.Parse(in)
if err != nil {
return nil, err
}
return &Schedule{s}, nil
}
func (s intervalScheduler) Next(t time.Time) time.Time {
return t.Add(s.interval)
}
func (s *Schedule) Unpack(str string) error {
tmp, err := Parse(str)
if err == nil {
*s = *tmp
}
return err
}