-
Notifications
You must be signed in to change notification settings - Fork 41
/
cron.go
48 lines (40 loc) · 1.08 KB
/
cron.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 tools
import "github.com/robfig/cron/v3"
type CronService struct {
cronJobs map[string]func()
cron *cron.Cron
}
// Cron is the global CronService
var Cron *CronService
// InitCronService creates an instance of CronService
func InitCronService() {
Cron = &CronService{
cronJobs: make(map[string]func(), 0),
cron: cron.New(),
}
}
// AddFunc creates a cronJob fn running at the interval specified by spec. The job can be referenced by name.
func (c *CronService) AddFunc(name string, fn func(), spec string) error {
c.cronJobs[name] = fn
_, err := c.cron.AddFunc(spec, fn)
return err
}
// Run starts the CronService
func (c *CronService) Run() {
c.cron.Start()
}
// RunJob executes the cronJob identified by name even when it's not due.
// Invalid names are ignored silently.
func (c *CronService) RunJob(name string) {
if job, ok := c.cronJobs[name]; ok {
go job()
}
}
// ListCronJobs returns a []string with the names of all cronjobs
func (c *CronService) ListCronJobs() []string {
var l []string
for job := range c.cronJobs {
l = append(l, job)
}
return l
}