2022-04-26 20:50:09 +03:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/v1/agh"
|
|
|
|
"github.com/AdguardTeam/golibs/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
// signalHandler processes incoming signals and shuts services down.
|
|
|
|
type signalHandler struct {
|
|
|
|
signal chan os.Signal
|
|
|
|
|
|
|
|
// services are the services that are shut down before application
|
|
|
|
// exiting.
|
|
|
|
services []agh.Service
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle processes OS signals.
|
|
|
|
func (h *signalHandler) handle() {
|
2022-04-29 14:39:02 +03:00
|
|
|
defer log.OnPanic("signalHandler.handle")
|
2022-04-26 20:50:09 +03:00
|
|
|
|
|
|
|
for sig := range h.signal {
|
2022-04-29 14:39:02 +03:00
|
|
|
log.Info("sighdlr: received signal %q", sig)
|
2022-04-26 20:50:09 +03:00
|
|
|
|
|
|
|
if aghos.IsShutdownSignal(sig) {
|
|
|
|
h.shutdown()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exit status constants.
|
|
|
|
const (
|
|
|
|
statusSuccess = 0
|
|
|
|
statusError = 1
|
|
|
|
)
|
|
|
|
|
|
|
|
// shutdown gracefully shuts down all services.
|
|
|
|
func (h *signalHandler) shutdown() {
|
|
|
|
ctx, cancel := ctxWithDefaultTimeout()
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
status := statusSuccess
|
|
|
|
|
2022-04-29 14:39:02 +03:00
|
|
|
log.Info("sighdlr: shutting down services")
|
2022-04-26 20:50:09 +03:00
|
|
|
for i, service := range h.services {
|
|
|
|
err := service.Shutdown(ctx)
|
|
|
|
if err != nil {
|
2022-04-29 14:39:02 +03:00
|
|
|
log.Error("sighdlr: shutting down service at index %d: %s", i, err)
|
2022-04-26 20:50:09 +03:00
|
|
|
status = statusError
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-29 14:39:02 +03:00
|
|
|
log.Info("sighdlr: shutting down adguard home")
|
2022-04-26 20:50:09 +03:00
|
|
|
|
|
|
|
os.Exit(status)
|
|
|
|
}
|
|
|
|
|
|
|
|
// newSignalHandler returns a new signalHandler that shuts down svcs.
|
|
|
|
func newSignalHandler(svcs ...agh.Service) (h *signalHandler) {
|
|
|
|
h = &signalHandler{
|
|
|
|
signal: make(chan os.Signal, 1),
|
|
|
|
services: svcs,
|
|
|
|
}
|
|
|
|
|
|
|
|
aghos.NotifyShutdownSignal(h.signal)
|
|
|
|
|
|
|
|
return h
|
|
|
|
}
|