Skip to content

Commit

Permalink
Launch agent transport as goroutine
Browse files Browse the repository at this point in the history
The agent depends on the transport to block and hold the program open.
Transports may block, but shouldn't be required to. This change ensures
the agent is self sustaining with a block and launches the transport in
its own goroutine.

Signed-off-by: Chris Doherty <chris.doherty4@gmail.com>
  • Loading branch information
chrisdoherty4 committed May 15, 2023
1 parent a410359 commit f723a96
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions internal/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package agent
import (
"context"
"errors"
"fmt"

"github.com/go-logr/logr"
"github.com/tinkerbell/tink/internal/agent/event"
Expand All @@ -27,6 +28,7 @@ type Agent struct {
// Runtime is the container runtime used to execute workflow actions.
Runtime ContainerRuntime

// sem ensure we handle a single workflow at a time.
sem chan struct{}
}

Expand All @@ -43,8 +45,7 @@ func (agent *Agent) Start(ctx context.Context) error {
}

if agent.Runtime == nil {
//nolint:stylecheck // Specifying field on data structure
return errors.New("Runtime field must be set before calling Start()")
return errors.New("agent.Runtime must be set before calling Start()")
}

agent.Log = agent.Log.WithValues("agent_id", agent.ID)
Expand All @@ -53,8 +54,19 @@ func (agent *Agent) Start(ctx context.Context) error {
agent.sem = make(chan struct{}, 1)
agent.sem <- struct{}{}

agent.Log.Info("Starting agent")
return agent.Transport.Start(ctx, agent.ID, agent)
// Launch the transport ensuring we can recover any errors.
transportErr := make(chan error, 1)
go func() {
agent.Log.Info("Starting agent")
transportErr <- agent.Transport.Start(ctx, agent.ID, agent)
}()

select {
case err := <-transportErr:
return fmt.Errorf("transport: %w", err)
case <-ctx.Done():
return ctx.Err()
}
}

// HandleWorkflow satisfies transport.
Expand Down

0 comments on commit f723a96

Please sign in to comment.