Skip to content

Commit

Permalink
dont store context on sttruct
Browse files Browse the repository at this point in the history
  • Loading branch information
steezeburger committed Apr 2, 2024
1 parent 13c33b2 commit 790f0c3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
10 changes: 4 additions & 6 deletions cmd/devtools/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ func runall(cmd *cobra.Command, args []string) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()

log.WithField("ctx", ctx).Info("Running all services")

homePath, err := os.UserHomeDir()
if err != nil {
log.WithError(err).Error("Error getting home dir")
Expand Down Expand Up @@ -82,22 +80,22 @@ func runall(cmd *cobra.Command, args []string) {
// shouldStart acts as a control channel to start this first process
shouldStart := make(chan bool)
close(shouldStart)
err = seqRunner.Start(shouldStart)
err = seqRunner.Start(ctx, shouldStart)
if err != nil {
log.WithError(err).Error("Error running sequencer")
cancel()
}
err = cometRunner.Start(seqRunner.GetDidStart())
err = cometRunner.Start(ctx, seqRunner.GetDidStart())
if err != nil {
log.WithError(err).Error("Error running cometbft")
cancel()
}
err = compRunner.Start(cometRunner.GetDidStart())
err = compRunner.Start(ctx, cometRunner.GetDidStart())
if err != nil {
log.WithError(err).Error("Error running composer")
cancel()
}
err = condRunner.Start(compRunner.GetDidStart())
err = condRunner.Start(ctx, compRunner.GetDidStart())
if err != nil {
log.WithError(err).Error("Error running conductor")
cancel()
Expand Down
21 changes: 10 additions & 11 deletions internal/processrunner/processrunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

// ProcessRunner is an interface that represents a process to be run.
type ProcessRunner interface {
Start(depStarted <-chan bool) error
Start(ctx context.Context, depStarted <-chan bool) error
Stop()
GetDidStart() <-chan bool
GetTitle() string
Expand All @@ -36,8 +36,6 @@ type processRunner struct {
didStart chan bool
stdout io.ReadCloser
stderr io.ReadCloser
ctx context.Context
cancel context.CancelFunc
outputBuf *bytes.Buffer
}

Expand All @@ -51,31 +49,30 @@ type NewProcessRunnerOpts struct {
// NewProcessRunner creates a new ProcessRunner.
// It creates a new exec.Cmd with the given binPath and args, and sets the environment.
func NewProcessRunner(ctx context.Context, opts NewProcessRunnerOpts) ProcessRunner {
ctx, cancel := context.WithCancel(ctx)

cmd := exec.CommandContext(ctx, opts.BinPath, opts.Args...)
cmd.Env = opts.Env
return &processRunner{
cmd: cmd,
title: opts.Title,
didStart: make(chan bool),
outputBuf: new(bytes.Buffer),
ctx: ctx,
cancel: cancel,
exitStatusStringLock: &sync.Mutex{},
}
}

// Start starts the process and returns the ProcessRunner and an error.
// It takes a channel that's closed when the dependency starts.
// This allows us to control the order of process startup.
func (pr *processRunner) Start(depStarted <-chan bool) error {
func (pr *processRunner) Start(ctx context.Context, depStarted <-chan bool) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()

select {
case <-depStarted:
// continue if the dependency has started.
case <-pr.ctx.Done():
case <-ctx.Done():
log.Info("Context cancelled before starting process", pr.title)
return pr.ctx.Err()
return ctx.Err()
}

// get stdout and stderr
Expand Down Expand Up @@ -114,23 +111,25 @@ func (pr *processRunner) Start(depStarted <-chan bool) error {
err = fmt.Errorf("process exited with error: %w", err)
log.Error(err)
pr.SetExitStatusString(err.Error())
cancel()
} else {
s := fmt.Sprintf("process exited cleanly")
log.Infof(s)
pr.SetExitStatusString(s)
cancel()
}
}()

return nil
}

// SetExitStatusString sets the exit status string of the process.
func (pr *processRunner) SetExitStatusString(status string) {
if status != "" {
pr.exitStatusStringLock.Lock()
defer pr.exitStatusStringLock.Unlock()
pr.exitStatusString = status
}
pr.cancel()
}

// Stop stops the process.
Expand Down
4 changes: 3 additions & 1 deletion internal/testutils/mocks.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package testutils

import (
"context"

"github.com/stretchr/testify/mock"
)

type MockProcessRunner struct {
mock.Mock
}

func (m *MockProcessRunner) Start(depStarted <-chan bool) error {
func (m *MockProcessRunner) Start(ctx context.Context, depStarted <-chan bool) error {
args := m.Called(depStarted)
return args.Error(0)
}
Expand Down

0 comments on commit 790f0c3

Please sign in to comment.