mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-12 04:30:04 +00:00
83569f1342
Former-commit-id: 406ba2f1e65ec58e822fcf1b9d54c44ba51a559c [formerly 52aebe050663c4dc73fc56e5e4c6846620267f1f] Former-commit-id: c959a9fda119e4403136ac4f8d1b345d464ab5df
75 lines
1.6 KiB
Go
75 lines
1.6 KiB
Go
package node
|
|
|
|
import (
|
|
"os"
|
|
"os/signal"
|
|
"sync"
|
|
"syscall"
|
|
|
|
"github.com/prysmaticlabs/geth-sharding/shared"
|
|
log "github.com/sirupsen/logrus"
|
|
"github.com/urfave/cli"
|
|
)
|
|
|
|
// BeaconNode defines a struct that handles the services running a random beacon chain
|
|
// full PoS node. It handles the lifecycle of the entire system and registers
|
|
// services to a service registry.
|
|
type BeaconNode struct {
|
|
services *shared.ServiceRegistry
|
|
lock sync.RWMutex
|
|
stop chan struct{} // Channel to wait for termination notifications.
|
|
}
|
|
|
|
// New creates a new node instance, sets up configuration options, and registers
|
|
// every required service to the node.
|
|
func New(ctx *cli.Context) (*BeaconNode, error) {
|
|
registry := shared.NewServiceRegistry()
|
|
beacon := &BeaconNode{
|
|
services: registry,
|
|
stop: make(chan struct{}),
|
|
}
|
|
|
|
return beacon, nil
|
|
}
|
|
|
|
// Start the BeaconNode and kicks off every registered service.
|
|
func (b *BeaconNode) Start() {
|
|
b.lock.Lock()
|
|
|
|
log.Info("Starting beacon node")
|
|
|
|
b.services.StartAll()
|
|
|
|
stop := b.stop
|
|
b.lock.Unlock()
|
|
|
|
go func() {
|
|
sigc := make(chan os.Signal, 1)
|
|
signal.Notify(sigc, syscall.SIGINT, syscall.SIGTERM)
|
|
defer signal.Stop(sigc)
|
|
<-sigc
|
|
log.Info("Got interrupt, shutting down...")
|
|
go b.Close()
|
|
for i := 10; i > 0; i-- {
|
|
<-sigc
|
|
if i > 1 {
|
|
log.Info("Already shutting down, interrupt more to panic.", "times", i-1)
|
|
}
|
|
}
|
|
panic("Panic closing the beacon node")
|
|
}()
|
|
|
|
// Wait for stop channel to be closed.
|
|
<-stop
|
|
}
|
|
|
|
// Close handles graceful shutdown of the system.
|
|
func (b *BeaconNode) Close() {
|
|
b.lock.Lock()
|
|
defer b.lock.Unlock()
|
|
|
|
b.services.StopAll()
|
|
log.Info("Stopping beacon node")
|
|
close(b.stop)
|
|
}
|