mirror of
https://github.com/caddyserver/caddy.git
synced 2025-02-08 17:16:36 +01:00
Changed SIGINT and added support for HUP, QUIT, and TERM
This commit is contained in:
parent
b42334eb91
commit
7b064535bf
3 changed files with 78 additions and 37 deletions
|
@ -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 {
|
||||||
|
|
||||||
// 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 (
|
||||||
"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)
|
||||||
<-shutdown
|
|
||||||
|
|
||||||
var exitCode int
|
for i := 0; true; i++ {
|
||||||
|
<-shutdown
|
||||||
|
|
||||||
|
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,40 +11,62 @@ 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)
|
||||||
|
|
||||||
log.Println("[INFO] SIGUSR1: Reloading")
|
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)
|
||||||
|
|
||||||
var updatedCaddyfile Input
|
case syscall.SIGHUP:
|
||||||
|
log.Println("[INFO] SIGHUP: Hanging up")
|
||||||
|
err := Stop()
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("[ERROR] SIGHUP stop: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
caddyfileMu.Lock()
|
case syscall.SIGUSR1:
|
||||||
if caddyfile == nil {
|
log.Println("[INFO] SIGUSR1: Reloading")
|
||||||
// Hmm, did spawing process forget to close stdin? Anyhow, this is unusual.
|
|
||||||
log.Println("[ERROR] SIGUSR1: no Caddyfile to reload (was stdin left open?)")
|
var updatedCaddyfile Input
|
||||||
caddyfileMu.Unlock()
|
|
||||||
continue
|
caddyfileMu.Lock()
|
||||||
}
|
if caddyfile == nil {
|
||||||
if caddyfile.IsFile() {
|
// Hmm, did spawing process forget to close stdin? Anyhow, this is unusual.
|
||||||
body, err := ioutil.ReadFile(caddyfile.Path())
|
log.Println("[ERROR] SIGUSR1: no Caddyfile to reload (was stdin left open?)")
|
||||||
if err == nil {
|
caddyfileMu.Unlock()
|
||||||
updatedCaddyfile = CaddyfileInput{
|
continue
|
||||||
Filepath: caddyfile.Path(),
|
}
|
||||||
Contents: body,
|
if caddyfile.IsFile() {
|
||||||
RealFile: true,
|
body, err := ioutil.ReadFile(caddyfile.Path())
|
||||||
|
if err == nil {
|
||||||
|
updatedCaddyfile = CaddyfileInput{
|
||||||
|
Filepath: caddyfile.Path(),
|
||||||
|
Contents: body,
|
||||||
|
RealFile: true,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
caddyfileMu.Unlock()
|
||||||
caddyfileMu.Unlock()
|
|
||||||
|
|
||||||
err := Restart(updatedCaddyfile)
|
err := Restart(updatedCaddyfile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("[ERROR] SIGUSR1: %v", err)
|
log.Printf("[ERROR] SIGUSR1: %v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
Loading…
Reference in a new issue