Improve signal handling
This commit starts to wire up better signal handling, it starts with handling shutdown a bit better, using the graceful shutdown for all the listeners we use. It also adds the initial switch case for handling config and acl reload, which is to be implemented.
This commit is contained in:
parent
be25bbce92
commit
0b4f59b82b
1 changed files with 42 additions and 13 deletions
55
app.go
55
app.go
|
@ -568,19 +568,6 @@ func (h *Headscale) Serve() error {
|
||||||
return fmt.Errorf("failed change permission of gRPC socket: %w", err)
|
return fmt.Errorf("failed change permission of gRPC socket: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle common process-killing signals so we can gracefully shut down:
|
|
||||||
sigc := make(chan os.Signal, 1)
|
|
||||||
signal.Notify(sigc, os.Interrupt, syscall.SIGTERM)
|
|
||||||
go func(c chan os.Signal) {
|
|
||||||
// Wait for a SIGINT or SIGKILL:
|
|
||||||
sig := <-c
|
|
||||||
log.Printf("Caught signal %s: shutting down.", sig)
|
|
||||||
// Stop listening (and unlink the socket if unix type):
|
|
||||||
socketListener.Close()
|
|
||||||
// And we're done:
|
|
||||||
os.Exit(0)
|
|
||||||
}(sigc)
|
|
||||||
|
|
||||||
grpcGatewayMux := runtime.NewServeMux()
|
grpcGatewayMux := runtime.NewServeMux()
|
||||||
|
|
||||||
// Make the grpc-gateway connect to grpc over socket
|
// Make the grpc-gateway connect to grpc over socket
|
||||||
|
@ -725,6 +712,48 @@ func (h *Headscale) Serve() error {
|
||||||
log.Info().
|
log.Info().
|
||||||
Msgf("listening and serving metrics on: %s", h.cfg.MetricsAddr)
|
Msgf("listening and serving metrics on: %s", h.cfg.MetricsAddr)
|
||||||
|
|
||||||
|
// Handle common process-killing signals so we can gracefully shut down:
|
||||||
|
sigc := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(sigc,
|
||||||
|
syscall.SIGHUP,
|
||||||
|
syscall.SIGINT,
|
||||||
|
syscall.SIGTERM,
|
||||||
|
syscall.SIGQUIT,
|
||||||
|
syscall.SIGHUP)
|
||||||
|
go func(c chan os.Signal) {
|
||||||
|
// Wait for a SIGINT or SIGKILL:
|
||||||
|
sig := <-c
|
||||||
|
switch sig {
|
||||||
|
case syscall.SIGHUP:
|
||||||
|
log.Info().
|
||||||
|
Str("signal", sig.String()).
|
||||||
|
Msg("Received SIGHUP, reloading ACL and Config")
|
||||||
|
|
||||||
|
// TODO(kradalby): Reload config on SIGHUP
|
||||||
|
|
||||||
|
default:
|
||||||
|
log.Info().
|
||||||
|
Str("signal", sig.String()).
|
||||||
|
Msg("Received signal to stop, shutting down gracefully")
|
||||||
|
|
||||||
|
// Gracefully shut down servers
|
||||||
|
promHTTPServer.Shutdown(ctx)
|
||||||
|
httpServer.Shutdown(ctx)
|
||||||
|
grpcSocket.GracefulStop()
|
||||||
|
|
||||||
|
// Close network listeners
|
||||||
|
promHTTPListener.Close()
|
||||||
|
httpListener.Close()
|
||||||
|
grpcGatewayConn.Close()
|
||||||
|
|
||||||
|
// Stop listening (and unlink the socket if unix type):
|
||||||
|
socketListener.Close()
|
||||||
|
|
||||||
|
// And we're done:
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
}(sigc)
|
||||||
|
|
||||||
return errorGroup.Wait()
|
return errorGroup.Wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue