/
plugin_host.go
146 lines (117 loc) · 3.14 KB
/
plugin_host.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
137
138
139
140
141
142
143
144
145
146
package plugins
import (
"fmt"
"log"
"net"
"time"
"google.golang.org/grpc"
"golang.org/x/net/context"
"github.com/RackHD/inservice/agent/grpc/host"
)
// PluginResponseTimeout sets the maximum duration allowed to respond to a request
// for a Plugin Service action (start/stop/status).
const PluginResponseTimeout time.Duration = 10 * time.Second
// PluginHost encapsulates a collection of PluginProcesses and manages their
// lifecycles.
type PluginHost struct {
address string
port int
processes []*PluginProcess
notifications chan *PluginProcess
plugins []string
}
// NewPluginHost returns a new PluginHost.
func NewPluginHost(address string, port int, plugins []string) (*PluginHost, error) {
return &PluginHost{
address: address,
port: port,
plugins: plugins,
}, nil
}
// Serve loads and manages all Plugins.
func (h *PluginHost) Serve() {
go h.serveGRPC()
h.servePlugins()
}
func (h *PluginHost) servePlugins() {
for _, plugin := range h.plugins {
h.processes = append(
h.processes,
NewPluginProcess(
plugin,
),
)
}
h.notifications = make(chan *PluginProcess, len(h.plugins))
for _, plugin := range h.processes {
err := plugin.Start(h.notifications)
if err != nil {
log.Printf("Error Starting Plugin: %s, %+v\n", err, plugin)
}
}
// Monitor
for plugin := range h.notifications {
if plugin.Restartable() {
err := plugin.Start(h.notifications)
if err != nil {
log.Printf("Plugin Unable to Restart: %s (%s)\n", plugin.Name, err)
}
log.Printf("Plugin Restarted: %s\n", plugin.Name)
} else {
log.Printf("Plugin Not Set for Restart: %s\n", plugin.Name)
}
}
}
func (h *PluginHost) serveGRPC() {
listen, err := net.Listen(
"tcp",
fmt.Sprintf("%s:%d", h.address, h.port),
)
if err != nil {
log.Fatalf("Failed to Listen: %v", err)
}
server := grpc.NewServer()
host.RegisterHostServer(server, h)
server.Serve(listen)
}
// Start implements the gRPC plugin host API and starts the requested plugin.
func (h *PluginHost) Start(ctx context.Context, in *host.StartRequest) (*host.StartResponse, error) {
for _, plugin := range h.processes {
if plugin.Name == in.Name {
err := plugin.Start(h.notifications)
if err != nil {
return nil, err
}
return &host.StartResponse{}, nil
}
}
return nil, fmt.Errorf("Unable to locate plugin %s.", in.Name)
}
// Stop implements the gRPC plugin host API and stops the requested plugin.
func (h *PluginHost) Stop(ctx context.Context, in *host.StopRequest) (*host.StopResponse, error) {
for _, plugin := range h.processes {
if plugin.Name == in.Name {
err := plugin.Stop()
if err != nil {
return nil, err
}
return &host.StopResponse{}, nil
}
}
return nil, fmt.Errorf("Unable to locate plugin %s.", in.Name)
}
// Status implements the gRPC plugin host API and gets status on all plugins.
func (h *PluginHost) Status(in *host.StatusRequest, stream host.Host_StatusServer) error {
for _, plugin := range h.processes {
var state = "Not Running"
if plugin.Status() {
state = "Running"
}
stream.Send(&host.StatusResponse{
Name: plugin.Name,
Status: state,
})
}
stream.Send(nil)
return nil
}