-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcronex.go
More file actions
49 lines (39 loc) · 818 Bytes
/
cronex.go
File metadata and controls
49 lines (39 loc) · 818 Bytes
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
// guonaihong apache 2.0
package cronex
import "github.com/antlabs/timer"
type TimerNoder = timer.TimeNoder
type Option = timer.Option
// cronex
type Cronex struct {
tm timer.Timer
}
// 初始化一个cronex
func New(opt ...Option) *Cronex {
if len(opt) == 0 {
opt = append(opt, timer.WithMinHeap())
}
return &Cronex{
tm: timer.NewTimer(opt...),
}
}
// 添加函数
func (c *Cronex) AddFunc(spec string, cmd func()) (node TimerNoder, err error) {
var schedule timer.Next
schedule, err = standardParser.Parse(spec)
if err != nil {
return
}
return c.tm.CustomFunc(schedule, cmd), nil
}
// 运行消费者循环
func (c *Cronex) Run() {
c.tm.Run()
}
// 异步运行消费者循环
func (c *Cronex) Start() {
go c.Run()
}
// 关闭cronex的任务循环
func (c *Cronex) Stop() {
c.tm.Stop()
}