From e65d156080502c2d2c3daf4553020d723818c290 Mon Sep 17 00:00:00 2001 From: yakud Date: Thu, 23 May 2024 17:11:57 +0300 Subject: [PATCH] Restart indexer service after fail --- cmd/galacticad-merkle/cmd/indexer/start.go | 19 +++++++++++++++++-- pkg/indexer/application.go | 8 +++++++- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/cmd/galacticad-merkle/cmd/indexer/start.go b/cmd/galacticad-merkle/cmd/indexer/start.go index 476374c..d659aaa 100644 --- a/cmd/galacticad-merkle/cmd/indexer/start.go +++ b/cmd/galacticad-merkle/cmd/indexer/start.go @@ -99,8 +99,23 @@ func CreateStartCmd() *cobra.Command { IndexerConfig: getIndexConfig(), } - if err := pkgindexer.StartApplication(cmd.Context(), appConfig, logger); err != nil { - return fmt.Errorf("start indexer server: %w", err) + for { + if err := pkgindexer.StartApplication(cmd.Context(), appConfig, logger); err != nil { + logger.Error("service produced an error", "error", err) + } + + needStop := false + select { + case <-cmd.Context().Done(): + needStop = true + default: + } + + if !needStop { + logger.Error("restarting service") + } else { + break + } } logger.Info("gracefully stopped indexer server") diff --git a/pkg/indexer/application.go b/pkg/indexer/application.go index edb27fd..1ae52ef 100644 --- a/pkg/indexer/application.go +++ b/pkg/indexer/application.go @@ -150,8 +150,14 @@ func StartApplication(ctx context.Context, config ApplicationConfig, logger log. func (app *Application) Init(ctx context.Context) error { app.logger.Info("connecting to EVM RPC", "evm_rpc", app.config.EvmRpc) if err := backoff.Retry(func() error { + ctxWithTimeout, cancel := context.WithTimeout(ctx, time.Second*5) + defer cancel() + var err error - app.ethereumClient, err = ethclient.DialContext(ctx, app.config.EvmRpc) + app.ethereumClient, err = ethclient.DialContext(ctxWithTimeout, app.config.EvmRpc) + if err != nil { + app.logger.Error("ethereum node dial", "error", err) + } return err }, backoff.NewExponentialBackOff()); err != nil {