Commit a76222f6 authored by Miek Gieben's avatar Miek Gieben Committed by Matt Holt

sigtrap: allow graceful shutdown for SIGTERM on posix (#1995)

* shutdown: allow graceful shutdown for SIGTERM on posix

The signal is already trapped; make it do the same thing as SIGQUIT to
be more inline with Unix/Linux shutdown expectations.

Fixes #1993

* Implement comment feedback ideas
parent e9515425
...@@ -25,25 +25,27 @@ import ( ...@@ -25,25 +25,27 @@ import (
// trapSignalsPosix captures POSIX-only signals. // trapSignalsPosix captures POSIX-only signals.
func trapSignalsPosix() { func trapSignalsPosix() {
signalToString := map[os.Signal]string{syscall.SIGHUP: "SIGHUP", syscall.SIGUSR1: "SIGUSR1"}
go func() { go func() {
sigchan := make(chan os.Signal, 1) sigchan := make(chan os.Signal, 1)
signal.Notify(sigchan, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGUSR1, syscall.SIGUSR2) signal.Notify(sigchan, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGUSR1, syscall.SIGUSR2)
for sig := range sigchan { for sig := range sigchan {
switch sig { switch sig {
case syscall.SIGTERM: case syscall.SIGQUIT:
log.Println("[INFO] SIGTERM: Terminating process") log.Println("[INFO] SIGQUIT: Terminating process")
if PidFile != "" { if PidFile != "" {
os.Remove(PidFile) os.Remove(PidFile)
} }
os.Exit(0) os.Exit(0)
case syscall.SIGQUIT: case syscall.SIGTERM:
log.Println("[INFO] SIGQUIT: Shutting down") log.Println("[INFO] SIGTERM: Shutting down")
exitCode := executeShutdownCallbacks("SIGQUIT") exitCode := executeShutdownCallbacks(signalToString[sig])
err := Stop() err := Stop()
if err != nil { if err != nil {
log.Printf("[ERROR] SIGQUIT stop: %v", err) log.Printf("[ERROR] SIGTERM stop: %v", err)
exitCode = 3 exitCode = 3
} }
if PidFile != "" { if PidFile != "" {
...@@ -51,32 +53,25 @@ func trapSignalsPosix() { ...@@ -51,32 +53,25 @@ func trapSignalsPosix() {
} }
os.Exit(exitCode) os.Exit(exitCode)
case syscall.SIGHUP: case syscall.SIGUSR1, syscall.SIGHUP:
log.Println("[INFO] SIGHUP: Hanging up") log.Printf("[INFO] %s: Reloading", signalToString[sig])
err := Stop()
if err != nil {
log.Printf("[ERROR] SIGHUP stop: %v", err)
}
case syscall.SIGUSR1:
log.Println("[INFO] SIGUSR1: Reloading")
// Start with the existing Caddyfile // Start with the existing Caddyfile
caddyfileToUse, inst, err := getCurrentCaddyfile() caddyfileToUse, inst, err := getCurrentCaddyfile()
if err != nil { if err != nil {
log.Printf("[ERROR] SIGUSR1: %v", err) log.Printf("[ERROR] %s: %v", signalToString[sig], err)
continue continue
} }
if loaderUsed.loader == nil { if loaderUsed.loader == nil {
// This also should never happen // This also should never happen
log.Println("[ERROR] SIGUSR1: no Caddyfile loader with which to reload Caddyfile") log.Printf("[ERROR] %s: no Caddyfile loader with which to reload Caddyfile", signalToString[sig])
continue continue
} }
// Load the updated Caddyfile // Load the updated Caddyfile
newCaddyfile, err := loaderUsed.loader.Load(inst.serverType) newCaddyfile, err := loaderUsed.loader.Load(inst.serverType)
if err != nil { if err != nil {
log.Printf("[ERROR] SIGUSR1: loading updated Caddyfile: %v", err) log.Printf("[ERROR] %s: loading updated Caddyfile: %v", signalToString[sig], err)
continue continue
} }
if newCaddyfile != nil { if newCaddyfile != nil {
...@@ -86,7 +81,7 @@ func trapSignalsPosix() { ...@@ -86,7 +81,7 @@ func trapSignalsPosix() {
// Kick off the restart; our work is done // Kick off the restart; our work is done
_, err = inst.Restart(caddyfileToUse) _, err = inst.Restart(caddyfileToUse)
if err != nil { if err != nil {
log.Printf("[ERROR] SIGUSR1: %v", err) log.Printf("[ERROR] %s: %v", signalToString[sig], err)
} }
case syscall.SIGUSR2: case syscall.SIGUSR2:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment