builder.go 2.02 KB
Newer Older
Jeromy's avatar
Jeromy committed
1 2 3
package core

import (
Jeromy's avatar
Jeromy committed
4
	"context"
Łukasz Magiera's avatar
Łukasz Magiera committed
5
	"sync"
Steven Allen's avatar
Steven Allen committed
6
	"time"
Jeromy's avatar
Jeromy committed
7

tavit ohanian's avatar
tavit ohanian committed
8 9
	"gitlab.dms3.io/dms3/go-dms3/core/bootstrap"
	"gitlab.dms3.io/dms3/go-dms3/core/node"
Michael Avila's avatar
Michael Avila committed
10

tavit ohanian's avatar
tavit ohanian committed
11
	"gitlab.dms3.io/dms3/go-metrics-interface"
12
	"go.uber.org/fx"
Łukasz Magiera's avatar
Łukasz Magiera committed
13
)
Jeromy's avatar
Jeromy committed
14

Steven Allen's avatar
Steven Allen committed
15 16 17 18 19 20 21 22 23
// from https://stackoverflow.com/a/59348871
type valueContext struct {
	context.Context
}

func (valueContext) Deadline() (deadline time.Time, ok bool) { return }
func (valueContext) Done() <-chan struct{}                   { return nil }
func (valueContext) Err() error                              { return nil }

24
type BuildCfg = node.BuildCfg // Alias for compatibility until we properly refactor the constructor interface
Jeromy's avatar
Jeromy committed
25

tavit ohanian's avatar
tavit ohanian committed
26 27
// NewNode constructs and returns an Dms3Node using the given cfg.
func NewNode(ctx context.Context, cfg *BuildCfg) (*Dms3Node, error) {
Steven Allen's avatar
Steven Allen committed
28 29 30 31 32 33 34
	// save this context as the "lifetime" ctx.
	lctx := ctx

	// derive a new context that ignores cancellations from the lifetime ctx.
	ctx, cancel := context.WithCancel(valueContext{ctx})

	// add a metrics scope.
tavit ohanian's avatar
tavit ohanian committed
35
	ctx = metrics.CtxScope(ctx, "dms3")
36

tavit ohanian's avatar
tavit ohanian committed
37
	n := &Dms3Node{
Łukasz Magiera's avatar
Łukasz Magiera committed
38 39
		ctx: ctx,
	}
40

Łukasz Magiera's avatar
Łukasz Magiera committed
41
	app := fx.New(
tavit ohanian's avatar
tavit ohanian committed
42
		node.DMS3(ctx, cfg),
43

44
		fx.NopLogger,
45 46 47
		fx.Extract(n),
	)

Łukasz Magiera's avatar
Łukasz Magiera committed
48 49 50 51 52
	var once sync.Once
	var stopErr error
	n.stop = func() error {
		once.Do(func() {
			stopErr = app.Stop(context.Background())
Steven Allen's avatar
Steven Allen committed
53 54 55 56 57
			if stopErr != nil {
				log.Error("failure on stop: ", stopErr)
			}
			// Cancel the context _after_ the app has stopped.
			cancel()
Łukasz Magiera's avatar
Łukasz Magiera committed
58 59 60 61 62
		})
		return stopErr
	}
	n.IsOnline = cfg.Online

63
	go func() {
Steven Allen's avatar
Steven Allen committed
64 65 66 67 68 69 70 71 72 73
		// Shut down the application if the lifetime context is canceled.
		// NOTE: we _should_ stop the application by calling `Close()`
		// on the process. But we currently manage everything with contexts.
		select {
		case <-lctx.Done():
			err := n.stop()
			if err != nil {
				log.Error("failure on stop: ", err)
			}
		case <-ctx.Done():
Steven Allen's avatar
Steven Allen committed
74
		}
75 76
	}()

Łukasz Magiera's avatar
Łukasz Magiera committed
77 78 79 80 81 82 83 84
	if app.Err() != nil {
		return nil, app.Err()
	}

	if err := app.Start(ctx); err != nil {
		return nil, err
	}

tavit ohanian's avatar
tavit ohanian committed
85
	// TODO: How soon will bootstrap move to p2p?
Łukasz Magiera's avatar
Łukasz Magiera committed
86 87 88
	if !cfg.Online {
		return n, nil
	}
Jeromy's avatar
Jeromy committed
89

Łukasz Magiera's avatar
Łukasz Magiera committed
90
	return n, n.Bootstrap(bootstrap.DefaultBootstrapConfig)
91
}