mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 03:16:45 +01:00
WIP
This commit is contained in:
128
cli/node/main.go
128
cli/node/main.go
@@ -107,6 +107,29 @@ func cmdWipeSQL(c *cli.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func waitSigInt() {
|
||||
stopCh := make(chan interface{})
|
||||
|
||||
// catch ^C to send the stop signal
|
||||
ossig := make(chan os.Signal, 1)
|
||||
signal.Notify(ossig, os.Interrupt)
|
||||
const forceStopCount = 3
|
||||
go func() {
|
||||
n := 0
|
||||
for sig := range ossig {
|
||||
if sig == os.Interrupt {
|
||||
log.Info("Received Interrupt Signal")
|
||||
stopCh <- nil
|
||||
n++
|
||||
if n == forceStopCount {
|
||||
log.Fatalf("Received %v Interrupt Signals", forceStopCount)
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
<-stopCh
|
||||
}
|
||||
|
||||
func cmdRun(c *cli.Context) error {
|
||||
cfg, err := parseCli(c)
|
||||
if err != nil {
|
||||
@@ -117,69 +140,24 @@ func cmdRun(c *cli.Context) error {
|
||||
return tracerr.Wrap(fmt.Errorf("error starting node: %w", err))
|
||||
}
|
||||
node.Start()
|
||||
|
||||
stopCh := make(chan interface{})
|
||||
|
||||
// catch ^C to send the stop signal
|
||||
ossig := make(chan os.Signal, 1)
|
||||
signal.Notify(ossig, os.Interrupt)
|
||||
const forceStopCount = 3
|
||||
go func() {
|
||||
n := 0
|
||||
for sig := range ossig {
|
||||
if sig == os.Interrupt {
|
||||
log.Info("Received Interrupt Signal")
|
||||
stopCh <- nil
|
||||
n++
|
||||
if n == forceStopCount {
|
||||
log.Fatalf("Received %v Interrupt Signals", forceStopCount)
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
<-stopCh
|
||||
waitSigInt()
|
||||
node.Stop()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func cmdServeAPI(c *cli.Context) error {
|
||||
cfgPath := c.String(flagCfg)
|
||||
cfg, err := config.LoadAPIServer(cfgPath)
|
||||
cfg, err := parseCliAPIServer(c)
|
||||
if err != nil {
|
||||
if err := cli.ShowAppHelp(c); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return tracerr.Wrap(fmt.Errorf("error parsing flags and config: %w", err))
|
||||
}
|
||||
|
||||
node, err := node.NewNode(cfg.mode, cfg.node)
|
||||
srv, err := node.NewAPIServer(cfg.mode, cfg.server)
|
||||
if err != nil {
|
||||
return tracerr.Wrap(fmt.Errorf("error starting node: %w", err))
|
||||
return tracerr.Wrap(fmt.Errorf("error starting api server: %w", err))
|
||||
}
|
||||
node.Start()
|
||||
|
||||
stopCh := make(chan interface{})
|
||||
|
||||
// catch ^C to send the stop signal
|
||||
ossig := make(chan os.Signal, 1)
|
||||
signal.Notify(ossig, os.Interrupt)
|
||||
const forceStopCount = 3
|
||||
go func() {
|
||||
n := 0
|
||||
for sig := range ossig {
|
||||
if sig == os.Interrupt {
|
||||
log.Info("Received Interrupt Signal")
|
||||
stopCh <- nil
|
||||
n++
|
||||
if n == forceStopCount {
|
||||
log.Fatalf("Received %v Interrupt Signals", forceStopCount)
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
<-stopCh
|
||||
node.Stop()
|
||||
srv.Start()
|
||||
waitSigInt()
|
||||
srv.Stop()
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -270,13 +248,55 @@ func getConfig(c *cli.Context) (*Config, error) {
|
||||
switch mode {
|
||||
case modeSync:
|
||||
cfg.mode = node.ModeSynchronizer
|
||||
cfg.node, err = config.LoadNode(nodeCfgPath)
|
||||
cfg.node, err = config.LoadNode(nodeCfgPath, false)
|
||||
if err != nil {
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
case modeCoord:
|
||||
cfg.mode = node.ModeCoordinator
|
||||
cfg.node, err = config.LoadCoordinator(nodeCfgPath)
|
||||
cfg.node, err = config.LoadNode(nodeCfgPath, true)
|
||||
if err != nil {
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
default:
|
||||
return nil, tracerr.Wrap(fmt.Errorf("invalid mode \"%v\"", mode))
|
||||
}
|
||||
|
||||
return &cfg, nil
|
||||
}
|
||||
|
||||
// ConfigAPIServer is the configuration of the api server execution
|
||||
type ConfigAPIServer struct {
|
||||
mode node.Mode
|
||||
server *config.APIServer
|
||||
}
|
||||
|
||||
func parseCliAPIServer(c *cli.Context) (*ConfigAPIServer, error) {
|
||||
cfg, err := getConfigAPIServer(c)
|
||||
if err != nil {
|
||||
if err := cli.ShowAppHelp(c); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
func getConfigAPIServer(c *cli.Context) (*ConfigAPIServer, error) {
|
||||
var cfg ConfigAPIServer
|
||||
mode := c.String(flagMode)
|
||||
nodeCfgPath := c.String(flagCfg)
|
||||
var err error
|
||||
switch mode {
|
||||
case modeSync:
|
||||
cfg.mode = node.ModeSynchronizer
|
||||
cfg.server, err = config.LoadAPIServer(nodeCfgPath, false)
|
||||
if err != nil {
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
case modeCoord:
|
||||
cfg.mode = node.ModeCoordinator
|
||||
cfg.server, err = config.LoadAPIServer(nodeCfgPath, true)
|
||||
if err != nil {
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user