Skip to content
This repository has been archived by the owner on May 11, 2024. It is now read-only.

Commit

Permalink
feat(cmd): revert SubcommandApplication context changes (#701)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidtaikocha authored Apr 9, 2024
1 parent a598847 commit 985f030
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 24 deletions.
15 changes: 10 additions & 5 deletions cmd/utils/sub_command.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package utils

import (
"context"
"os"
"os/signal"
"syscall"
Expand All @@ -13,17 +14,20 @@ import (
)

type SubcommandApplication interface {
InitFromCli(*cli.Context) error
InitFromCli(context.Context, *cli.Context) error
Name() string
Start() error
Close()
Close(context.Context)
}

func SubcommandAction(app SubcommandApplication) cli.ActionFunc {
return func(c *cli.Context) error {
logger.InitLogger(c)

if err := app.InitFromCli(c); err != nil {
ctx, ctxClose := context.WithCancel(context.Background())
defer ctxClose()

if err := app.InitFromCli(ctx, c); err != nil {
return err
}

Expand All @@ -34,13 +38,14 @@ func SubcommandAction(app SubcommandApplication) cli.ActionFunc {
return err
}

if err := metrics.Serve(c); err != nil {
if err := metrics.Serve(ctx, c); err != nil {
log.Error("Starting metrics server error", "error", err)
return err
}

defer func() {
app.Close()
ctxClose()
app.Close(ctx)
log.Info("Application stopped", "name", app.Name())
}()

Expand Down
3 changes: 2 additions & 1 deletion driver/config_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package driver

import (
"context"
"os"
"time"

Expand Down Expand Up @@ -37,7 +38,7 @@ func (s *DriverTestSuite) TestNewConfigFromCliContext() {
s.NotEmpty(c.JwtSecret)
s.True(c.P2PSyncVerifiedBlocks)
s.Equal(l2CheckPoint, c.L2CheckPoint)
s.NotNil(new(Driver).InitFromCli(ctx))
s.NotNil(new(Driver).InitFromCli(context.Background(), ctx))

return err
}
Expand Down
6 changes: 3 additions & 3 deletions driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ type Driver struct {
}

// InitFromCli initializes the given driver instance based on the command line flags.
func (d *Driver) InitFromCli(c *cli.Context) error {
func (d *Driver) InitFromCli(ctx context.Context, c *cli.Context) error {
cfg, err := NewConfigFromCliContext(c)
if err != nil {
return err
}

return d.InitFromConfig(c.Context, cfg)
return d.InitFromConfig(ctx, cfg)
}

// InitFromConfig initializes the driver instance based on the given configurations.
Expand Down Expand Up @@ -100,7 +100,7 @@ func (d *Driver) Start() error {
}

// Close closes the driver instance.
func (d *Driver) Close() {
func (d *Driver) Close(_ context.Context) {
d.l1HeadSub.Unsubscribe()
d.state.Close()
d.wg.Wait()
Expand Down
2 changes: 1 addition & 1 deletion driver/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ func (s *DriverTestSuite) TestDoSyncNoNewL2Blocks() {
func (s *DriverTestSuite) TestStartClose() {
s.Nil(s.d.Start())
s.cancel()
s.d.Close()
s.d.Close(s.d.ctx)
}

func (s *DriverTestSuite) TestL1Current() {
Expand Down
5 changes: 3 additions & 2 deletions internal/metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package metrics

import (
"context"
"net"
"net/http"
"strconv"
Expand Down Expand Up @@ -55,7 +56,7 @@ var (

// Serve starts the metrics server on the given address, will be closed when the given
// context is cancelled.
func Serve(c *cli.Context) error {
func Serve(ctx context.Context, c *cli.Context) error {
if !c.Bool(flags.MetricsEnabled.Name) {
return nil
}
Expand All @@ -72,7 +73,7 @@ func Serve(c *cli.Context) error {
}

go func() {
<-c.Context.Done()
<-ctx.Done()
if err := server.Close(); err != nil {
log.Error("Failed to close metrics server", "error", err)
}
Expand Down
7 changes: 4 additions & 3 deletions proposer/config_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package proposer

import (
"context"
"fmt"
"os"
"strings"
Expand Down Expand Up @@ -32,8 +33,8 @@ func (s *ProposerTestSuite) TestNewConfigFromCliContext() {

app := s.SetupApp()

app.Action = func(ctx *cli.Context) error {
c, err := NewConfigFromCliContext(ctx)
app.Action = func(cliCtx *cli.Context) error {
c, err := NewConfigFromCliContext(cliCtx)
s.Nil(err)
s.Equal(l1Endpoint, c.L1Endpoint)
s.Equal(l2Endpoint, c.L2Endpoint)
Expand All @@ -56,7 +57,7 @@ func (s *ProposerTestSuite) TestNewConfigFromCliContext() {
s.Equal(c.ProverEndpoints[i].String(), e)
}

s.Nil(new(Proposer).InitFromCli(ctx))
s.Nil(new(Proposer).InitFromCli(context.Background(), cliCtx))
return nil
}

Expand Down
6 changes: 3 additions & 3 deletions proposer/proposer.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ type Proposer struct {
}

// InitFromCli New initializes the given proposer instance based on the command line flags.
func (p *Proposer) InitFromCli(c *cli.Context) error {
func (p *Proposer) InitFromCli(ctx context.Context, c *cli.Context) error {
cfg, err := NewConfigFromCliContext(c)
if err != nil {
return err
}

return p.InitFromConfig(c.Context, cfg)
return p.InitFromConfig(ctx, cfg)
}

// InitFromConfig initializes the proposer instance based on the given configurations.
Expand Down Expand Up @@ -192,7 +192,7 @@ func (p *Proposer) eventLoop() {
}

// Close closes the proposer instance.
func (p *Proposer) Close() {
func (p *Proposer) Close(_ context.Context) {
p.wg.Wait()
}

Expand Down
2 changes: 1 addition & 1 deletion proposer/proposer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ func (s *ProposerTestSuite) TestUpdateProposingTicker() {
func (s *ProposerTestSuite) TestStartClose() {
s.Nil(s.p.Start())
s.cancel()
s.NotPanics(func() { s.p.Close() })
s.NotPanics(func() { s.p.Close(s.p.ctx) })
}

func TestProposerTestSuite(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion prover/config_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package prover

import (
"context"
"fmt"
"os"
"time"
Expand Down Expand Up @@ -52,7 +53,7 @@ func (s *ProverTestSuite) TestNewConfigFromCliContextGuardianProver() {
s.Equal(uint64(minTierFee), c.MinSgxTierFee.Uint64())
s.Equal(c.L1NodeVersion, l1NodeVersion)
s.Equal(c.L2NodeVersion, l2NodeVersion)
s.Nil(new(Prover).InitFromCli(ctx))
s.Nil(new(Prover).InitFromCli(context.Background(), ctx))
s.True(c.ProveUnassignedBlocks)
s.Equal(uint64(100), c.MaxProposedIn)
s.Equal(os.Getenv("ASSIGNMENT_HOOK_ADDRESS"), c.AssignmentHookAddress.String())
Expand Down
8 changes: 4 additions & 4 deletions prover/prover.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ type Prover struct {
}

// InitFromCli initializes the given prover instance based on the command line flags.
func (p *Prover) InitFromCli(c *cli.Context) error {
func (p *Prover) InitFromCli(ctx context.Context, c *cli.Context) error {
cfg, err := NewConfigFromCliContext(c)
if err != nil {
return err
}

return InitFromConfig(c.Context, p, cfg)
return InitFromConfig(ctx, p, cfg)
}

// InitFromConfig initializes the prover instance based on the given configurations.
Expand Down Expand Up @@ -313,8 +313,8 @@ func (p *Prover) eventLoop() {
}

// Close closes the prover instance.
func (p *Prover) Close() {
if err := p.server.Shutdown(p.ctx); err != nil {
func (p *Prover) Close(ctx context.Context) {
if err := p.server.Shutdown(ctx); err != nil {
log.Error("Failed to shut down prover server", "error", err)
}
p.wg.Wait()
Expand Down

0 comments on commit 985f030

Please sign in to comment.