Skip to content

Commit

Permalink
Merge pull request #2294 from rlan35/s3
Browse files Browse the repository at this point in the history
Make graceful shutdown more robust and faster
  • Loading branch information
Leo Chen authored Feb 22, 2020
2 parents ac4b6fc + d3dfd12 commit 51201d3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
37 changes: 18 additions & 19 deletions cmd/harmony/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,24 @@ func main() {

nodeConfig := createGlobalConfig()
currentNode := setupConsensusAndNode(nodeConfig)

// Prepare for graceful shutdown from os signals
osSignal := make(chan os.Signal)
signal.Notify(osSignal, os.Interrupt, syscall.SIGTERM)
go func() {
for {
select {
case sig := <-osSignal:
if sig == syscall.SIGTERM || sig == os.Interrupt {
msg := "Got %s signal. Gracefully shutting down...\n"
utils.Logger().Printf(msg, sig)
fmt.Printf(msg, sig)
currentNode.ShutDown()
}
}
}
}()

//setup state syncing and beacon syncing frequency
currentNode.SetSyncFreq(*syncFreq)
currentNode.SetBeaconSyncFreq(*beaconSyncFreq)
Expand Down Expand Up @@ -654,25 +672,6 @@ func main() {
if currentNode.NodeConfig.GetMetricsFlag() {
go currentNode.CollectMetrics()
}
// Prepare for graceful shutdown from os signals
osSignal := make(chan os.Signal)
signal.Notify(osSignal, os.Interrupt, os.Kill, syscall.SIGTERM)
go func() {
for {
select {
case sig := <-osSignal:
if sig == os.Kill || sig == syscall.SIGTERM {
fmt.Printf("Got %s signal. Gracefully shutting down...\n", sig)
currentNode.ShutDown()
}
if sig == os.Interrupt {
fmt.Printf("Got %s signal. Dumping state to DB...\n", sig)
currentNode.Blockchain().Stop()
currentNode.Beaconchain().Stop()
}
}
}
}()

currentNode.StartServer()
}
6 changes: 3 additions & 3 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -689,8 +689,8 @@ func (node *Node) SetBeaconSyncFreq(syncFreq int) {
func (node *Node) ShutDown() {
node.Blockchain().Stop()
node.Beaconchain().Stop()
node.StopServices()
node.stopHTTP()
fmt.Printf("Exiting node program...")
msg := "Successfully shut down!\n"
utils.Logger().Print(msg)
fmt.Print(msg)
os.Exit(0)
}

0 comments on commit 51201d3

Please sign in to comment.