-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathcron.go
92 lines (72 loc) · 1.95 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package cherryCron
import (
"fmt"
"time"
clog "github.com/cherry-game/cherry/logger"
"github.com/robfig/cron/v3"
)
var _cron = cron.New(
cron.WithSeconds(),
cron.WithChain(cron.Recover(&CronLogger{})),
)
type CronLogger struct {
}
func (CronLogger) Info(msg string, keysAndValues ...interface{}) {
clog.Infow(msg, keysAndValues...)
}
func (CronLogger) Error(err error, _ string, _ ...interface{}) {
clog.Error(err)
}
func Init(opts ...cron.Option) {
if len(opts) < 1 {
opts = append(opts, cron.WithSeconds())
opts = append(opts, cron.WithChain(cron.Recover(&CronLogger{})))
}
_cron = cron.New(opts...)
}
func AddFunc(spec string, cmd func()) (cron.EntryID, error) {
return _cron.AddJob(spec, cron.FuncJob(cmd))
}
// AddEveryDayFunc 每天的x时x分x秒执行一次(每天1次)
func AddEveryDayFunc(cmd func(), hour, minutes, seconds int) (cron.EntryID, error) {
spec := fmt.Sprintf("%d %d %d * * ?", seconds, minutes, hour)
return _cron.AddFunc(spec, cmd)
}
// AddEveryHourFunc 每小时的x分x秒执行一次(每天24次)
func AddEveryHourFunc(cmd func(), minute, second int) (cron.EntryID, error) {
spec := fmt.Sprintf("%d %d * * * ?", second, minute)
return _cron.AddFunc(spec, cmd)
}
// AddDurationFunc 每间隔x秒执行一次
func AddDurationFunc(cmd func(), duration time.Duration) (cron.EntryID, error) {
spec := fmt.Sprintf("@every %ds", int(duration.Seconds()))
clog.Debug(spec)
return _cron.AddFunc(spec, cmd)
}
func AddJob(spec string, cmd cron.Job) (cron.EntryID, error) {
return _cron.AddJob(spec, cmd)
}
func Schedule(schedule cron.Schedule, cmd cron.Job) cron.EntryID {
return _cron.Schedule(schedule, cmd)
}
func Entries() []cron.Entry {
return _cron.Entries()
}
func Location() *time.Location {
return _cron.Location()
}
func Entry(id cron.EntryID) cron.Entry {
return _cron.Entry(id)
}
func Remove(id cron.EntryID) {
_cron.Remove(id)
}
func Start() {
_cron.Start()
}
func Run() {
_cron.Run()
}
func Stop() {
_cron.Stop()
}