Skip to content

Commit

Permalink
feat: restart node when handle errors and panics
Browse files Browse the repository at this point in the history
Signed-off-by: Godefroy Ponsinet <godefroy.ponsinet@outlook.com>
  • Loading branch information
90dy committed Nov 5, 2018
1 parent d5f3bde commit a7b9dc2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
Expand Up @@ -40,7 +40,6 @@ export default class List extends PureComponent {

render () {
const { navigation } = this.props
console.log(navigation)
const { restartDaemon } = this.state
if (restartDaemon) {
return (
Expand Down
23 changes: 21 additions & 2 deletions client/react-native/gomobile/core.go
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"strconv"
"strings"
"time"

account "berty.tech/core/manager/account"
reuse "github.com/libp2p/go-reuseport"
Expand Down Expand Up @@ -56,11 +57,11 @@ func GetPort() (int, error) {
return strconv.Atoi(strings.Split(currentAccount.GQLBind, ":")[1])
}

func Start(datastorePath string, logger Logger) error {
func Start(datastorePath string, loggerNative Logger) error {
if currentAccount != nil {
return errors.New("daemon already started")
}
if err := initLogger(logger); err != nil {
if err := initLogger(loggerNative); err != nil {
return err
}

Expand Down Expand Up @@ -114,6 +115,24 @@ func Start(datastorePath string, logger Logger) error {
return err
}

go func() {
err := <-currentAccount.ErrChan()
logger().Error("handle account error, try to restart", zap.Error(err))
for {
if currentAccount == nil {
err = Start(datastorePath, loggerNative)
} else {
err = Restart(datastorePath, loggerNative)
}
if err != nil {
logger().Error("restart error", zap.Error(err))
time.Sleep(time.Second)
continue
}
break
}
}()

return nil
}

Expand Down
15 changes: 15 additions & 0 deletions core/manager/account/account.go
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/jinzhu/gorm"
reuse "github.com/libp2p/go-reuseport"
"github.com/pkg/errors"
"go.uber.org/zap"
"google.golang.org/grpc"
)

Expand Down Expand Up @@ -169,10 +170,12 @@ func (a *Account) startGrpcServer() error {

a.grpcListener, err = reuse.Listen(addr.Network(), fmt.Sprintf("%s:%d", addr.IP.String(), addr.Port))
if err != nil {
defer a.panicHandler()
return err
}

go func() {
defer a.panicHandler()
a.errChan <- a.grpcServer.Serve(a.grpcListener)
}()

Expand All @@ -187,6 +190,7 @@ func (a *Account) startGQL() error {
}

go func() {
defer a.panicHandler()
a.errChan <- a.grpcServer.Serve(a.ioGrpc.Listener())
}()

Expand All @@ -210,6 +214,7 @@ func (a *Account) startGQL() error {

// start gql server
go func() {
defer a.panicHandler()
a.errChan <- http.Serve(a.gqlListener, a.gqlHandler)
}()
return nil
Expand Down Expand Up @@ -238,7 +243,9 @@ func (a *Account) initNode() error {
func (a *Account) startNode() error {
// start node
go func() {
defer a.panicHandler()
a.errChan <- a.node.Start()

}()

// show banner
Expand Down Expand Up @@ -282,3 +289,11 @@ func (a *Account) startNode() error {
func (a *Account) ErrChan() chan error {
return a.errChan
}

func (a *Account) panicHandler() {
if r := recover(); r != nil {
err := errors.New(fmt.Sprintf("%+v", r))
logger().Error("panic handler: panic received, send error to errChan", zap.Error(err))
a.errChan <- err
}
}

0 comments on commit a7b9dc2

Please sign in to comment.