Commit 7b064535 authored by Matthew Holt's avatar Matthew Holt

Changed SIGINT and added support for HUP, QUIT, and TERM

parent b42334eb
...@@ -12,8 +12,7 @@ ...@@ -12,8 +12,7 @@
// You should use caddy.Wait() to wait for all Caddy servers // You should use caddy.Wait() to wait for all Caddy servers
// to quit before your process exits. // to quit before your process exits.
// //
// Importing this package has the side-effect of trapping // Importing this package has the side-effect of trapping signals.
// SIGINT on all platforms and SIGUSR1 on not-Windows systems.
// It has to do this in order to perform shutdowns or reloads. // It has to do this in order to perform shutdowns or reloads.
package caddy package caddy
...@@ -275,7 +274,7 @@ func startServers(groupings bindingGroup) error { ...@@ -275,7 +274,7 @@ func startServers(groupings bindingGroup) error {
// Stop stops all servers. It blocks until they are all stopped. // Stop stops all servers. It blocks until they are all stopped.
// It does NOT execute shutdown callbacks that may have been // It does NOT execute shutdown callbacks that may have been
// configured by middleware (they are executed on SIGINT). // configured by middleware (they must be executed separately).
func Stop() error { func Stop() error {
letsencrypt.Deactivate() letsencrypt.Deactivate()
......
...@@ -4,30 +4,50 @@ import ( ...@@ -4,30 +4,50 @@ import (
"log" "log"
"os" "os"
"os/signal" "os/signal"
"sync"
"github.com/mholt/caddy/server" "github.com/mholt/caddy/server"
) )
func init() { func init() {
// Trap quit signals (cross-platform) // Trap interrupt signal (cross-platform); triggers forceful shutdown
// that executes shutdown callbacks first. A second interrupt signal
// will exit the process immediately.
go func() { go func() {
shutdown := make(chan os.Signal, 1) shutdown := make(chan os.Signal, 1)
signal.Notify(shutdown, os.Interrupt, os.Kill) signal.Notify(shutdown, os.Interrupt)
for i := 0; true; i++ {
<-shutdown <-shutdown
var exitCode int if i > 0 {
log.Println("[INFO] SIGINT: Force quit")
os.Exit(1)
}
log.Println("[INFO] SIGINT: Shutting down")
go os.Exit(executeShutdownCallbacks("SIGINT"))
}
}()
}
// executeShutdownCallbacks executes the shutdown callbacks as initiated
// by signame. It logs any errors and returns the recommended exit status.
// This function is idempotent; subsequent invocations always return 0.
func executeShutdownCallbacks(signame string) (exitCode int) {
shutdownCallbacksOnce.Do(func() {
serversMu.Lock() serversMu.Lock()
errs := server.ShutdownCallbacks(servers) errs := server.ShutdownCallbacks(servers)
serversMu.Unlock() serversMu.Unlock()
if len(errs) > 0 { if len(errs) > 0 {
for _, err := range errs { for _, err := range errs {
log.Printf("[ERROR] Shutting down: %v", err) log.Printf("[ERROR] %s shutdown: %v", signame, err)
} }
exitCode = 1 exitCode = 1
} }
})
os.Exit(exitCode) return
}()
} }
var shutdownCallbacksOnce sync.Once
...@@ -11,14 +11,35 @@ import ( ...@@ -11,14 +11,35 @@ import (
) )
func init() { func init() {
// Trap POSIX-only signals // Trap all POSIX-only signals
go func() { go func() {
reload := make(chan os.Signal, 1) sigchan := make(chan os.Signal, 1)
signal.Notify(reload, syscall.SIGUSR1) // reload configuration signal.Notify(sigchan, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGUSR1)
for { for sig := range sigchan {
<-reload switch sig {
case syscall.SIGTERM:
log.Println("[INFO] SIGTERM: Terminating process")
os.Exit(0)
case syscall.SIGQUIT:
log.Println("[INFO] SIGQUIT: Shutting down")
exitCode := executeShutdownCallbacks("SIGQUIT")
err := Stop()
if err != nil {
log.Printf("[ERROR] SIGQUIT stop: %v", err)
exitCode = 1
}
os.Exit(exitCode)
case syscall.SIGHUP:
log.Println("[INFO] SIGHUP: Hanging up")
err := Stop()
if err != nil {
log.Printf("[ERROR] SIGHUP stop: %v", err)
}
case syscall.SIGUSR1:
log.Println("[INFO] SIGUSR1: Reloading") log.Println("[INFO] SIGUSR1: Reloading")
var updatedCaddyfile Input var updatedCaddyfile Input
...@@ -47,5 +68,6 @@ func init() { ...@@ -47,5 +68,6 @@ func init() {
log.Printf("[ERROR] SIGUSR1: %v", err) log.Printf("[ERROR] SIGUSR1: %v", err)
} }
} }
}
}() }()
} }
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