agent: simplify signal handling
This commit is contained in:
parent
6262d85dab
commit
055347f7cd
|
@ -28,9 +28,6 @@ import (
|
|||
"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
|
||||
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, syscall.SIGPIPE)
|
||||
|
||||
// Wait for a signal
|
||||
WAIT:
|
||||
for {
|
||||
var sig os.Signal
|
||||
var reloadErrCh chan error
|
||||
select {
|
||||
|
@ -748,19 +744,17 @@ WAIT:
|
|||
cmd.UI.Error(err.Error())
|
||||
return 1
|
||||
case <-agent.ShutdownCh():
|
||||
// Agent is already shutdown!
|
||||
// Agent is already down!
|
||||
return 0
|
||||
}
|
||||
|
||||
// Skip SIGPIPE signals and skip logging whenever such signal is received as well
|
||||
if sig == syscall.SIGPIPE {
|
||||
goto WAIT
|
||||
}
|
||||
switch sig {
|
||||
case syscall.SIGPIPE:
|
||||
continue
|
||||
|
||||
case syscall.SIGHUP:
|
||||
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)
|
||||
if conf != nil {
|
||||
cfg = conf
|
||||
|
@ -772,25 +766,17 @@ WAIT:
|
|||
if reloadErrCh != nil {
|
||||
reloadErrCh <- err
|
||||
}
|
||||
goto WAIT
|
||||
}
|
||||
|
||||
// Check if we should do a graceful leave
|
||||
graceful := false
|
||||
if sig == os.Interrupt && !(*cfg.SkipLeaveOnInt) {
|
||||
graceful = true
|
||||
} else if sig == syscall.SIGTERM && (*cfg.LeaveOnTerm) {
|
||||
graceful = true
|
||||
}
|
||||
default:
|
||||
cmd.UI.Output(fmt.Sprintf("Caught signal: %v", sig))
|
||||
|
||||
// Bail fast if not doing a graceful leave
|
||||
graceful := (sig == os.Interrupt && !(*cfg.SkipLeaveOnInt)) || (sig == syscall.SIGTERM && (*cfg.LeaveOnTerm))
|
||||
if !graceful {
|
||||
return 1
|
||||
}
|
||||
|
||||
// Attempt a graceful leave
|
||||
gracefulCh := make(chan struct{})
|
||||
cmd.UI.Output("Gracefully shutting down agent...")
|
||||
gracefulCh := make(chan struct{})
|
||||
go func() {
|
||||
if err := agent.Leave(); err != nil {
|
||||
cmd.UI.Error(fmt.Sprintf("Error: %s", err))
|
||||
|
@ -799,7 +785,7 @@ WAIT:
|
|||
close(gracefulCh)
|
||||
}()
|
||||
|
||||
// Wait for leave or another signal
|
||||
gracefulTimeout := 5 * time.Second
|
||||
select {
|
||||
case <-signalCh:
|
||||
return 1
|
||||
|
@ -808,6 +794,8 @@ WAIT:
|
|||
case <-gracefulCh:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// handleReload is invoked when we should reload our configs, e.g. SIGHUP
|
||||
|
|
Loading…
Reference in a new issue