Skip to content

Commit

Permalink
Gracefully stop services on SIGTERM and SIGINT signals (#2239)
Browse files Browse the repository at this point in the history
  • Loading branch information
fridrik01 authored Dec 11, 2024
1 parent 44bcc69 commit 357fee0
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 3 deletions.
6 changes: 6 additions & 0 deletions beacon/blockchain/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,9 @@ func (s *Service[

return nil
}

func (s *Service[
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
]) Stop() error {
return nil
}
4 changes: 4 additions & 0 deletions beacon/validator/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,7 @@ func (s *Service[
) error {
return nil
}

func (s *Service[_, _, _, _, _, _, _, _, _, _, _, _, _, _]) Stop() error {
return nil
}
3 changes: 1 addition & 2 deletions consensus/cometbft/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,7 @@ func (s *Service[_]) Start(
return s.node.Start()
}

// Close is called in start cmd to gracefully cleanup resources.
func (s *Service[_]) Close() error {
func (s *Service[_]) Stop() error {
var errs []error

if s.node != nil && s.node.IsRunning() {
Expand Down
4 changes: 4 additions & 0 deletions execution/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ func (s *EngineClient[
}
}

func (s *EngineClient[_, _]) Stop() error {
return nil
}

func (s *EngineClient[_, _]) IsConnected() bool {
s.connectedMu.RLock()
defer s.connectedMu.RUnlock()
Expand Down
4 changes: 4 additions & 0 deletions node-api/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ func (s *Server[_]) start(ctx context.Context) {
}
}

func (s *Server[_]) Stop() error {
return nil
}

// Name returns the name of the API server service.
func (s *Server[_]) Name() string {
return "node-api-server"
Expand Down
17 changes: 16 additions & 1 deletion node-core/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,26 @@ func (n *node) Start(

// Start all the registered services.
if err := n.registry.StartAll(gctx); err != nil {
// Make sure the services that were successfully started are stopped
// before exiting. We assume that it is safe to call Stop on a
// service that was never started so we can call StopAll here
//#nosec:G703 // ok to ignore this
_ = n.registry.StopAll()
return err
}

// Wait for those aforementioned exit signals.
return g.Wait()
err := g.Wait()
if err != nil {
return err
}

// Stopp each service allowing them the exit gracefully.
if err = n.registry.StopAll(); err != nil {
return err
}

return nil
}

// listenForQuitSignals listens for SIGINT and SIGTERM. When a signal is
Expand Down
4 changes: 4 additions & 0 deletions node-core/services/registry/mocks/basic.mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions node-core/services/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ import (
type Basic interface {
// Start spawns any goroutines required by the service.
Start(ctx context.Context) error
// Stop stops the service. It should be safe to call
// Stop on a service that has not been started
Stop() error
// Name returns the name of the service.
Name() string
}
Expand Down Expand Up @@ -85,6 +88,23 @@ func (s *Registry) StartAll(ctx context.Context) error {
return nil
}

func (s *Registry) StopAll() error {
s.logger.Info("Stopping services", "num", len(s.serviceTypes))
for _, typeName := range s.serviceTypes {
s.logger.Info("Stopping service", "type", typeName)
svc := s.services[typeName]
if svc == nil {
s.logger.Error("service not found", "type", typeName)
continue
}

if err := svc.Stop(); err != nil {
return err
}
}
return nil
}

// RegisterService appends a service constructor function to the service
// registry.
func (s *Registry) RegisterService(service Basic) error {
Expand Down
4 changes: 4 additions & 0 deletions node-core/services/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ func (rs *ReportingService[_, _]) Start(ctx context.Context) error {
return nil
}

func (rs *ReportingService[_, _]) Stop() error {
return nil
}

func (rs *ReportingService[_, _]) printToConsole(
ethClient engineprimitives.ClientVersionV1) {
rs.logger.Info(fmt.Sprintf(`
Expand Down
4 changes: 4 additions & 0 deletions observability/telemetry/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,7 @@ func (s *Service) Name() string {
func (s *Service) Start(context.Context) error {
return nil
}

func (s *Service) Stop() error {
return nil
}

0 comments on commit 357fee0

Please sign in to comment.