mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-28 18:08:51 +03:00
f5602d9c46
Merge in DNS/adguard-home from hup-reload to master Squashed commit of the following: commit 5cd4ab85bdc7544a4eded2a61f5a5571175daa44 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Fri Oct 7 19:58:17 2022 +0300 next: imp signal hdlr commit 8fd18e749fec46982d26fc408e661bd802586c37 Merge: a8780455f1dd3334
Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Fri Oct 7 19:46:48 2022 +0300 Merge branch 'master' into hup-reload commit a87804550e15d7fe3d9ded2e5a736c395f96febd Merge: 349dbe54960a7a75
Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Fri Oct 7 15:49:23 2022 +0300 Merge branch 'master' into hup-reload commit 349dbe54fe27eeaf56776c73c3cc5649018d4c60 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Fri Oct 7 15:43:52 2022 +0300 next: imp docs, names commit 7287a86d283489127453009267911003cea5227e Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Fri Oct 7 13:39:44 2022 +0300 WIP all: impl dynamic reconfiguration
118 lines
2.7 KiB
Go
118 lines
2.7 KiB
Go
package cmd
|
|
|
|
import (
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
|
|
"github.com/AdguardTeam/AdGuardHome/internal/next/agh"
|
|
"github.com/AdguardTeam/AdGuardHome/internal/next/configmgr"
|
|
"github.com/AdguardTeam/golibs/log"
|
|
)
|
|
|
|
// signalHandler processes incoming signals and shuts services down.
|
|
type signalHandler struct {
|
|
// signal is the channel to which OS signals are sent.
|
|
signal chan os.Signal
|
|
|
|
// confFile is the path to the configuration file.
|
|
confFile string
|
|
|
|
// start is the time at which AdGuard Home has been started.
|
|
start time.Time
|
|
|
|
// services are the services that are shut down before application exiting.
|
|
services []agh.Service
|
|
}
|
|
|
|
// handle processes OS signals.
|
|
func (h *signalHandler) handle() {
|
|
defer log.OnPanic("signalHandler.handle")
|
|
|
|
for sig := range h.signal {
|
|
log.Info("sighdlr: received signal %q", sig)
|
|
|
|
if aghos.IsReconfigureSignal(sig) {
|
|
h.reconfigure()
|
|
} else if aghos.IsShutdownSignal(sig) {
|
|
status := h.shutdown()
|
|
log.Info("sighdlr: exiting with status %d", status)
|
|
|
|
os.Exit(status)
|
|
}
|
|
}
|
|
}
|
|
|
|
// reconfigure rereads the configuration file and updates and restarts services.
|
|
func (h *signalHandler) reconfigure() {
|
|
log.Info("sighdlr: reconfiguring adguard home")
|
|
|
|
status := h.shutdown()
|
|
if status != statusSuccess {
|
|
log.Info("sighdlr: reconfiruging: exiting with status %d", status)
|
|
|
|
os.Exit(status)
|
|
}
|
|
|
|
// TODO(a.garipov): This is a very rough way to do it. Some services can be
|
|
// reconfigured without the full shutdown, and the error handling is
|
|
// currently not the best.
|
|
|
|
confMgr, err := configmgr.New(h.confFile, h.start)
|
|
fatalOnError(err)
|
|
|
|
web := confMgr.Web()
|
|
err = web.Start()
|
|
fatalOnError(err)
|
|
|
|
dns := confMgr.DNS()
|
|
err = dns.Start()
|
|
fatalOnError(err)
|
|
|
|
h.services = []agh.Service{
|
|
dns,
|
|
web,
|
|
}
|
|
|
|
log.Info("sighdlr: successfully reconfigured adguard home")
|
|
}
|
|
|
|
// Exit status constants.
|
|
const (
|
|
statusSuccess = 0
|
|
statusError = 1
|
|
)
|
|
|
|
// shutdown gracefully shuts down all services.
|
|
func (h *signalHandler) shutdown() (status int) {
|
|
ctx, cancel := ctxWithDefaultTimeout()
|
|
defer cancel()
|
|
|
|
status = statusSuccess
|
|
|
|
log.Info("sighdlr: shutting down services")
|
|
for i, service := range h.services {
|
|
err := service.Shutdown(ctx)
|
|
if err != nil {
|
|
log.Error("sighdlr: shutting down service at index %d: %s", i, err)
|
|
status = statusError
|
|
}
|
|
}
|
|
|
|
return status
|
|
}
|
|
|
|
// newSignalHandler returns a new signalHandler that shuts down svcs.
|
|
func newSignalHandler(confFile string, start time.Time, svcs ...agh.Service) (h *signalHandler) {
|
|
h = &signalHandler{
|
|
signal: make(chan os.Signal, 1),
|
|
confFile: confFile,
|
|
start: start,
|
|
services: svcs,
|
|
}
|
|
|
|
aghos.NotifyShutdownSignal(h.signal)
|
|
aghos.NotifyReconfigureSignal(h.signal)
|
|
|
|
return h
|
|
}
|