2017-05-16 12:09:52 +00:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-05-25 13:14:52 +00:00
|
|
|
"os"
|
2017-09-25 18:22:57 +00:00
|
|
|
osSignal "os/signal"
|
2017-05-16 12:09:52 +00:00
|
|
|
|
|
|
|
"github.com/status-im/status-go/geth/common"
|
2017-08-10 13:35:58 +00:00
|
|
|
"github.com/status-im/status-go/geth/log"
|
2017-09-25 18:22:57 +00:00
|
|
|
"github.com/status-im/status-go/geth/signal"
|
2017-05-16 12:09:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// HaltOnPanic recovers from panic, logs issue, sends upward notification, and exits
|
|
|
|
func HaltOnPanic() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
err := fmt.Errorf("%v: %v", ErrNodeRunFailure, r)
|
|
|
|
|
|
|
|
// send signal up to native app
|
2017-09-25 18:22:57 +00:00
|
|
|
signal.Send(signal.Envelope{
|
|
|
|
Type: signal.EventNodeCrashed,
|
|
|
|
Event: signal.NodeCrashEvent{
|
2017-05-16 12:09:52 +00:00
|
|
|
Error: err.Error(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
common.Fatalf(err) // os.exit(1) is called internally
|
|
|
|
}
|
|
|
|
}
|
2017-05-25 13:14:52 +00:00
|
|
|
|
|
|
|
// HaltOnInterruptSignal stops node and panics if you press Ctrl-C enough times
|
|
|
|
func HaltOnInterruptSignal(nodeManager *NodeManager) {
|
|
|
|
sigc := make(chan os.Signal, 1)
|
2017-09-25 18:22:57 +00:00
|
|
|
osSignal.Notify(sigc, os.Interrupt)
|
|
|
|
defer osSignal.Stop(sigc)
|
2017-05-25 13:14:52 +00:00
|
|
|
<-sigc
|
|
|
|
if nodeManager.node == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Info("Got interrupt, shutting down...")
|
|
|
|
go nodeManager.node.Stop() // nolint: errcheck
|
|
|
|
for i := 3; i > 0; i-- {
|
|
|
|
<-sigc
|
|
|
|
if i > 1 {
|
|
|
|
log.Info(fmt.Sprintf("Already shutting down, interrupt %d more times for panic.", i-1))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
panic("interrupted!")
|
|
|
|
}
|