core: call app.Stop once

This commit is contained in:
Łukasz Magiera 2019-05-25 18:28:42 +02:00
parent 2192ed775d
commit 7cfb4aa2b4
2 changed files with 14 additions and 8 deletions

View File

@ -2,6 +2,7 @@ package core
import (
"context"
"sync"
"github.com/ipfs/go-metrics-interface"
"go.uber.org/fx"
@ -27,19 +28,26 @@ func NewNode(ctx context.Context, cfg *BuildCfg) (*IpfsNode, error) {
fx.Extract(n),
)
var once sync.Once
var stopErr error
n.stop = func() error {
once.Do(func() {
stopErr = app.Stop(context.Background())
})
return stopErr
}
n.IsOnline = cfg.Online
go func() {
// Note that some services use contexts to signal shutting down, which is
// very suboptimal. This needs to be here until that's addressed somehow
<-ctx.Done()
err := app.Stop(context.Background())
err := n.stop()
if err != nil {
log.Error("failure on stop: ", err)
}
}()
n.IsOnline = cfg.Online
n.app = app
if app.Err() != nil {
return nil, app.Err()
}

View File

@ -13,8 +13,6 @@ import (
"context"
"io"
"go.uber.org/fx"
version "github.com/ipfs/go-ipfs"
"github.com/ipfs/go-ipfs/core/bootstrap"
"github.com/ipfs/go-ipfs/core/node"
@ -107,7 +105,7 @@ type IpfsNode struct {
Process goprocess.Process
ctx context.Context
app *fx.App
stop func() error
// Flags
IsOnline bool `optional:"true"` // Online is set when networking is enabled.
@ -124,7 +122,7 @@ type Mounts struct {
// Close calls Close() on the App object
func (n *IpfsNode) Close() error {
return n.app.Stop(n.ctx)
return n.stop()
}
// Context returns the IpfsNode context