This repository has been archived by the owner on Jul 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
141 lines (123 loc) · 3.62 KB
/
main.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
package main
import (
"flag"
"os"
"os/signal"
"syscall"
"github.com/ConsenSys/quorum-hibernate/config"
"github.com/ConsenSys/quorum-hibernate/log"
"github.com/ConsenSys/quorum-hibernate/node"
"github.com/ConsenSys/quorum-hibernate/proxy"
"github.com/ConsenSys/quorum-hibernate/rpc"
"github.com/sirupsen/logrus"
)
type NodeHibernatorApp struct {
node *node.NodeControl
proxyServers []proxy.Proxy
rpcService *rpc.RPCService
}
var nhApp = NodeHibernatorApp{}
func main() {
var verbosity int
flag.IntVar(&verbosity, "verbosity", log.InfoLevel, "logging verbosity")
// Read config file path
var configFile string
flag.StringVar(&configFile, "config", "config.toml", "config file")
flag.Parse()
logrus.SetLevel(logrus.Level(verbosity + 2))
log.Debug("main - config file", "path", configFile)
nodeConfig, err := readNodeConfigFromFile(configFile)
if err != nil {
log.Error("unable to load config", "err", err)
return
}
log.Debug("main - node config", "basic", nodeConfig.BasicConfig, "nhs", nodeConfig.Peers)
rpcBackendErrCh := make(chan error)
proxyBackendErrCh := make(chan error)
if !Start(nodeConfig, err, proxyBackendErrCh, rpcBackendErrCh) {
return
}
waitForShutdown(rpcBackendErrCh, proxyBackendErrCh)
}
func Start(nodeConfig *config.Node, err error, proxyBackendErrCh chan error, rpcBackendErrCh chan error) bool {
nhApp.node = node.NewNodeControl(nodeConfig)
if nhApp.proxyServers, err = proxy.MakeProxyServices(nhApp.node, proxyBackendErrCh); err != nil {
log.Error("Start - creating proxies failed", "err", err)
return false
}
nhApp.rpcService = rpc.NewRPCService(nhApp.node, nhApp.node.GetRPCConfig(), rpcBackendErrCh)
// start node service
nhApp.node.Start()
// start proxies
for _, p := range nhApp.proxyServers {
p.Start()
}
// start rpc server
if err := nhApp.rpcService.Start(); err != nil {
log.Info("Start - rpc server failed", "err", err)
return false
}
return true
}
func waitForShutdown(rpcBackendErrCh chan error, proxyBackendErrCh chan error) {
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, syscall.SIGINT, syscall.SIGTERM)
defer signal.Stop(sigc)
for {
select {
case err := <-sigc:
log.Error("waitForShutdown - Received interrupt signal, shutting down...", "err", err)
Shutdown()
return
case err := <-rpcBackendErrCh:
log.Error("waitForShutdown - RPC backend failed, shutting down...", "err", err)
Shutdown()
return
case err := <-proxyBackendErrCh:
log.Error("waitForShutdown - Proxy backend failed, shutting down...", "err", err)
Shutdown()
return
}
}
}
func readNodeConfigFromFile(configFile string) (*config.Node, error) {
nhReader, err := config.NewNodeHibernatorReader(configFile)
if err != nil {
return nil, err
}
log.Debug("readNodeConfigFromFile - loading node hibernator config file")
nhConfig, err := nhReader.Read()
if err != nil {
return nil, err
}
log.Debug("readNodeConfigFromFile - validating node hibernator config file")
// validate config rules
if err = nhConfig.IsValid(); err != nil {
return nil, err
}
log.Debug("readNodeConfigFromFile - loading peers config file")
peersReader, err := config.NewPeersReader(nhConfig.PeersConfigFile)
if err != nil {
return nil, err
}
peersConfig, err := peersReader.Read()
if err != nil {
return nil, err
}
log.Debug("readNodeConfigFromFile - validating peers config file")
if err := peersConfig.IsValid(); err != nil {
return nil, err
}
return &config.Node{
BasicConfig: &nhConfig,
Peers: peersConfig,
}, nil
}
func Shutdown() {
for _, p := range nhApp.proxyServers {
p.Stop()
}
nhApp.rpcService.Stop()
nhApp.node.Stop()
log.ErrWriter.Close()
}