added a notify system
This commit is contained in:
@@ -7,19 +7,22 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"git.haelnorr.com/h/golib/notify"
|
||||
"k8s.io/apimachinery/pkg/util/validation"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
GZIP bool
|
||||
server *http.Server
|
||||
logger *logger
|
||||
routes bool
|
||||
middleware bool
|
||||
errorPage ErrorPageFunc
|
||||
ready chan struct{}
|
||||
GZIP bool
|
||||
server *http.Server
|
||||
logger *logger
|
||||
routes bool
|
||||
middleware bool
|
||||
errorPage ErrorPageFunc
|
||||
ready chan struct{}
|
||||
notifier *Notifier
|
||||
shutdowndelay time.Duration
|
||||
}
|
||||
|
||||
// Ready returns a channel that is closed when the server is started
|
||||
@@ -83,10 +86,11 @@ func NewServer(config *Config) (*Server, error) {
|
||||
}
|
||||
|
||||
server := &Server{
|
||||
server: httpServer,
|
||||
routes: false,
|
||||
GZIP: config.GZIP,
|
||||
ready: make(chan struct{}),
|
||||
server: httpServer,
|
||||
routes: false,
|
||||
GZIP: config.GZIP,
|
||||
ready: make(chan struct{}),
|
||||
shutdowndelay: config.ShutdownDelay,
|
||||
}
|
||||
return server, nil
|
||||
}
|
||||
@@ -105,6 +109,8 @@ func (server *Server) Start(ctx context.Context) error {
|
||||
}
|
||||
}
|
||||
|
||||
server.startNotifier()
|
||||
|
||||
go func() {
|
||||
if server.logger == nil {
|
||||
fmt.Printf("Listening for requests on %s", server.server.Addr)
|
||||
@@ -126,6 +132,13 @@ func (server *Server) Start(ctx context.Context) error {
|
||||
}
|
||||
|
||||
func (server *Server) Shutdown(ctx context.Context) error {
|
||||
server.logger.logger.Debug().Dur("shutdown_delay", server.shutdowndelay).Msg("HWS Server shutting down")
|
||||
server.NotifyAll(notify.Notification{
|
||||
Title: "Shutting down",
|
||||
Message: fmt.Sprintf("Server is shutting down in %v", server.shutdowndelay),
|
||||
Level: LevelShutdown,
|
||||
})
|
||||
<-time.NewTimer(server.shutdowndelay).C
|
||||
if !server.IsReady() {
|
||||
return errors.New("Server isn't running")
|
||||
}
|
||||
@@ -136,6 +149,7 @@ func (server *Server) Shutdown(ctx context.Context) error {
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "Failed to shutdown the server gracefully")
|
||||
}
|
||||
server.closeNotifier()
|
||||
server.ready = make(chan struct{})
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user