certificates/ca/signal.go

76 lines
1.9 KiB
Go
Raw Permalink Normal View History

2018-10-05 21:48:36 +00:00
package ca
import (
"log"
"os"
"os/signal"
"syscall"
)
2019-04-03 18:07:11 +00:00
// Stopper is the interface that external commands can implement to stop the
// server.
type Stopper interface {
Stop() error
}
2018-10-05 21:48:36 +00:00
// StopReloader is the interface that external commands can implement to stop
// the server and reload the configuration while running.
type StopReloader interface {
Stop() error
Reload() error
}
2019-04-03 18:07:11 +00:00
// StopHandler watches SIGINT, SIGTERM on a list of servers implementing the
// Stopper interface, and when one of those signals is caught we'll run Stop
// (SIGINT, SIGTERM) on all servers.
func StopHandler(servers ...Stopper) {
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
defer signal.Stop(signals)
2019-09-05 01:31:09 +00:00
for sig := range signals {
switch sig {
case syscall.SIGINT, syscall.SIGTERM:
log.Println("shutting down ...")
for _, server := range servers {
err := server.Stop()
if err != nil {
log.Printf("error stopping server: %s", err.Error())
2019-04-03 18:07:11 +00:00
}
}
2019-09-05 01:31:09 +00:00
return
2019-04-03 18:07:11 +00:00
}
}
}
2018-10-05 21:48:36 +00:00
// StopReloaderHandler watches SIGINT, SIGTERM and SIGHUP on a list of servers
// implementing the StopReloader interface, and when one of those signals is
// caught we'll run Stop (SIGINT, SIGTERM) or Reload (SIGHUP) on all servers.
func StopReloaderHandler(servers ...StopReloader) {
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
defer signal.Stop(signals)
2019-09-05 01:31:09 +00:00
for sig := range signals {
switch sig {
case syscall.SIGHUP:
log.Println("reloading ...")
for _, server := range servers {
err := server.Reload()
if err != nil {
log.Printf("error reloading server: %+v", err)
2018-10-05 21:48:36 +00:00
}
2019-09-05 01:31:09 +00:00
}
case syscall.SIGINT, syscall.SIGTERM:
log.Println("shutting down ...")
for _, server := range servers {
err := server.Stop()
if err != nil {
log.Printf("error stopping server: %s", err.Error())
2018-10-05 21:48:36 +00:00
}
}
2019-09-05 01:31:09 +00:00
return
2018-10-05 21:48:36 +00:00
}
}
}