Skip to content

Commit

Permalink
remove handling signals as they are handled in main function
Browse files Browse the repository at this point in the history
  • Loading branch information
kasia-kujawa committed Dec 11, 2024
1 parent f389297 commit 6dba283
Showing 1 changed file with 2 additions and 10 deletions.
12 changes: 2 additions & 10 deletions cmd/agent/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import (
"net"
"net/http"
"os"
"os/signal"
"runtime"
"syscall"
"time"

"github.com/samber/lo"
Expand Down Expand Up @@ -116,13 +114,10 @@ func runAgentMode(ctx context.Context, castaiclient castai.Client, log *logrus.E
ctx, ctxCancel := context.WithCancel(ctx)
defer ctxCancel()

sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM, syscall.SIGKILL)

// buffer will allow for all senders to push, even though we will only read first error and cancel context after it;
// all errors from exitCh are logged
exitCh := make(chan error, 10)
go watchExitErrors(ctx, log, exitCh, sigChan, ctxCancel)
go watchExitErrors(ctx, log, exitCh, ctxCancel)

agentVersion := config.VersionInfo
log.Infof("running agent version: %v", agentVersion)
Expand Down Expand Up @@ -250,17 +245,14 @@ func runAgentMode(ctx context.Context, castaiclient castai.Client, log *logrus.E
}

// if any errors are observed on exitCh, context cancel is called, and all errors in the channel are logged
func watchExitErrors(ctx context.Context, log *logrus.Entry, exitCh chan error, sigChan chan os.Signal, ctxCancel func()) {
func watchExitErrors(ctx context.Context, log *logrus.Entry, exitCh chan error, ctxCancel func()) {
for {
select {
case err := <-exitCh:
if err != nil && !errors.Is(err, context.Canceled) {
log.Errorf("agent stopped with an error: %v", err)
}
ctxCancel()
case s := <-sigChan:
log.Infof("received system signal: %v, closing agent", s)
ctxCancel()
case <-ctx.Done():
log.Infof("context done")
return
Expand Down

0 comments on commit 6dba283

Please sign in to comment.