-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmanager.go
139 lines (115 loc) · 2.51 KB
/
manager.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
package croner
import (
"gopkg.in/robfig/cron.v2"
"time"
)
type CronManager struct {
JobMap map[int] *WrappedJob
MainCron *cron.Cron
jobReturnsWithEid chan JobRunReturnWithEid
stop chan struct{}
ignorePanic bool
onlyOne bool
poolSize uint
timeInterrupt uint
running bool
}
func NewCronManager(c CronManagerConfig) *CronManager {
if c.PoolSize > 0 {
permit = make(chan struct{}, c.PoolSize)
}
return &CronManager{
make(map[int] *WrappedJob),
cron.New(),
make(chan JobRunReturnWithEid, 10),
make(chan struct{}),
c.IgnorePanic,
c.OnlyOne,
c.PoolSize,
c.TimeInterrupt,
false,
}
}
type CronManagerConfig struct {
IgnorePanic bool
OnlyOne bool
PoolSize uint
TimeInterrupt uint
}
func (r *CronManager) SetConfig(cfg CronManagerConfig) {
if r.running {
panic("Can't set config when manager running")
}
if r.poolSize != cfg.PoolSize && cfg.PoolSize > 0 {
permit = make(chan struct{}, cfg.PoolSize)
}
r.ignorePanic = cfg.IgnorePanic
r.onlyOne = cfg.OnlyOne
r.poolSize = cfg.PoolSize
r.timeInterrupt = cfg.TimeInterrupt
}
func (r *CronManager) Add(spec string, j JobInf, info interface{}) (int, error){
schedule, err := cron.Parse(spec)
if err != nil {
return -1, err
}
wrappedJob := NewWrappedJob(j, r)
entryId := int(r.MainCron.Schedule(schedule, wrappedJob))
r.JobMap[entryId] = wrappedJob
wrappedJob.Id = entryId
wrappedJob.Info = info
return entryId, nil
}
func (r *CronManager) Remove(id int) {
r.MainCron.Remove(cron.EntryID(id))
delete(r.JobMap, id)
}
func (r *CronManager) DisActive(id int) {
r.MainCron.Remove(cron.EntryID(id))
r.JobMap[id].status = STOP
}
func (r *CronManager) RemoveAll() {
// It's safe when deleting key in loop
for id := range r.JobMap {
r.Remove(id)
}
}
func (r *CronManager) Start() {
if !r.running{
r.MainCron.Start()
r.run()
r.running = true
}
}
func (r *CronManager) Stop() {
r.MainCron.Stop()
r.stop <- struct{}{}
r.running = false
}
func (r *CronManager) Job(id int) (*WrappedJob, bool) {
v, ok := r.JobMap[id]
return v, ok
}
func (r *CronManager) run() {
go func(){
for {
select {
case value := <-r.jobReturnsWithEid:
jobReturnHooks.Run(&value)
case <-r.stop:
return
}
}
}()
}
func Validate(spec string) bool{
_, err := cron.Parse(spec)
return err == nil
}
func Next(spec string) (time.Time, error) {
schema, err := cron.Parse(spec)
if err != nil {
return time.Time{}, err
}
return schema.Next(time.Now()), nil
}