This repository has been archived by the owner on Jan 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.go
124 lines (106 loc) · 2.67 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
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
package mantra
import (
"context"
"fmt"
"reflect"
"strings"
"github.com/mustafaturan/bus"
"github.com/thejerf/suture"
"pkg.glorieux.io/mantra/internal/log"
"pkg.glorieux.io/mantra/internal/structs"
)
// Service is a service
type Service interface {
// TODO: See if I can get rid of Stop
Stop() error
}
// Event is an event from bus.Event
type Event *bus.Event
type service struct {
id suture.ServiceToken
ctx context.Context
stop context.CancelFunc
wrappedService Service
wrappedServiceName string
wrappedServiceMethods []reflect.Method
Address *Address
events chan *bus.Event
}
func newService(wrappedService Service) *service {
ctx, stop := context.WithCancel(context.Background())
s := &service{
ctx: ctx,
stop: stop,
wrappedService: wrappedService,
wrappedServiceName: structs.Name(wrappedService),
wrappedServiceMethods: structs.Methods(wrappedService),
events: make(chan *bus.Event),
}
s.Address = newAddress(s.wrappedServiceName)
bus.RegisterHandler(
s.wrappedServiceName,
&bus.Handler{
Matcher: fmt.Sprintf("^%s.*$", s.Address.String()),
Handle: s.handler,
},
)
topics := s.topics()
log.Debugf("Registering topics: %v", topics)
bus.RegisterTopics(topics...)
return s
}
func (s *service) handler(e *bus.Event) {
s.events <- e
}
// Serve runs the service
func (s *service) Serve() {
for {
select {
case <-s.ctx.Done():
return
case e := <-s.events:
for _, m := range s.wrappedServiceMethods {
topicMethodName := e.Topic.Name[strings.LastIndex(e.Topic.Name, ".")+1:]
if topicMethodName != m.Name {
continue
}
numArgs := m.Type.NumIn()
passedArgs := e.Data.([]interface{})
log.Debugf("METHOD: %s %d %v", m.Name, m.Type.NumIn(), m.Func)
if len(passedArgs) != numArgs-1 {
log.Errorf("Bad argument count! Got %d expected %d.", len(passedArgs), numArgs-1)
}
values := []reflect.Value{}
for _, a := range passedArgs {
log.Debug("ARG", a)
values = append(values, reflect.ValueOf(a))
}
t := reflect.ValueOf(s.wrappedService)
meth := t.MethodByName(m.Name)
meth.Call(values)
}
}
}
}
func (s *service) topics() []string {
t := []string{}
for _, m := range s.wrappedServiceMethods {
t = append(t, newTopic(s.Address, m.Name).String())
}
return t
}
// Stop stops the service
func (s *service) Stop() {
log.Warnf("Stopping %s", s.wrappedServiceName)
err := s.wrappedService.Stop()
if err != nil {
log.Error(err)
}
bus.DeregisterTopics(s.topics()...)
bus.DeregisterHandler(s.wrappedServiceName)
s.stop()
close(s.events)
}
func (s *service) String() string {
return s.wrappedServiceName
}