Skip to content

Commit

Permalink
Merge pull request #8 from Galactica-corp/feature/indexer-reconnect
Browse files Browse the repository at this point in the history
Restart indexer service after fail
  • Loading branch information
yakud authored May 23, 2024
2 parents 3968cb5 + e65d156 commit d7e7b24
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
19 changes: 17 additions & 2 deletions cmd/galacticad-merkle/cmd/indexer/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
8 changes: 7 additions & 1 deletion pkg/indexer/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit d7e7b24

Please sign in to comment.