-
Notifications
You must be signed in to change notification settings - Fork 14
/
agent.go
85 lines (77 loc) · 1.88 KB
/
agent.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
package main
import (
"context"
"net"
"os"
"os/signal"
"github.com/crosbymichael/boss/agent"
"github.com/crosbymichael/boss/api/v1"
"github.com/crosbymichael/boss/config"
"github.com/crosbymichael/boss/system"
raven "github.com/getsentry/raven-go"
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"github.com/urfave/cli"
"google.golang.org/grpc"
"google.golang.org/grpc/health"
"google.golang.org/grpc/health/grpc_health_v1"
)
var agentCommand = cli.Command{
Name: "agent",
Usage: "run the boss agent",
Action: func(clix *cli.Context) error {
s := make(chan os.Signal, 32)
signal.Notify(s, os.Interrupt)
c, err := config.Load()
if err != nil {
return err
}
client, err := system.NewClient()
if err != nil {
return err
}
defer client.Close()
store, err := c.Store()
if err != nil {
return err
}
a, err := agent.New(c, client, store)
if err != nil {
return err
}
server := newServer()
v1.RegisterAgentServer(server, a)
go func() {
<-s
server.Stop()
}()
l, err := net.Listen("tcp", clix.GlobalString("agent"))
if err != nil {
return err
}
defer l.Close()
return server.Serve(l)
},
}
func newServer() *grpc.Server {
s := grpc.NewServer(
grpc.UnaryInterceptor(unary),
grpc.StreamInterceptor(stream),
)
hs := health.NewServer()
grpc_health_v1.RegisterHealthServer(s, hs)
return s
}
func unary(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
r, err := grpc_prometheus.UnaryServerInterceptor(ctx, req, info, handler)
if err != nil {
raven.CaptureError(err, nil)
}
return r, err
}
func stream(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
err := grpc_prometheus.StreamServerInterceptor(srv, ss, info, handler)
if err != nil {
raven.CaptureError(err, nil)
}
return err
}