Skip to content

Commit

Permalink
STAC-16788: Updated process agent graceful shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
zvheerden committed Aug 16, 2022
1 parent d772a7e commit 0843810
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 88 deletions.
15 changes: 8 additions & 7 deletions cmd/agent/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/StackVista/stackstate-agent/pkg/aggregator"
"github.com/StackVista/stackstate-agent/pkg/batcher"
"github.com/StackVista/stackstate-agent/pkg/collector/check"
"github.com/StackVista/stackstate-agent/pkg/telemetry"
"github.com/StackVista/stackstate-agent/pkg/topology"
"github.com/StackVista/stackstate-process-agent/cmd/agent/features"
"io"
"io/ioutil"
"math/rand"
Expand All @@ -19,6 +13,13 @@ import (
"sync/atomic"
"time"

"github.com/StackVista/stackstate-agent/pkg/aggregator"
"github.com/StackVista/stackstate-agent/pkg/batcher"
"github.com/StackVista/stackstate-agent/pkg/collector/check"
"github.com/StackVista/stackstate-agent/pkg/telemetry"
"github.com/StackVista/stackstate-agent/pkg/topology"
"github.com/StackVista/stackstate-process-agent/cmd/agent/features"

log "github.com/cihub/seelog"

"github.com/StackVista/stackstate-process-agent/checks"
Expand Down Expand Up @@ -140,7 +141,7 @@ func (l *Collector) run(exit chan bool) {
}
log.Infof("Starting process-agent for host=%s, endpoints=%s, enabled checks=%v", l.cfg.HostName, eps, l.cfg.EnabledChecks)

handleSignals(exit)
go HandleSignals(exit)
heartbeat := time.NewTicker(15 * time.Second)
queueSizeTicker := time.NewTicker(10 * time.Second)
featuresTicker := time.NewTicker(5 * time.Second)
Expand Down
44 changes: 18 additions & 26 deletions cmd/agent/main_docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,23 @@ import (
log "github.com/cihub/seelog"
)

// Handles signals - tells us whether we should exit.
func handleSignals(exit chan bool) {
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM)

go func() {
// Set up the signals async so we can Start the agent
select {
case sig := <-signalCh:
log.Infof("Received signal '%s', shutting down...", sig)
signalCh <- nil
exit <- true
default:
// continue
}
}()

// By default systemd redirects the stdout to journald. When journald is stopped or crashes we receive a SIGPIPE signal.
// Go ignores SIGPIPE signals unless it is when stdout or stdout is closed, in this case the agent is stopped.
// We never want the agent to stop upon receiving SIGPIPE, so we intercept the SIGPIPE signals and just discard them.
sigpipeCh := make(chan os.Signal, 1)
signal.Notify(sigpipeCh, syscall.SIGPIPE)
go func() {
for range sigpipeCh {
// do nothing
// HandleSignals tells us whether we should exit.
func HandleSignals(exit chan struct{}) {
sigIn := make(chan os.Signal, 100)
signal.Notify(sigIn, syscall.SIGINT, syscall.SIGTERM, syscall.SIGPIPE)
// unix only in all likelihood; but we don't care.
for sig := range sigIn {
switch sig {
case syscall.SIGINT, syscall.SIGTERM:
log.Infof("Caught signal '%s'; terminating.", sig)
close(exit)
return
case syscall.SIGPIPE:
// By default systemd redirects the stdout to journald. When journald is stopped or crashes we receive a SIGPIPE signal.
// Go ignores SIGPIPE signals unless it is when stderr or stdout is closed, in this case the agent is stopped.
// We never want the agent to stop upon receiving SIGPIPE, so we intercept the SIGPIPE signals and just discard them.
// See https://golang.org/pkg/os/signal/#hdr-SIGPIPE
continue
}
}()
}
}
45 changes: 18 additions & 27 deletions cmd/agent/main_nodocker.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,23 @@ import (
log "github.com/cihub/seelog"
)

// Handles signals - tells us whether we should exit.
func handleSignals(exit chan bool) {
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM)

go func() {
// Set up the signals async so we can Start the agent
select {
case sig := <-signalCh:
log.Infof("Received signal '%s', shutting down...", sig)
signalCh <- nil
exit <- true
default:
// continue
}
}()

// By default systemd redirects the stdout to journald. When journald is stopped or crashes we receive a SIGPIPE signal.
// Go ignores SIGPIPE signals unless it is when stdout or stdout is closed, in this case the agent is stopped.
// We never want the agent to stop upon receiving SIGPIPE, so we intercept the SIGPIPE signals and just discard them.
sigpipeCh := make(chan os.Signal, 1)
signal.Notify(sigpipeCh, syscall.SIGPIPE)
go func() {
for range sigpipeCh {
// do nothing
// HandleSignals tells us whether we should exit.
func HandleSignals(exit chan bool) {
sigIn := make(chan os.Signal, 100)
signal.Notify(sigIn, syscall.SIGINT, syscall.SIGTERM, syscall.SIGPIPE)
// unix only in all likelihood; but we don't care.
for sig := range sigIn {
switch sig {
case syscall.SIGINT, syscall.SIGTERM:
log.Infof("Caught signal '%s'; terminating.", sig)
close(exit)
return
case syscall.SIGPIPE:
// By default systemd redirects the stdout to journald. When journald is stopped or crashes we receive a SIGPIPE signal.
// Go ignores SIGPIPE signals unless it is when stderr or stdout is closed, in this case the agent is stopped.
// We never want the agent to stop upon receiving SIGPIPE, so we intercept the SIGPIPE signals and just discard them.
// See https://golang.org/pkg/os/signal/#hdr-SIGPIPE
continue
}
}()

}
}
49 changes: 21 additions & 28 deletions cmd/network-tracer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ func main() {
log.Infof("network tracer started")

// Handles signals, which tells us whether we should exit.
e := make(chan bool)
handleSignals(e)
<-e
exit := make(chan bool)
go HandleSignals(exit)
<-exit
}

func gracefulExit() {
Expand All @@ -105,32 +105,25 @@ func gracefulExit() {
os.Exit(0)
}

func handleSignals(exit chan bool) {
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM)

go func() {
// Set up the signals async so we can Start the agent
select {
case sig := <-signalCh:
log.Infof("Received signal '%s', shutting down...", sig)
signalCh <- nil
exit <- true
default:
// continue
// HandleSignals tells us whether we should exit.
func HandleSignals(exit chan bool) {
sigIn := make(chan os.Signal, 100)
signal.Notify(sigIn, syscall.SIGINT, syscall.SIGTERM, syscall.SIGPIPE)
// unix only in all likelihood; but we don't care.
for sig := range sigIn {
switch sig {
case syscall.SIGINT, syscall.SIGTERM:
log.Infof("Caught signal '%s'; terminating.", sig)
close(exit)
return
case syscall.SIGPIPE:
// By default systemd redirects the stdout to journald. When journald is stopped or crashes we receive a SIGPIPE signal.
// Go ignores SIGPIPE signals unless it is when stderr or stdout is closed, in this case the agent is stopped.
// We never want the agent to stop upon receiving SIGPIPE, so we intercept the SIGPIPE signals and just discard them.
// See https://golang.org/pkg/os/signal/#hdr-SIGPIPE
continue
}
}()

// By default systemd redirects the stdout to journald. When journald is stopped or crashes we receive a SIGPIPE signal.
// Go ignores SIGPIPE signals unless it is when stdout or stdout is closed, in this case the agent is stopped.
// We never want the agent to stop upon receiving SIGPIPE, so we intercept the SIGPIPE signals and just discard them.
sigpipeCh := make(chan os.Signal, 1)
signal.Notify(sigpipeCh, syscall.SIGPIPE)
go func() {
for range sigpipeCh {
// do nothing
}
}()
}
}

// versionString returns the version information filled in at build time
Expand Down

0 comments on commit 0843810

Please sign in to comment.