don't ignore errors when adding routes; improve logging

This commit is contained in:
Dan Laine 2020-06-24 13:27:10 -04:00
parent 3ca3a7377a
commit 4c75989056
1 changed files with 69 additions and 38 deletions

View File

@ -443,66 +443,83 @@ func (n *Node) initSharedMemory() {
// initKeystoreAPI initializes the keystore service // initKeystoreAPI initializes the keystore service
// Assumes n.APIServer is already set // Assumes n.APIServer is already set
func (n *Node) initKeystoreAPI() { func (n *Node) initKeystoreAPI() error {
n.Log.Info("initializing Keystore API") n.Log.Info("initializing keystore")
keystoreDB := prefixdb.New([]byte("keystore"), n.DB) keystoreDB := prefixdb.New([]byte("keystore"), n.DB)
n.keystoreServer.Initialize(n.Log, keystoreDB) n.keystoreServer.Initialize(n.Log, keystoreDB)
keystoreHandler := n.keystoreServer.CreateHandler() keystoreHandler := n.keystoreServer.CreateHandler()
if n.Config.KeystoreAPIEnabled { if !n.Config.KeystoreAPIEnabled {
n.APIServer.AddRoute(keystoreHandler, &sync.RWMutex{}, "keystore", "", n.HTTPLog) n.Log.Info("skipping keystore API initializaion because it has been disabled")
return nil
} }
n.Log.Info("initializing keystore API")
return n.APIServer.AddRoute(keystoreHandler, &sync.RWMutex{}, "keystore", "", n.HTTPLog)
} }
// initMetricsAPI initializes the Metrics API // initMetricsAPI initializes the Metrics API
// Assumes n.APIServer is already set // Assumes n.APIServer is already set
func (n *Node) initMetricsAPI() { func (n *Node) initMetricsAPI() error {
n.Log.Info("initializing Metrics API") n.Log.Info("initializing metrics")
registry, handler := metrics.NewService() registry, handler := metrics.NewService()
if n.Config.MetricsAPIEnabled { if n.Config.MetricsAPIEnabled {
n.APIServer.AddRoute(handler, &sync.RWMutex{}, "metrics", "", n.HTTPLog) n.Log.Info("initializing metrics API")
if err := n.APIServer.AddRoute(handler, &sync.RWMutex{}, "metrics", "", n.HTTPLog); err != nil {
return err
}
} else {
n.Log.Info("skipping metrics API initialization because it has been disabled")
} }
n.Config.ConsensusParams.Metrics = registry n.Config.ConsensusParams.Metrics = registry
return nil
} }
// initAdminAPI initializes the Admin API service // initAdminAPI initializes the Admin API service
// Assumes n.log, n.chainManager, and n.ValidatorAPI already initialized // Assumes n.log, n.chainManager, and n.ValidatorAPI already initialized
func (n *Node) initAdminAPI() { func (n *Node) initAdminAPI() error {
if n.Config.AdminAPIEnabled { if !n.Config.AdminAPIEnabled {
n.Log.Info("initializing Admin API") n.Log.Info("skipping admin API initializaion because it has been disabled")
service := admin.NewService(Version, n.ID, n.Config.NetworkID, n.Log, n.chainManager, n.Net, &n.APIServer) return nil
n.APIServer.AddRoute(service, &sync.RWMutex{}, "admin", "", n.HTTPLog)
} }
n.Log.Info("initializing admin API")
service := admin.NewService(Version, n.ID, n.Config.NetworkID, n.Log, n.chainManager, n.Net, &n.APIServer)
return n.APIServer.AddRoute(service, &sync.RWMutex{}, "admin", "", n.HTTPLog)
} }
func (n *Node) initInfoAPI() { func (n *Node) initInfoAPI() error {
if n.Config.InfoAPIEnabled { if !n.Config.InfoAPIEnabled {
n.Log.Info("initializing Info API") n.Log.Info("skipping info API initializaion because it has been disabled")
service := info.NewService(n.Log, Version, n.ID, n.Config.NetworkID, n.chainManager, n.Net) return nil
n.APIServer.AddRoute(service, &sync.RWMutex{}, "info", "", n.HTTPLog)
} }
n.Log.Info("initializing info API")
service := info.NewService(n.Log, Version, n.ID, n.Config.NetworkID, n.chainManager, n.Net)
return n.APIServer.AddRoute(service, &sync.RWMutex{}, "info", "", n.HTTPLog)
} }
// initHealthAPI initializes the Health API service // initHealthAPI initializes the Health API service
// Assumes n.Log, n.ConsensusAPI, and n.ValidatorAPI already initialized // Assumes n.Log, n.Net, n.APIServer, n.HTTPLog already initialized
func (n *Node) initHealthAPI() { func (n *Node) initHealthAPI() error {
if !n.Config.HealthAPIEnabled { if !n.Config.HealthAPIEnabled {
return n.Log.Info("skipping health API initializaion because it has been disabled")
return nil
} }
n.Log.Info("initializing Health API") n.Log.Info("initializing Health API")
service := health.NewService(n.Log) service := health.NewService(n.Log)
service.RegisterHeartbeat("network.validators.heartbeat", n.Net, 5*time.Minute) service.RegisterHeartbeat("network.validators.heartbeat", n.Net, 5*time.Minute)
n.APIServer.AddRoute(service.Handler(), &sync.RWMutex{}, "health", "", n.HTTPLog) return n.APIServer.AddRoute(service.Handler(), &sync.RWMutex{}, "health", "", n.HTTPLog)
} }
// initIPCAPI initializes the IPC API service // initIPCAPI initializes the IPC API service
// Assumes n.log and n.chainManager already initialized // Assumes n.log and n.chainManager already initialized
func (n *Node) initIPCAPI() { func (n *Node) initIPCAPI() error {
if n.Config.IPCEnabled { if !n.Config.IPCEnabled {
n.Log.Info("initializing IPC API") n.Log.Info("skipping ipc API initializaion because it has been disabled")
service := ipcs.NewService(n.Log, n.chainManager, n.DecisionDispatcher, &n.APIServer) return nil
n.APIServer.AddRoute(service, &sync.RWMutex{}, "ipcs", "", n.HTTPLog)
} }
n.Log.Info("initializing ipc API")
service := ipcs.NewService(n.Log, n.chainManager, n.DecisionDispatcher, &n.APIServer)
return n.APIServer.AddRoute(service, &sync.RWMutex{}, "ipcs", "", n.HTTPLog)
} }
// Give chains and VMs aliases as specified by the genesis information // Give chains and VMs aliases as specified by the genesis information
@ -561,9 +578,13 @@ func (n *Node) Initialize(Config *Config, logger logging.Logger, logFactory logg
n.initBeacons() n.initBeacons()
// Start HTTP APIs // Start HTTP APIs
n.initAPIServer() // Start the API Server n.initAPIServer() // Start the API Server
n.initKeystoreAPI() // Start the Keystore API if err := n.initKeystoreAPI(); err != nil { // Start the Keystore API
n.initMetricsAPI() // Start the Metrics API return fmt.Errorf("couldn't initialize keystore API: %w", err)
}
if err := n.initMetricsAPI(); err != nil { // Start the Metrics API
return fmt.Errorf("couldn't initialize metrics API: %w", err)
}
// initialize shared memory // initialize shared memory
n.initSharedMemory() n.initSharedMemory()
@ -579,15 +600,25 @@ func (n *Node) Initialize(Config *Config, logger logging.Logger, logFactory logg
n.initEventDispatcher() // Set up the event dipatcher n.initEventDispatcher() // Set up the event dipatcher
n.initChainManager() // Set up the chain manager n.initChainManager() // Set up the chain manager
n.initAdminAPI() // Start the Admin API if err := n.initAdminAPI(); err != nil { // Start the Admin API
n.initInfoAPI() // Start the Info API return fmt.Errorf("couldn't initialize admin API: %w", err)
n.initHealthAPI() // Start the Health API
n.initIPCAPI() // Start the IPC API
if err := n.initAliases(); err != nil { // Set up aliases
return err
} }
return n.initChains() // Start the Platform chain if err := n.initInfoAPI(); err != nil { // Start the Info API
return fmt.Errorf("couldn't initialize info API: %w", err)
}
if err := n.initHealthAPI(); err != nil { // Start the Health API
return fmt.Errorf("couldn't initialize health API: %w", err)
}
if err := n.initIPCAPI(); err != nil { // Start the IPC API
return fmt.Errorf("couldn't initialize ipc API: %w", err)
}
if err := n.initAliases(); err != nil { // Set up aliases
return fmt.Errorf("couldn't initialize aliases: %w", err)
}
if err := n.initChains(); err != nil { // Start the Platform chain
return fmt.Errorf("couldn't initialize chains: %w", err)
}
return nil
} }
// Shutdown this node // Shutdown this node