-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcron.go
65 lines (55 loc) · 1.29 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
package cronjob
import (
"github.com/better-go/pkg/log"
"github.com/robfig/cron/v3"
)
/*
cron job:
- cron 表达式:
- http://www.quartz-scheduler.org/documentation/quartz-2.3.0/tutorials/tutorial-lesson-06.html
- https://en.wikipedia.org/wiki/Cron
*/
type CronJob struct {
Cron *cron.Cron // 暴露原库 API
}
func New() *CronJob {
return &CronJob{
Cron: cron.New(
cron.WithSeconds(), // 支持解析秒
cron.WithChain(),
),
}
}
// 注册 task:
func (m *CronJob) RegisterTask(tasks ...Task) (err error) {
// batch register:
for _, item := range tasks {
// register:
if entryID, err := m.Cron.AddFunc(item.Schedule, item.TaskFunc); err != nil {
log.Errorf("cron job register tasks func error:, entryID=%v, err=%v", entryID, err)
}
}
return err
}
// 注册和启动分开, 灵活调用位置
func (m *CronJob) Run(tasks ...Task) {
m.RunAsync(tasks...)
}
// 异步:
func (m *CronJob) RunAsync(tasks ...Task) {
// 允许在 run 中注册, 也可以分开, 传空即可
_ = m.RegisterTask(tasks...)
// 启动: 异步方式
m.Cron.Start()
}
// 同步:
func (m *CronJob) RunSync(tasks ...Task) {
// 允许在 run 中注册, 也可以分开, 传空即可
_ = m.RegisterTask(tasks...)
// 启动: 同步方式
m.Cron.Run()
}
// stop:
func (m *CronJob) Stop() {
m.Cron.Stop()
}