Skip to content

Commit

Permalink
Use errgroup for all go routines
Browse files Browse the repository at this point in the history
Instead of using log.Fatal in some of them

Signed-off-by: Richard Wall <richard.wall@venafi.com>
  • Loading branch information
wallrj committed Oct 30, 2024
1 parent 3da4d47 commit 0cb53e6
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions pkg/agent/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,16 @@ func Run(cmd *cobra.Command, args []string) {
logs.Log.Fatalf("While evaluating configuration: %v", err)
}

go func() {
group, gctx := errgroup.WithContext(ctx)
defer func() {
// TODO: replace Fatalf log calls with Errorf and return the error
cancel()
if err := group.Wait(); err != nil {
logs.Log.Fatalf("failed to wait for controller-runtime component to stop: %v", err)
}
}()

group.Go(func() error {
server := http.NewServeMux()

if Flags.Profiling {
Expand Down Expand Up @@ -105,21 +114,25 @@ func Run(cmd *cobra.Command, args []string) {

err := http.ListenAndServe(":8081", server)
if err != nil && !errors.Is(err, http.ErrServerClosed) {
logs.Log.Fatalf("failed to run the health check server: %s", err)
return fmt.Errorf("failed to run the health check server: %s", err)
}
}()
// The agent must stop if the management server stops
cancel()
return nil
})

_, isVenConn := preflightClient.(*client.VenConnClient)
if isVenConn {
go func() {
err := preflightClient.(manager.Runnable).Start(ctx)
group.Go(func() error {
err := preflightClient.(manager.Runnable).Start(gctx)
if err != nil {
logs.Log.Fatalf("failed to start a controller-runtime component: %v", err)
return fmt.Errorf("failed to start a controller-runtime component: %v", err)
}

// The agent must stop if the controller-runtime component stops.
cancel()
}()
return nil
})
}

// To help users notice issues with the agent, we show the error messages in
Expand All @@ -130,15 +143,6 @@ func Run(cmd *cobra.Command, args []string) {
}

dataGatherers := map[string]datagatherer.DataGatherer{}
group, gctx := errgroup.WithContext(ctx)

defer func() {
// TODO: replace Fatalf log calls with Errorf and return the error
cancel()
if err := group.Wait(); err != nil {
logs.Log.Fatalf("failed to wait for controller-runtime component to stop: %v", err)
}
}()

// load datagatherer config and boot each one
for _, dgConfig := range config.DataGatherers {
Expand All @@ -160,6 +164,8 @@ func Run(cmd *cobra.Command, args []string) {
if err := newDg.Run(gctx.Done()); err != nil {
return fmt.Errorf("failed to start %q data gatherer %q: %v", kind, dgConfig.Name, err)
}
// The agent must stop if any of the data gatherers stops
cancel()
return nil
})

Expand Down Expand Up @@ -200,7 +206,11 @@ func Run(cmd *cobra.Command, args []string) {
break
}

time.Sleep(config.Period)
select {
case <-gctx.Done():
return
case <-time.After(config.Period):
}
}
}

Expand Down

0 comments on commit 0cb53e6

Please sign in to comment.