Skip to content

Commit 9c4da94

Browse files
committed
STAC-16788: Updated process agent graceful shutdown
1 parent d772a7e commit 9c4da94

4 files changed

Lines changed: 65 additions & 88 deletions

File tree

cmd/agent/collector.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,6 @@ import (
55
"context"
66
"encoding/json"
77
"fmt"
8-
"github.com/StackVista/stackstate-agent/pkg/aggregator"
9-
"github.com/StackVista/stackstate-agent/pkg/batcher"
10-
"github.com/StackVista/stackstate-agent/pkg/collector/check"
11-
"github.com/StackVista/stackstate-agent/pkg/telemetry"
12-
"github.com/StackVista/stackstate-agent/pkg/topology"
13-
"github.com/StackVista/stackstate-process-agent/cmd/agent/features"
148
"io"
159
"io/ioutil"
1610
"math/rand"
@@ -19,6 +13,13 @@ import (
1913
"sync/atomic"
2014
"time"
2115

16+
"github.com/StackVista/stackstate-agent/pkg/aggregator"
17+
"github.com/StackVista/stackstate-agent/pkg/batcher"
18+
"github.com/StackVista/stackstate-agent/pkg/collector/check"
19+
"github.com/StackVista/stackstate-agent/pkg/telemetry"
20+
"github.com/StackVista/stackstate-agent/pkg/topology"
21+
"github.com/StackVista/stackstate-process-agent/cmd/agent/features"
22+
2223
log "github.com/cihub/seelog"
2324

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

143-
handleSignals(exit)
144+
go HandleSignals(exit)
144145
heartbeat := time.NewTicker(15 * time.Second)
145146
queueSizeTicker := time.NewTicker(10 * time.Second)
146147
featuresTicker := time.NewTicker(5 * time.Second)

cmd/agent/main_docker.go

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,23 @@ import (
1212
log "github.com/cihub/seelog"
1313
)
1414

15-
// Handles signals - tells us whether we should exit.
16-
func handleSignals(exit chan bool) {
17-
signalCh := make(chan os.Signal, 1)
18-
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM)
19-
20-
go func() {
21-
// Set up the signals async so we can Start the agent
22-
select {
23-
case sig := <-signalCh:
24-
log.Infof("Received signal '%s', shutting down...", sig)
25-
signalCh <- nil
26-
exit <- true
27-
default:
28-
// continue
29-
}
30-
}()
31-
32-
// By default systemd redirects the stdout to journald. When journald is stopped or crashes we receive a SIGPIPE signal.
33-
// Go ignores SIGPIPE signals unless it is when stdout or stdout is closed, in this case the agent is stopped.
34-
// We never want the agent to stop upon receiving SIGPIPE, so we intercept the SIGPIPE signals and just discard them.
35-
sigpipeCh := make(chan os.Signal, 1)
36-
signal.Notify(sigpipeCh, syscall.SIGPIPE)
37-
go func() {
38-
for range sigpipeCh {
39-
// do nothing
15+
// HandleSignals tells us whether we should exit.
16+
func HandleSignals(exit chan bool) {
17+
sigIn := make(chan os.Signal, 100)
18+
signal.Notify(sigIn, syscall.SIGINT, syscall.SIGTERM, syscall.SIGPIPE)
19+
// unix only in all likelihood; but we don't care.
20+
for sig := range sigIn {
21+
switch sig {
22+
case syscall.SIGINT, syscall.SIGTERM:
23+
log.Infof("Caught signal '%s'; terminating.", sig)
24+
close(exit)
25+
return
26+
case syscall.SIGPIPE:
27+
// By default systemd redirects the stdout to journald. When journald is stopped or crashes we receive a SIGPIPE signal.
28+
// Go ignores SIGPIPE signals unless it is when stderr or stdout is closed, in this case the agent is stopped.
29+
// We never want the agent to stop upon receiving SIGPIPE, so we intercept the SIGPIPE signals and just discard them.
30+
// See https://golang.org/pkg/os/signal/#hdr-SIGPIPE
31+
continue
4032
}
41-
}()
33+
}
4234
}

cmd/agent/main_nodocker.go

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,23 @@ import (
1111
log "github.com/cihub/seelog"
1212
)
1313

14-
// Handles signals - tells us whether we should exit.
15-
func handleSignals(exit chan bool) {
16-
signalCh := make(chan os.Signal, 1)
17-
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM)
18-
19-
go func() {
20-
// Set up the signals async so we can Start the agent
21-
select {
22-
case sig := <-signalCh:
23-
log.Infof("Received signal '%s', shutting down...", sig)
24-
signalCh <- nil
25-
exit <- true
26-
default:
27-
// continue
28-
}
29-
}()
30-
31-
// By default systemd redirects the stdout to journald. When journald is stopped or crashes we receive a SIGPIPE signal.
32-
// Go ignores SIGPIPE signals unless it is when stdout or stdout is closed, in this case the agent is stopped.
33-
// We never want the agent to stop upon receiving SIGPIPE, so we intercept the SIGPIPE signals and just discard them.
34-
sigpipeCh := make(chan os.Signal, 1)
35-
signal.Notify(sigpipeCh, syscall.SIGPIPE)
36-
go func() {
37-
for range sigpipeCh {
38-
// do nothing
14+
// HandleSignals tells us whether we should exit.
15+
func HandleSignals(exit chan bool) {
16+
sigIn := make(chan os.Signal, 100)
17+
signal.Notify(sigIn, syscall.SIGINT, syscall.SIGTERM, syscall.SIGPIPE)
18+
// unix only in all likelihood; but we don't care.
19+
for sig := range sigIn {
20+
switch sig {
21+
case syscall.SIGINT, syscall.SIGTERM:
22+
log.Infof("Caught signal '%s'; terminating.", sig)
23+
close(exit)
24+
return
25+
case syscall.SIGPIPE:
26+
// By default systemd redirects the stdout to journald. When journald is stopped or crashes we receive a SIGPIPE signal.
27+
// Go ignores SIGPIPE signals unless it is when stderr or stdout is closed, in this case the agent is stopped.
28+
// We never want the agent to stop upon receiving SIGPIPE, so we intercept the SIGPIPE signals and just discard them.
29+
// See https://golang.org/pkg/os/signal/#hdr-SIGPIPE
30+
continue
3931
}
40-
}()
41-
32+
}
4233
}

cmd/network-tracer/main.go

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ func main() {
9292
log.Infof("network tracer started")
9393

9494
// Handles signals, which tells us whether we should exit.
95-
e := make(chan bool)
96-
handleSignals(e)
97-
<-e
95+
exit := make(chan bool)
96+
go HandleSignals(exit)
97+
<-exit
9898
}
9999

100100
func gracefulExit() {
@@ -105,32 +105,25 @@ func gracefulExit() {
105105
os.Exit(0)
106106
}
107107

108-
func handleSignals(exit chan bool) {
109-
signalCh := make(chan os.Signal, 1)
110-
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM)
111-
112-
go func() {
113-
// Set up the signals async so we can Start the agent
114-
select {
115-
case sig := <-signalCh:
116-
log.Infof("Received signal '%s', shutting down...", sig)
117-
signalCh <- nil
118-
exit <- true
119-
default:
120-
// continue
108+
// HandleSignals tells us whether we should exit.
109+
func HandleSignals(exit chan bool) {
110+
sigIn := make(chan os.Signal, 100)
111+
signal.Notify(sigIn, syscall.SIGINT, syscall.SIGTERM, syscall.SIGPIPE)
112+
// unix only in all likelihood; but we don't care.
113+
for sig := range sigIn {
114+
switch sig {
115+
case syscall.SIGINT, syscall.SIGTERM:
116+
log.Infof("Caught signal '%s'; terminating.", sig)
117+
close(exit)
118+
return
119+
case syscall.SIGPIPE:
120+
// By default systemd redirects the stdout to journald. When journald is stopped or crashes we receive a SIGPIPE signal.
121+
// Go ignores SIGPIPE signals unless it is when stderr or stdout is closed, in this case the agent is stopped.
122+
// We never want the agent to stop upon receiving SIGPIPE, so we intercept the SIGPIPE signals and just discard them.
123+
// See https://golang.org/pkg/os/signal/#hdr-SIGPIPE
124+
continue
121125
}
122-
}()
123-
124-
// By default systemd redirects the stdout to journald. When journald is stopped or crashes we receive a SIGPIPE signal.
125-
// Go ignores SIGPIPE signals unless it is when stdout or stdout is closed, in this case the agent is stopped.
126-
// We never want the agent to stop upon receiving SIGPIPE, so we intercept the SIGPIPE signals and just discard them.
127-
sigpipeCh := make(chan os.Signal, 1)
128-
signal.Notify(sigpipeCh, syscall.SIGPIPE)
129-
go func() {
130-
for range sigpipeCh {
131-
// do nothing
132-
}
133-
}()
126+
}
134127
}
135128

136129
// versionString returns the version information filled in at build time

0 commit comments

Comments
 (0)