-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface.go
112 lines (93 loc) · 2.01 KB
/
interface.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
package cron
import (
. "github.com/infrago/base"
"github.com/robfig/cron/v3"
)
func (this *Module) Register(name string, value Any) {
switch config := value.(type) {
case Job:
this.Job(name, config)
case Filter:
this.Filter(name, config)
}
}
func (this *Module) Configure(global Map) {
var config Map
if vvv, ok := global["corn"].(Map); ok {
config = vvv
}
if config == nil {
return
}
if setting, ok := config["setting"].(Map); ok {
this.config.Setting = setting
}
}
func (this *Module) Initialize() {
if this.initialized {
return
}
//时间记录
for key, job := range this.jobs {
times := make([]string, 0)
if job.Time != "" {
times = append(times, job.Time)
}
if job.Times != nil || len(job.Times) > 0 {
times = append(times, job.Times...)
}
this.jobTimes[key] = times
}
//拦截器
this.filterActions = make([]ctxFunc, 0)
for _, filter := range this.filters {
if filter.Action != nil {
this.filterActions = append(this.filterActions, filter.Action)
}
}
this.initialized = true
}
func (this *Module) Connect() {
if this.connected {
return
}
this.cron = cron.New(cron.WithParser(cron.NewParser(
cron.SecondOptional | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow | cron.Descriptor,
)))
// this.cronEntries = make(map[string][]string, 0)
inst := &Instance{
this,
}
for key, times := range this.jobTimes {
name := key
// ids := make([]string, 0)
for _, crontab := range times {
// timeName := fmt.Sprintf("%s.%v", key, i)
_, err := this.cron.AddFunc(crontab, func() {
inst.Serve(name)
})
if err != nil {
panic("[cron]注册失败:" + err.Error())
}
// ids = append(ids, id)
}
// this.cronEntries[name] = ids
}
this.instance = inst
this.connected = true
}
func (this *Module) Launch() {
if this.launched {
return
}
this.cron.Start()
this.launched = true
}
func (this *Module) Terminate() {
if this.cron != nil {
this.cron.Stop()
}
this.launched = false
this.connected = false
this.initialized = false
}