-
Notifications
You must be signed in to change notification settings - Fork 42
/
core.go
164 lines (140 loc) · 4.1 KB
/
core.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"fmt"
"net"
"net/http"
_ "net/http/pprof" //nolint
"os"
"os/signal"
"syscall"
"testing"
"time"
"github.com/prometheus/client_golang/prometheus/promhttp"
zerolog "github.com/rs/zerolog/log"
"github.com/projecteru2/core/auth"
"github.com/projecteru2/core/cluster/calcium"
"github.com/projecteru2/core/engine/factory"
"github.com/projecteru2/core/log"
"github.com/projecteru2/core/metrics"
"github.com/projecteru2/core/rpc"
pb "github.com/projecteru2/core/rpc/gen"
"github.com/projecteru2/core/selfmon"
"github.com/projecteru2/core/utils"
"github.com/projecteru2/core/version"
cli "github.com/urfave/cli/v2"
_ "go.uber.org/automaxprocs"
"google.golang.org/grpc"
)
var (
configPath string
embeddedStorage bool
)
func serve(c *cli.Context) error {
config, err := utils.LoadConfig(configPath)
if err != nil {
zerolog.Fatal().Err(err).Send()
}
if err := log.SetupLog(c.Context, &config.Log, config.SentryDSN); err != nil {
zerolog.Fatal().Err(err).Send()
}
defer log.SentryDefer()
logger := log.WithFunc("main")
var t *testing.T
if embeddedStorage {
t = &testing.T{}
}
cluster, err := calcium.New(c.Context, config, t)
if err != nil {
logger.Error(c.Context, err)
return err
}
defer cluster.Finalizer()
// init engine cache and start engine cache checker
factory.InitEngineCache(c.Context, config, cluster.GetStore())
cluster.DisasterRecover(c.Context)
stop := make(chan struct{}, 1)
vibranium := rpc.New(cluster, config, stop)
s, err := net.Listen("tcp", config.Bind)
if err != nil {
logger.Error(c.Context, err)
return err
}
opts := []grpc.ServerOption{
grpc.MaxConcurrentStreams(uint32(config.GRPCConfig.MaxConcurrentStreams)),
grpc.MaxRecvMsgSize(config.GRPCConfig.MaxRecvMsgSize),
}
if config.Auth.Username != "" {
logger.Info(c.Context, "cluster auth enable.")
auth := auth.NewAuth(config.Auth)
opts = append(opts, grpc.StreamInterceptor(auth.StreamInterceptor))
opts = append(opts, grpc.UnaryInterceptor(auth.UnaryInterceptor))
logger.Infof(c.Context, "username %s password %s", config.Auth.Username, config.Auth.Password)
}
grpcServer := grpc.NewServer(opts...)
pb.RegisterCoreRPCServer(grpcServer, vibranium)
utils.SentryGo(func() {
if err := grpcServer.Serve(s); err != nil {
logger.Error(c.Context, err, "start grpc failed")
}
})
if config.Profile != "" {
http.Handle("/metrics", metrics.Client.ResourceMiddleware(cluster)(promhttp.Handler()))
utils.SentryGo(func() {
server := &http.Server{
Addr: config.Profile,
ReadHeaderTimeout: 3 * time.Second,
}
if err := server.ListenAndServe(); err != nil {
logger.Error(c.Context, err, "start http failed")
}
})
}
unregisterService, err := cluster.RegisterService(c.Context)
if err != nil {
logger.Error(c.Context, err, "failed to register service")
return err
}
logger.Info(c.Context, "cluster started successfully.")
// wait for unix signals and try to GracefulStop
ctx, cancel := signal.NotifyContext(c.Context, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
defer cancel()
// start node status checker
utils.SentryGo(func() {
selfmon.RunNodeStatusWatcher(ctx, config, cluster, t)
})
<-ctx.Done()
logger.Info(c.Context, "interrupt by signal")
close(stop)
unregisterService()
grpcServer.GracefulStop()
logger.Info(c.Context, "gRPC server gracefully stopped.")
logger.Info(c.Context, "check if cluster still have running tasks.")
vibranium.Wait()
logger.Info(c.Context, "cluster gracefully stopped.")
return nil
}
func main() {
cli.VersionPrinter = func(_ *cli.Context) {
fmt.Print(version.String())
}
app := cli.NewApp()
app.Name = version.NAME
app.Usage = "Run eru core"
app.Version = version.VERSION
app.Flags = []cli.Flag{
&cli.StringFlag{
Name: "config",
Value: "/etc/eru/core.yaml",
Usage: "config file path for core, in yaml",
Destination: &configPath,
EnvVars: []string{"ERU_CONFIG_PATH"},
},
&cli.BoolFlag{
Name: "embedded-storage",
Usage: "active embedded storage",
Destination: &embeddedStorage,
},
}
app.Action = serve
_ = app.Run(os.Args)
}