Skip to content

Commit

Permalink
Merge pull request #625 from nats-io/add_sigterm_trap
Browse files Browse the repository at this point in the history
[ADDED] Trap for SIGTERM
  • Loading branch information
kozlovic committed Jul 13, 2018
2 parents 5c5e801 + fdd06f1 commit 5452fec
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ services:
- mysql
before_script:
- EXCLUDE_VENDOR=$(go list ./... | grep -v "/vendor/")
- go build
- go install
- $(exit $(go fmt $EXCLUDE_VENDOR | wc -l))
- go vet $EXCLUDE_VENDOR
- $(exit $(misspell -locale US . | grep -v "vendor/" | wc -l))
Expand Down
4 changes: 2 additions & 2 deletions server/signal.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ import (
// Signal Handling
func (s *StanServer) handleSignals() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGUSR1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM, syscall.SIGUSR1)
go func() {
for sig := range c {
// Notify will relay only the signals that we have
// registered, so we don't need a "default" in the
// switch statement.
switch sig {
case syscall.SIGINT:
case syscall.SIGINT, syscall.SIGTERM:
s.Shutdown()
os.Exit(0)
case syscall.SIGUSR1:
Expand Down
41 changes: 41 additions & 0 deletions server/signal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
package server

import (
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
"sync"
"syscall"
"testing"
"time"
Expand Down Expand Up @@ -114,3 +117,41 @@ func TestSignalToReOpenLogFile(t *testing.T) {
f(iter)
}
}

type stderrCatcher struct {
sync.Mutex
b []byte
}

func (sc *stderrCatcher) Write(p []byte) (n int, err error) {
sc.Lock()
sc.b = append(sc.b, p...)
sc.Unlock()
return len(p), nil
}

func TestSignalTrapsSIGTERM(t *testing.T) {
// This test requires that the
cmd := exec.Command("nats-streaming-server")
sc := &stderrCatcher{}
cmd.Stderr = sc
cmd.Start()
// Wait for it to print some startup trace
waitFor(t, 2*time.Second, 15*time.Millisecond, func() error {
sc.Lock()
ready := bytes.Contains(sc.b, []byte("STREAM: ------------------"))
sc.Unlock()
if ready {
return nil
}
return fmt.Errorf("process not started yet")
})
syscall.Kill(cmd.Process.Pid, syscall.SIGTERM)
cmd.Wait()
sc.Lock()
gotIt := bytes.Contains(sc.b, []byte("Shutting down"))
sc.Unlock()
if !gotIt {
t.Fatalf("Did not get the Shutting down trace (make sure you did a `go install` prior to running the test")
}
}

0 comments on commit 5452fec

Please sign in to comment.