-
Notifications
You must be signed in to change notification settings - Fork 0
/
crontab.go
159 lines (133 loc) · 3.09 KB
/
crontab.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package crontab
import (
"fmt"
"log"
"sync"
"errors"
cron "github.com/robfig/cron/v3"
)
// Crontab crontab manager
type Crontab struct {
inner *cron.Cron
ids map[string]cron.EntryID
mutex sync.Mutex
}
// NewCrontab new crontab
func NewCrontab() *Crontab {
return &Crontab{
inner: cron.New(cron.WithSeconds()),
ids: make(map[string]cron.EntryID),
}
}
//// NewCrontab new crontab
//func (c *Crontab) New() *Crontab {
// return &Crontab{
// inner: cron.New(cron.WithSeconds()),
// ids: make(map[string]cron.EntryID),
// }
//}
// IDs ...
func (c *Crontab) IDs() []string {
c.mutex.Lock()
defer c.mutex.Unlock()
validIDs := make([]string, 0, len(c.ids))
invalidIDs := make([]string, 0)
for sid, eid := range c.ids {
if e := c.inner.Entry(eid); e.ID != eid {
invalidIDs = append(invalidIDs, sid)
continue
}
validIDs = append(validIDs, sid)
}
for _, id := range invalidIDs {
delete(c.ids, id)
}
return validIDs
}
// Start start the crontab engine
func (c *Crontab) Start() {
c.inner.Start()
}
// Stop stop the crontab engine
func (c *Crontab) Stop() {
c.inner.Stop()
}
// DelByID remove one crontab task
func (c *Crontab) DelByID(id string) {
c.mutex.Lock()
defer c.mutex.Unlock()
fmt.Println("id:", id)
eid, ok := c.ids[id]
if !ok {
return
}
c.inner.Remove(eid)
delete(c.ids, id)
}
// AddByID add one crontab task
// id is unique
// spec is the crontab expression
func (c *Crontab) AddByID(id string, spec string, cmd cron.Job) error {
c.mutex.Lock()
defer c.mutex.Unlock()
if _, ok := c.ids[id]; ok {
return errors.New("crontab id exists")
}
eid, err := c.inner.AddJob(spec, cmd)
if err != nil {
return err
}
log.Println("AddByID eid:", eid)
c.ids[id] = eid
return nil
}
// AddByFunc add function as crontab task
func (c *Crontab) AddByFunc(id string, spec string, f func()) error {
c.mutex.Lock()
defer c.mutex.Unlock()
if _, ok := c.ids[id]; ok {
return errors.New("crontab id exists")
}
eid, err := c.inner.AddFunc(spec, f)
if err != nil {
return err
}
c.ids[id] = eid
log.Println("AddByFunc eid:", c.ids)
return nil
}
// IsExists check the crontab task whether existed with job id
func (c *Crontab) IsExists(jid string) bool {
_, exist := c.ids[jid]
return exist
}
//secondStr := "*/30 * * * * ?"
//crontab := crontab.NewCrontab()
// 实现接口的方式添加定时任务
//task := &testTask{}
//type testTask struct{}
//func (t *testTask) Run() {
// log.Println("hello world")
//}
//if err := crontab.AddByID("111-222", "*/5 * * * * ?", task); err != nil {
// fmt.Printf("error to add crontab task:%s", err)
// ch <- 111
//}
//// 添加函数作为定时任务
//taskFunc := func() {
// log.Println("hello add")
//}
//
//if err := crontab.AddByFunc("333-222", "*/12 * * * * ?", taskFunc); err != nil {
// log.Println("error to add crontab task:", err)
// ch <- "333-222"
//}
//crontab.AddByFunc("36015d73-d192-4f2e-a69d-423e8076482e", "*/12 * * * * ?", func() {
// log.Println("error to add crontab task")
// if bl {
// crontab.DelByID("36015d73-d192-4f2e-a69d-423e8076482e")
// }
//})
//crontab.Start()
//fmt.Println(crontab.IDs())
//select {}