-
Notifications
You must be signed in to change notification settings - Fork 2
/
scheduler.go
50 lines (41 loc) · 923 Bytes
/
scheduler.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
package main
import (
"github.com/robfig/cron"
"log"
)
type Scheduler struct {
Instance *cron.Cron
}
var Skeddy *Scheduler
func NewScheduler() *Scheduler {
return &Scheduler{Instance: cron.New()}
}
func (s *Scheduler) Start(entries []*Entry) {
log.Println("Starting scheduler ...")
s.Instance.Start()
for _, e := range entries {
s.AddEntry(e)
}
}
func (s *Scheduler) AddEntry(e *Entry) {
log.Println("Adding entry", e)
s.Instance.AddFunc(e.Expression, func() { Dispatch(e.Endpoint, e.Payload) })
}
func (s *Scheduler) ReStart(entries []*Entry) {
log.Println("Restarting scheduler ...")
s.Instance.Stop()
s.Instance = cron.New()
s.Instance.Start()
for _, e := range entries {
s.AddEntry(e)
}
}
func StartScheduler() {
Skeddy = NewScheduler()
entries := Store.AllEntries()
Skeddy.Start(entries)
}
func ParseExpression(expression string) error {
_, err := cron.Parse(expression)
return err
}