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

Make signal trapping optional

Go programs using the caddy package may not want the it to capture all the signals...
parent 7b064535
...@@ -11,9 +11,6 @@ ...@@ -11,9 +11,6 @@
// //
// 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 signals.
// It has to do this in order to perform shutdowns or reloads.
package caddy package caddy
import ( import (
......
package caddy package caddy
// Restart restarts Caddy forcefully using newCaddyfile,
// or, if nil, the current/existing Caddyfile is reused.
func Restart(newCaddyfile Input) error { func Restart(newCaddyfile Input) error {
if newCaddyfile == nil { if newCaddyfile == nil {
caddyfileMu.Lock() caddyfileMu.Lock()
......
...@@ -9,10 +9,20 @@ import ( ...@@ -9,10 +9,20 @@ import (
"github.com/mholt/caddy/server" "github.com/mholt/caddy/server"
) )
func init() { // TrapSignals create signal handlers for all applicable signals for this
// Trap interrupt signal (cross-platform); triggers forceful shutdown // system. If your Go program uses signals, this is a rather invasive
// that executes shutdown callbacks first. A second interrupt signal // function; best to implement them yourself in that case. Signals are not
// will exit the process immediately. // required for the caddy package to function properly, but this is a
// convenient way to allow the user to control this package of your program.
func TrapSignals() {
trapSignalsCrossPlatform()
trapSignalsPosix()
}
// trapSignalsCrossPlatform captures SIGINT, which triggers forceful
// shutdown that executes shutdown callbacks first. A second interrupt
// signal will exit the process immediately.
func trapSignalsCrossPlatform() {
go func() { go func() {
shutdown := make(chan os.Signal, 1) shutdown := make(chan os.Signal, 1)
signal.Notify(shutdown, os.Interrupt) signal.Notify(shutdown, os.Interrupt)
......
...@@ -10,8 +10,8 @@ import ( ...@@ -10,8 +10,8 @@ import (
"syscall" "syscall"
) )
func init() { // trapSignalsPosix captures POSIX-only signals.
// Trap all POSIX-only signals func trapSignalsPosix() {
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) signal.Notify(sigchan, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGUSR1)
......
package caddy
func trapSignalsPosix() {}
...@@ -12,6 +12,7 @@ CHANGES ...@@ -12,6 +12,7 @@ CHANGES
- New -log flag to enable process log - New -log flag to enable process log
- New -pidfile flag to enable writing pidfile - New -pidfile flag to enable writing pidfile
- New -grace flag to customize the graceful shutdown timeout - New -grace flag to customize the graceful shutdown timeout
- New support for SIGHUP, SIGTERM, and SIGQUIT signals
- browse: Render filenames with multiple whitespace properly - browse: Render filenames with multiple whitespace properly
- markdown: Include Last-Modified header in response - markdown: Include Last-Modified header in response
- markdown: Render tables, strikethrough, and fenced code blocks - markdown: Render tables, strikethrough, and fenced code blocks
......
...@@ -30,6 +30,7 @@ const ( ...@@ -30,6 +30,7 @@ const (
) )
func init() { func init() {
caddy.TrapSignals()
flag.BoolVar(&letsencrypt.Agreed, "agree", false, "Agree to Let's Encrypt Subscriber Agreement") flag.BoolVar(&letsencrypt.Agreed, "agree", false, "Agree to Let's Encrypt Subscriber Agreement")
flag.StringVar(&letsencrypt.CAUrl, "ca", "https://acme-staging.api.letsencrypt.org/directory", "Certificate authority ACME server") flag.StringVar(&letsencrypt.CAUrl, "ca", "https://acme-staging.api.letsencrypt.org/directory", "Certificate authority ACME server")
flag.StringVar(&conf, "conf", "", "Configuration file to use (default="+caddy.DefaultConfigFile+")") flag.StringVar(&conf, "conf", "", "Configuration file to use (default="+caddy.DefaultConfigFile+")")
......
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