Wait for safe shutdown of NATS Server (#2289)

This commit is contained in:
Neil Alexander 2022-03-21 10:32:34 +00:00 committed by GitHub
parent 8336ce972e
commit 9572f5ed19
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 26 additions and 14 deletions

View file

@ -6,6 +6,7 @@ import (
"time"
"github.com/matrix-org/dendrite/setup/config"
"github.com/matrix-org/dendrite/setup/process"
"github.com/sirupsen/logrus"
natsserver "github.com/nats-io/nats-server/v2/server"
@ -15,7 +16,7 @@ import (
var natsServer *natsserver.Server
var natsServerMutex sync.Mutex
func Prepare(cfg *config.JetStream) (natsclient.JetStreamContext, *natsclient.Conn) {
func Prepare(process *process.ProcessContext, cfg *config.JetStream) (natsclient.JetStreamContext, *natsclient.Conn) {
// check if we need an in-process NATS Server
if len(cfg.Addresses) != 0 {
return setupNATS(cfg, nil)
@ -35,7 +36,16 @@ func Prepare(cfg *config.JetStream) (natsclient.JetStreamContext, *natsclient.Co
panic(err)
}
natsServer.ConfigureLogger()
go natsServer.Start()
go func() {
process.ComponentStarted()
natsServer.Start()
}()
go func() {
<-process.WaitForShutdown()
natsServer.Shutdown()
natsServer.WaitForShutdown()
process.ComponentFinished()
}()
}
natsServerMutex.Unlock()
if !natsServer.ReadyForConnections(time.Second * 10) {