Skip to content

Commit

Permalink
Merge pull request #675 from onflow/janez/switch-to-component
Browse files Browse the repository at this point in the history
Cleanup run cmd
  • Loading branch information
janezpodhostnik authored Nov 26, 2024
2 parents 01bb8e7 + 537379c commit eb05840
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 18 deletions.
6 changes: 4 additions & 2 deletions bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"math"
"time"

"github.com/onflow/flow-go/module/component"

pebbleDB "github.com/cockroachdb/pebble"

"github.com/onflow/flow-go-sdk/access"
Expand Down Expand Up @@ -557,7 +559,7 @@ func setupStorage(
// Run will run complete bootstrap of the EVM gateway with all the engines.
// Run is a blocking call, but it does signal readiness of the service
// through a channel provided as an argument.
func Run(ctx context.Context, cfg *config.Config, ready chan struct{}) error {
func Run(ctx context.Context, cfg *config.Config, ready component.ReadyFunc) error {
boot, err := New(cfg)
if err != nil {
return err
Expand All @@ -580,7 +582,7 @@ func Run(ctx context.Context, cfg *config.Config, ready chan struct{}) error {
}

// mark ready
close(ready)
ready()

// if context is canceled start shutdown
<-ctx.Done()
Expand Down
38 changes: 26 additions & 12 deletions cmd/run/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package run
import (
"context"
"encoding/json"
"errors"
"fmt"
"math/big"
"os"
Expand All @@ -28,27 +29,38 @@ import (
var Cmd = &cobra.Command{
Use: "run",
Short: "Runs the EVM Gateway Node",
Run: func(command *cobra.Command, _ []string) {
RunE: func(command *cobra.Command, _ []string) error {

ctx, cancel := context.WithCancel(command.Context())
defer cancel()

// create multi-key account
// TODO(JanezP): move to separate command
if _, exists := os.LookupEnv("MULTIKEY_MODE"); exists {
bootstrap.RunCreateMultiKeyAccount()
return
return nil
}

if err := parseConfigFromFlags(); err != nil {
log.Err(err).Msg("failed to parse flags")
os.Exit(1)
return fmt.Errorf("failed to parse flags: %w", err)
}

ctx, cancel := context.WithCancel(command.Context())
done := make(chan struct{})
ready := make(chan struct{})
go func() {
defer close(done)
err := bootstrap.Run(ctx, cfg, ready)
if err != nil {
log.Err(err).Msg("failed to run bootstrap")
cancel()
// In case an error happens before ready is called we need to close the ready channel
defer close(ready)

err := bootstrap.Run(
ctx,
cfg,
func() {
close(ready)
},
)
if err != nil && !errors.Is(err, context.Canceled) {
log.Err(err).Msg("Gateway runtime error")
}
}()

Expand All @@ -57,17 +69,19 @@ var Cmd = &cobra.Command{
osSig := make(chan os.Signal, 1)
signal.Notify(osSig, syscall.SIGINT, syscall.SIGTERM)

// wait for gateway to exit or for a shutdown signal
select {
case <-osSig:
log.Info().Msg("OS Signal to shutdown received, shutting down")
cancel()
case <-done:
log.Info().Msg("done, shutting down")
cancel()
}

log.Info().Msg("OS Signal to shutdown received, shutting down")
cancel()
// Wait for the gateway to completely stop
<-done

return nil
},
}

Expand Down
4 changes: 3 additions & 1 deletion tests/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,9 @@ func servicesSetup(t *testing.T) (emulator.Emulator, func()) {

bootstrapDone := make(chan struct{})
go func() {
err = bootstrap.Run(ctx, cfg, bootstrapDone)
err = bootstrap.Run(ctx, cfg, func() {
close(bootstrapDone)
})
require.NoError(t, err)
}()

Expand Down
12 changes: 9 additions & 3 deletions tests/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ func Test_ConcurrentTransactionSubmission(t *testing.T) {

ready := make(chan struct{})
go func() {
err := bootstrap.Run(ctx, cfg, ready)
err := bootstrap.Run(ctx, cfg, func() {
close(ready)
})
require.NoError(t, err)
}()

Expand Down Expand Up @@ -181,7 +183,9 @@ func Test_EthClientTest(t *testing.T) {

ready := make(chan struct{})
go func() {
err := bootstrap.Run(ctx, cfg, ready)
err := bootstrap.Run(ctx, cfg, func() {
close(ready)
})
require.NoError(t, err)
}()

Expand Down Expand Up @@ -288,7 +292,9 @@ func Test_CloudKMSConcurrentTransactionSubmission(t *testing.T) {

ready := make(chan struct{})
go func() {
err := bootstrap.Run(ctx, cfg, ready)
err := bootstrap.Run(ctx, cfg, func() {
close(ready)
})
require.NoError(t, err)
}()

Expand Down

0 comments on commit eb05840

Please sign in to comment.