Skip to content

Commit

Permalink
use reflect to get service type instead of hard coded index
Browse files Browse the repository at this point in the history
  • Loading branch information
jimjbrettj committed Mar 15, 2024
1 parent 1e62aeb commit 75accf7
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions lib/services/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package services

import (
"github.com/ChainSafe/gossamer/dot/state"
"github.com/ChainSafe/gossamer/dot/sync"
"reflect"
)

Expand Down Expand Up @@ -53,27 +55,26 @@ func (s *ServiceRegistry) StartAll() {
s.logger.Debug("All services started.")
}

// StopAll calls `Service.Stop()` for all registered services
func (s *ServiceRegistry) StopAll() {
s.logger.Infof("Stopping services: %v", s.serviceTypes)

// PauseServices pauses key services before shutdown to allow a graceful shutdown
func (s *ServiceRegistry) PauseServices() {
s.logger.Infof("Pausing key services")
// Pause the sync and state service to allow for graceful shutdown
// TODO make this not rely on a specific index
if len(s.serviceTypes) > 5 {
syncService := s.serviceTypes[5]
err := s.services[syncService].Pause()
if err != nil {
s.logger.Errorf("Error pausing service %s: %s", syncService, err)
for _, typ := range s.serviceTypes {
if (reflect.TypeOf(&sync.Service{}) == typ) || (reflect.TypeOf(&state.Service{}) == typ) {
err := s.services[typ].Pause()
if err != nil {
s.logger.Errorf("Error pausing service %s: %s", typ, err)
}
}
} else {
s.logger.Warnf("unable to pause sync service")
}
s.logger.Infof("Paused key services")
}

stateService := s.serviceTypes[len(s.serviceTypes)-1]
err := s.services[stateService].Pause()
if err != nil {
s.logger.Errorf("Error pausing service %s: %s", stateService, err)
}
// StopAll calls `Service.Stop()` for all registered services
func (s *ServiceRegistry) StopAll() {
s.logger.Infof("Stopping services: %v", s.serviceTypes)

s.PauseServices()

for _, typ := range s.serviceTypes {
s.logger.Debugf("Stopping service %s", typ)
Expand Down

0 comments on commit 75accf7

Please sign in to comment.