-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
serve.go
69 lines (57 loc) · 1.69 KB
/
serve.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
package endure
import (
"reflect"
"sort"
"github.com/roadrunner-server/endure/v2/graph"
"github.com/roadrunner-server/errors"
"go.uber.org/zap"
)
func (e *Endure) serve() error {
/*
topological order
*/
vertices := e.graph.TopologicalOrder()
serveVertices := make([]*graph.Vertex, len(vertices))
copy(serveVertices, vertices)
sort.Slice(serveVertices, func(i, j int) bool {
return serveVertices[i].Weight() > serveVertices[j].Weight()
})
if len(serveVertices) == 0 {
return errors.E(errors.Str("error occurred, nothing to run"))
}
for i := 0; i < len(serveVertices); i++ {
if !serveVertices[i].IsActive() {
continue
}
if !reflect.TypeOf(serveVertices[i].Plugin()).Implements(reflect.TypeOf((*Service)(nil)).Elem()) {
continue
}
serveMethod, _ := reflect.TypeOf(serveVertices[i].Plugin()).MethodByName(ServeMethodName)
var inVals []reflect.Value
inVals = append(inVals, reflect.ValueOf(serveVertices[i].Plugin()))
e.log.Debug("calling serve method", zap.String("plugin", serveVertices[i].ID().String()))
ret := serveMethod.Func.Call(inVals)[0].Interface()
if ret != nil {
if errCh, ok := ret.(chan error); ok && errCh != nil {
// check if we have an error in the user's channel
select {
case er := <-errCh:
return errors.E(
errors.FunctionCall,
errors.Errorf(
"serve error from the plugin %s stopping execution, error: %v",
serveVertices[i].ID().String(), er),
)
default:
// if we don't have an error in the user's channel, activate poller
e.poll(&result{
// listen for the user's error channel
errCh: errCh,
vertexID: serveVertices[i].ID().String(),
})
}
}
}
}
return nil
}