Skip to content

Commit eb05840

Browse files
Merge pull request #675 from onflow/janez/switch-to-component
Cleanup run cmd
2 parents 01bb8e7 + 537379c commit eb05840

File tree

4 files changed

+42
-18
lines changed

4 files changed

+42
-18
lines changed

bootstrap/bootstrap.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"math"
88
"time"
99

10+
"github.com/onflow/flow-go/module/component"
11+
1012
pebbleDB "github.com/cockroachdb/pebble"
1113

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

582584
// mark ready
583-
close(ready)
585+
ready()
584586

585587
// if context is canceled start shutdown
586588
<-ctx.Done()

cmd/run/cmd.go

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package run
33
import (
44
"context"
55
"encoding/json"
6+
"errors"
67
"fmt"
78
"math/big"
89
"os"
@@ -28,27 +29,38 @@ import (
2829
var Cmd = &cobra.Command{
2930
Use: "run",
3031
Short: "Runs the EVM Gateway Node",
31-
Run: func(command *cobra.Command, _ []string) {
32+
RunE: func(command *cobra.Command, _ []string) error {
33+
34+
ctx, cancel := context.WithCancel(command.Context())
35+
defer cancel()
36+
3237
// create multi-key account
38+
// TODO(JanezP): move to separate command
3339
if _, exists := os.LookupEnv("MULTIKEY_MODE"); exists {
3440
bootstrap.RunCreateMultiKeyAccount()
35-
return
41+
return nil
3642
}
3743

3844
if err := parseConfigFromFlags(); err != nil {
39-
log.Err(err).Msg("failed to parse flags")
40-
os.Exit(1)
45+
return fmt.Errorf("failed to parse flags: %w", err)
4146
}
4247

43-
ctx, cancel := context.WithCancel(command.Context())
4448
done := make(chan struct{})
4549
ready := make(chan struct{})
4650
go func() {
4751
defer close(done)
48-
err := bootstrap.Run(ctx, cfg, ready)
49-
if err != nil {
50-
log.Err(err).Msg("failed to run bootstrap")
51-
cancel()
52+
// In case an error happens before ready is called we need to close the ready channel
53+
defer close(ready)
54+
55+
err := bootstrap.Run(
56+
ctx,
57+
cfg,
58+
func() {
59+
close(ready)
60+
},
61+
)
62+
if err != nil && !errors.Is(err, context.Canceled) {
63+
log.Err(err).Msg("Gateway runtime error")
5264
}
5365
}()
5466

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

72+
// wait for gateway to exit or for a shutdown signal
6073
select {
6174
case <-osSig:
6275
log.Info().Msg("OS Signal to shutdown received, shutting down")
6376
cancel()
6477
case <-done:
6578
log.Info().Msg("done, shutting down")
66-
cancel()
6779
}
6880

69-
log.Info().Msg("OS Signal to shutdown received, shutting down")
70-
cancel()
81+
// Wait for the gateway to completely stop
82+
<-done
83+
84+
return nil
7185
},
7286
}
7387

tests/helpers.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,9 @@ func servicesSetup(t *testing.T) (emulator.Emulator, func()) {
161161

162162
bootstrapDone := make(chan struct{})
163163
go func() {
164-
err = bootstrap.Run(ctx, cfg, bootstrapDone)
164+
err = bootstrap.Run(ctx, cfg, func() {
165+
close(bootstrapDone)
166+
})
165167
require.NoError(t, err)
166168
}()
167169

tests/integration_test.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ func Test_ConcurrentTransactionSubmission(t *testing.T) {
8383

8484
ready := make(chan struct{})
8585
go func() {
86-
err := bootstrap.Run(ctx, cfg, ready)
86+
err := bootstrap.Run(ctx, cfg, func() {
87+
close(ready)
88+
})
8789
require.NoError(t, err)
8890
}()
8991

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

182184
ready := make(chan struct{})
183185
go func() {
184-
err := bootstrap.Run(ctx, cfg, ready)
186+
err := bootstrap.Run(ctx, cfg, func() {
187+
close(ready)
188+
})
185189
require.NoError(t, err)
186190
}()
187191

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

289293
ready := make(chan struct{})
290294
go func() {
291-
err := bootstrap.Run(ctx, cfg, ready)
295+
err := bootstrap.Run(ctx, cfg, func() {
296+
close(ready)
297+
})
292298
require.NoError(t, err)
293299
}()
294300

0 commit comments

Comments
 (0)