agent: simplify signal handling

pull/3108/head
Frank Schroeder 8 years ago committed by Frank Schröder
parent 34fd31b7b1
commit 96d8035adc

@ -28,9 +28,6 @@ import (
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
) )
// gracefulTimeout controls how long we wait before forcefully terminating
var gracefulTimeout = 5 * time.Second
// validDatacenter is used to validate a datacenter // validDatacenter is used to validate a datacenter
var validDatacenter = regexp.MustCompile("^[a-zA-Z0-9_-]+$") var validDatacenter = regexp.MustCompile("^[a-zA-Z0-9_-]+$")
@ -732,8 +729,7 @@ func (cmd *Command) wait(agent *Agent, cfg *Config) int {
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP) signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP)
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGPIPE) signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGPIPE)
// Wait for a signal for {
WAIT:
var sig os.Signal var sig os.Signal
var reloadErrCh chan error var reloadErrCh chan error
select { select {
@ -748,19 +744,17 @@ WAIT:
cmd.UI.Error(err.Error()) cmd.UI.Error(err.Error())
return 1 return 1
case <-agent.ShutdownCh(): case <-agent.ShutdownCh():
// Agent is already shutdown! // Agent is already down!
return 0 return 0
} }
// Skip SIGPIPE signals and skip logging whenever such signal is received as well switch sig {
if sig == syscall.SIGPIPE { case syscall.SIGPIPE:
goto WAIT continue
}
case syscall.SIGHUP:
cmd.UI.Output(fmt.Sprintf("Caught signal: %v", sig)) cmd.UI.Output(fmt.Sprintf("Caught signal: %v", sig))
// Check if this is a SIGHUP
if sig == syscall.SIGHUP {
conf, err := cmd.handleReload(agent, cfg) conf, err := cmd.handleReload(agent, cfg)
if conf != nil { if conf != nil {
cfg = conf cfg = conf
@ -772,25 +766,17 @@ WAIT:
if reloadErrCh != nil { if reloadErrCh != nil {
reloadErrCh <- err reloadErrCh <- err
} }
goto WAIT
}
// Check if we should do a graceful leave default:
graceful := false cmd.UI.Output(fmt.Sprintf("Caught signal: %v", sig))
if sig == os.Interrupt && !(*cfg.SkipLeaveOnInt) {
graceful = true
} else if sig == syscall.SIGTERM && (*cfg.LeaveOnTerm) {
graceful = true
}
// Bail fast if not doing a graceful leave graceful := (sig == os.Interrupt && !(*cfg.SkipLeaveOnInt)) || (sig == syscall.SIGTERM && (*cfg.LeaveOnTerm))
if !graceful { if !graceful {
return 1 return 1
} }
// Attempt a graceful leave
gracefulCh := make(chan struct{})
cmd.UI.Output("Gracefully shutting down agent...") cmd.UI.Output("Gracefully shutting down agent...")
gracefulCh := make(chan struct{})
go func() { go func() {
if err := agent.Leave(); err != nil { if err := agent.Leave(); err != nil {
cmd.UI.Error(fmt.Sprintf("Error: %s", err)) cmd.UI.Error(fmt.Sprintf("Error: %s", err))
@ -799,7 +785,7 @@ WAIT:
close(gracefulCh) close(gracefulCh)
}() }()
// Wait for leave or another signal gracefulTimeout := 5 * time.Second
select { select {
case <-signalCh: case <-signalCh:
return 1 return 1
@ -808,6 +794,8 @@ WAIT:
case <-gracefulCh: case <-gracefulCh:
return 0 return 0
} }
}
}
} }
// handleReload is invoked when we should reload our configs, e.g. SIGHUP // handleReload is invoked when we should reload our configs, e.g. SIGHUP

Loading…
Cancel
Save