Skip to content

Commit

Permalink
Update Run to RunE
Browse files Browse the repository at this point in the history
  • Loading branch information
joelsmith-2019 committed May 20, 2024
1 parent b0586fc commit 867507a
Showing 1 changed file with 21 additions and 27 deletions.
48 changes: 21 additions & 27 deletions cmd/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func Start(a *AppState) *cobra.Command {
PersistentPreRun: func(cmd *cobra.Command, _ []string) {
a.InitAppState()
},
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
logger := a.Logger
cfg := a.Config

Expand All @@ -49,16 +49,14 @@ func Start(a *AppState) *cobra.Command {

flushOnly, err := cmd.Flags().GetBool(flagFlushOnlyMode)
if err != nil {
logger.Error("Invalid flush only flag", "error", err)
os.Exit(1)
return fmt.Errorf("invalid flush only flag error=%e", err)
}

if flushInterval == 0 {
if flushOnly {
logger.Error("Flush only mode requires a flush interval")
os.Exit(1)
return fmt.Errorf("flush only mode requires a flush interval")
} else {
logger.Info("Flush interval not set. Use the --flush-interval flag to set a reoccurring flush")
logger.Error("Flush interval not set. Use the --flush-interval flag to set a reoccurring flush")
}
}

Expand All @@ -72,24 +70,21 @@ func Start(a *AppState) *cobra.Command {

port, err := cmd.Flags().GetInt16(flagMetricsPort)
if err != nil {
logger.Error("Invalid port", "error", err)
os.Exit(1)
return fmt.Errorf("invalid port error=%e", err)
}

metrics := relayer.InitPromMetrics(port)

for name, cfg := range cfg.Chains {
c, err := cfg.Chain(name)
if err != nil {
logger.Error("Error creating chain", "err: ", err)
os.Exit(1)
return fmt.Errorf("error creating chain error=%e", err)
}

logger = logger.With("name", c.Name(), "domain", c.Domain())

if err := c.InitializeClients(cmd.Context(), logger); err != nil {
logger.Error("Error initializing client", "err", err)
os.Exit(1)
return fmt.Errorf("error initializing client error=%e", err)
}

go c.TrackLatestBlockHeight(cmd.Context(), logger, metrics)
Expand All @@ -103,23 +98,20 @@ func Start(a *AppState) *cobra.Command {
break
}
if i == maxRetries-1 {
logger.Error("Unable to get height")
os.Exit(1)
return fmt.Errorf("unable to get height")
}
}

if err := c.InitializeBroadcaster(cmd.Context(), logger, sequenceMap); err != nil {
logger.Error("Error initializing broadcaster", "error", err)
os.Exit(1)
return fmt.Errorf("error initializing broadcaster error=%e", err)
}

go c.StartListener(cmd.Context(), logger, processingQueue, flushOnly, flushInterval)

go c.WalletBalanceMetric(cmd.Context(), a.Logger, metrics)

if _, ok := registeredDomains[c.Domain()]; ok {
logger.Error("Duplicate domain found", "domain", c.Domain(), "name:", c.Name())
os.Exit(1)
return fmt.Errorf("duplicate domain found domain=%d name=%s", c.Domain(), c.Name())
}

registeredDomains[c.Domain()] = c
Expand All @@ -130,17 +122,19 @@ func Start(a *AppState) *cobra.Command {
go StartProcessor(cmd.Context(), a, registeredDomains, processingQueue, sequenceMap, metrics)
}

defer func() {
for _, c := range registeredDomains {
logger.Info(fmt.Sprintf("%s: latest-block: %d last-flushed-block: %d", c.Name(), c.LatestBlock(), c.LastFlushedBlock()))
err := c.CloseClients()
if err != nil {
logger.Error("Error closing clients", "error", err)
}
// wait for context to be done
<-cmd.Context().Done()

// close clients & output latest block heights
for _, c := range registeredDomains {
logger.Info(fmt.Sprintf("%s: latest-block: %d last-flushed-block: %d", c.Name(), c.LatestBlock(), c.LastFlushedBlock()))
err := c.CloseClients()
if err != nil {
logger.Error("Error closing clients", "error", err)
}
}()
}

<-cmd.Context().Done()
return nil
},
}

Expand Down

0 comments on commit 867507a

Please sign in to comment.