-
Notifications
You must be signed in to change notification settings - Fork 376
/
cron_trigger.go
42 lines (36 loc) · 1.03 KB
/
cron_trigger.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
package metricprovider
import (
"context"
"fmt"
"time"
"github.com/robfig/cron/v3"
)
type CronTrigger struct {
Name string
Location *time.Location
Start string
End string
}
// IsActive return true if the now is in cron trigger schedule start and end, false if else
func (c *CronTrigger) IsActive(ctx context.Context, now time.Time) (bool, error) {
nowWithLocation := now.In(c.Location)
schedStart, err := cron.ParseStandard(c.Start)
if err != nil {
return false, fmt.Errorf("cron %v unparseable schedule: %v. err: %v", c.Name, c.Start, err)
}
schedEnd, err := cron.ParseStandard(c.End)
if err != nil {
return false, fmt.Errorf("cron %v unparseable schedule: %v. err: %v", c.Name, c.End, err)
}
nextStart := schedStart.Next(nowWithLocation)
nextEnd := schedEnd.Next(nowWithLocation)
nowTimestamp := nowWithLocation.Unix()
switch {
case nextStart.Unix() < nextEnd.Unix() && nowTimestamp < nextStart.Unix():
return false, nil
case nowTimestamp <= nextEnd.Unix():
return true, nil
default:
return false, nil
}
}