-
Notifications
You must be signed in to change notification settings - Fork 24
/
schedule.go
48 lines (39 loc) · 978 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
// Package cron handles anything related to time.
package cron
import (
"fmt"
"time"
"github.com/robfig/cron/v3"
)
// cronSchedule holds a parsed cron expression and its original input.
type cronSchedule struct {
spec string
schedule cron.Schedule
}
// New creates a new Schedule.
func New(spec string) (Schedule, error) {
sche, err := cron.ParseStandard(spec)
if err != nil {
return nil, fmt.Errorf("parsing %q: %w", spec, err)
}
return cronSchedule{
spec: spec,
schedule: sche,
}, nil
}
// MustNew creates a new Schedule, and panics if it fails to parse the input.
func MustNew(spec string) Schedule {
cron, err := New(spec)
if err != nil {
panic(fmt.Errorf(`schedule.MustNew failed: %w`, err))
}
return cron
}
// Next tells the next scheduled time.
func (s cronSchedule) Next() time.Time {
return s.schedule.Next(time.Now())
}
// Describe gives back the original cron string.
func (s cronSchedule) Describe() string {
return s.spec
}