/
service.go
92 lines (80 loc) · 2.02 KB
/
service.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
package event
import (
"fmt"
"github.com/apex/log"
"github.com/crawlab-team/crawlab-core/entity"
"github.com/crawlab-team/crawlab-core/interfaces"
"github.com/crawlab-team/crawlab-core/utils"
"github.com/crawlab-team/go-trace"
"github.com/thoas/go-funk"
"regexp"
)
var S interfaces.EventService
type Service struct {
keys []string
includes []string
excludes []string
chs []*chan interfaces.EventData
}
func (svc *Service) Register(key, include, exclude string, ch *chan interfaces.EventData) {
svc.keys = append(svc.keys, key)
svc.includes = append(svc.includes, include)
svc.excludes = append(svc.excludes, exclude)
svc.chs = append(svc.chs, ch)
}
func (svc *Service) Unregister(key string) {
idx := funk.IndexOfString(svc.keys, key)
if idx != -1 {
svc.keys = append(svc.keys[:idx], svc.keys[(idx+1):]...)
svc.includes = append(svc.includes[:idx], svc.includes[(idx+1):]...)
svc.excludes = append(svc.excludes[:idx], svc.excludes[(idx+1):]...)
svc.chs = append(svc.chs[:idx], svc.chs[(idx+1):]...)
log.Infof("[EventService] unregistered %s", key)
}
}
func (svc *Service) SendEvent(eventName string, data ...interface{}) {
for i, key := range svc.keys {
// include
include := svc.includes[i]
matchedInclude, err := regexp.MatchString(include, eventName)
if err != nil {
trace.PrintError(err)
continue
}
if !matchedInclude {
continue
}
// exclude
exclude := svc.excludes[i]
matchedExclude, err := regexp.MatchString(exclude, eventName)
if err != nil {
trace.PrintError(err)
continue
}
if matchedExclude {
continue
}
// send event
utils.LogDebug(fmt.Sprintf("key %s matches event %s", key, eventName))
ch := svc.chs[i]
go func(ch *chan interfaces.EventData) {
for _, d := range data {
*ch <- &entity.EventData{
Event: eventName,
Data: d,
}
}
}(ch)
}
}
func NewEventService() (svc interfaces.EventService) {
if S != nil {
return S
}
svc = &Service{
chs: []*chan interfaces.EventData{},
keys: []string{},
}
S = svc
return svc
}