[#72] Shutdown inner ring app if RPC node has been terminated

Adopt error channel from Listener interface.

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2020-10-13 18:40:35 +03:00 committed by Alex Vanin
parent ccbb9ce6ab
commit dd48666357
2 changed files with 27 additions and 6 deletions

View file

@ -10,6 +10,7 @@ import (
"github.com/nspcc-dev/neofs-node/pkg/util/grace" "github.com/nspcc-dev/neofs-node/pkg/util/grace"
"github.com/nspcc-dev/neofs-node/pkg/util/logger" "github.com/nspcc-dev/neofs-node/pkg/util/logger"
"github.com/nspcc-dev/neofs-node/pkg/util/profiler" "github.com/nspcc-dev/neofs-node/pkg/util/profiler"
"go.uber.org/zap"
) )
const ( const (
@ -44,6 +45,7 @@ func main() {
exitErr(err) exitErr(err)
ctx := grace.NewGracefulContext(log) ctx := grace.NewGracefulContext(log)
intErr := make(chan error) // internal inner ring errors
pprof := profiler.NewProfiler(log, cfg) pprof := profiler.NewProfiler(log, cfg)
prometheus := profiler.NewMetrics(log, cfg) prometheus := profiler.NewMetrics(log, cfg)
@ -66,15 +68,19 @@ func main() {
} }
// start inner ring // start inner ring
err = innerRing.Start(ctx) err = innerRing.Start(ctx, intErr)
if err != nil { if err != nil {
exitErr(err) exitErr(err)
} }
log.Info("application started") log.Info("application started")
// todo: select ctx.Done or exported error channel select {
<-ctx.Done() case <-ctx.Done():
case err := <-intErr:
// todo: restart application instead of shutdown
log.Info("internal error", zap.String("msg", err.Error()))
}
innerRing.Stop() innerRing.Stop()

View file

@ -75,7 +75,7 @@ const (
) )
// Start runs all event providers. // Start runs all event providers.
func (s *Server) Start(ctx context.Context) error { func (s *Server) Start(ctx context.Context, intError chan<- error) error {
err := s.initConfigFromBlockchain() err := s.initConfigFromBlockchain()
if err != nil { if err != nil {
return err return err
@ -83,8 +83,23 @@ func (s *Server) Start(ctx context.Context) error {
s.localTimers.Start(ctx) // local timers start ticking s.localTimers.Start(ctx) // local timers start ticking
go s.morphListener.Listen(ctx) // listen for neo:morph events morphErr := make(chan error)
go s.mainnetListener.Listen(ctx) // listen for neo:mainnet events mainnnetErr := make(chan error)
// anonymous function to multiplex error channels
go func() {
select {
case <-ctx.Done():
return
case err := <-morphErr:
intError <- errors.Wrap(err, "sidechain")
case err := <-mainnnetErr:
intError <- errors.Wrap(err, "mainnet")
}
}()
go s.morphListener.ListenWithError(ctx, morphErr) // listen for neo:morph events
go s.mainnetListener.ListenWithError(ctx, mainnnetErr) // listen for neo:mainnet events
return nil return nil
} }