mirror of
https://github.com/caddyserver/caddy.git
synced 2025-02-08 17:16:36 +01:00
Make signal trapping optional
Go programs using the caddy package may not want the it to capture all the signals...
This commit is contained in:
parent
7b064535bf
commit
7d5b6b96ea
7 changed files with 23 additions and 9 deletions
|
@ -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 (
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
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 (
|
||||||
"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 (
|
||||||
"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)
|
||||||
|
|
3
caddy/sigtrap_windows.go
Normal file
3
caddy/sigtrap_windows.go
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
package caddy
|
||||||
|
|
||||||
|
func trapSignalsPosix() {}
|
1
dist/CHANGES.txt
vendored
1
dist/CHANGES.txt
vendored
|
@ -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
|
||||||
|
|
1
main.go
1
main.go
|
@ -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+")")
|
||||||
|
|
Loading…
Reference in a new issue