Alias for compatibility until we properly refactor the constructor interface NewNode constructs and returns an IpfsNode using the given cfg.
(ctx context.Context, cfg *BuildCfg)
| 59 | |
| 60 | // NewNode constructs and returns an IpfsNode using the given cfg. |
| 61 | func NewNode(ctx context.Context, cfg *BuildCfg) (*IpfsNode, error) { |
| 62 | // save this context as the "lifetime" ctx. |
| 63 | lctx := ctx |
| 64 | |
| 65 | // derive a new context that ignores cancellations from the lifetime ctx. |
| 66 | ctx, cancel := context.WithCancel(valueContext{ctx}) |
| 67 | |
| 68 | // add a metrics scope. |
| 69 | ctx = metrics.CtxScope(ctx, "ipfs") |
| 70 | |
| 71 | n := &IpfsNode{ |
| 72 | ctx: ctx, |
| 73 | } |
| 74 | |
| 75 | opts := []fx.Option{ |
| 76 | node.IPFS(ctx, cfg), |
| 77 | fx.NopLogger, |
| 78 | } |
| 79 | for _, optFunc := range fxOptionFuncs { |
| 80 | var err error |
| 81 | opts, err = optFunc(FXNodeInfo{FXOptions: opts}) |
| 82 | if err != nil { |
| 83 | cancel() |
| 84 | return nil, fmt.Errorf("building fx opts: %w", err) |
| 85 | } |
| 86 | } |
| 87 | //nolint:staticcheck // https://github.com/ipfs/kubo/pull/9423#issuecomment-1341038770 |
| 88 | opts = append(opts, fx.Extract(n)) |
| 89 | |
| 90 | app := fx.New(opts...) |
| 91 | |
| 92 | var once sync.Once |
| 93 | var stopErr error |
| 94 | n.stop = func() error { |
| 95 | once.Do(func() { |
| 96 | // Bound app.Stop with a deadline so an FX OnStop hook that |
| 97 | // never returns cannot hang the daemon. ShutdownTimeout==0 |
| 98 | // opts out of the cap entirely and restores the legacy |
| 99 | // behavior of waiting forever for hooks to complete. The |
| 100 | // daemon's watchdog in cmd/ipfs/kubo/daemon.go fires at the |
| 101 | // same deadline and is the unconditional os.Exit fallback. |
| 102 | stopCtx := context.Background() |
| 103 | if cfg.ShutdownTimeout > 0 { |
| 104 | var stopCancel context.CancelFunc |
| 105 | stopCtx, stopCancel = context.WithTimeout(stopCtx, cfg.ShutdownTimeout) |
| 106 | defer stopCancel() |
| 107 | } |
| 108 | stopErr = app.Stop(stopCtx) |
| 109 | if stopErr != nil { |
| 110 | log.Errorf("failure on stop: %v", stopErr) |
| 111 | } |
| 112 | // Cancel the context _after_ the app has stopped. |
| 113 | cancel() |
| 114 | }) |
| 115 | return stopErr |
| 116 | } |
| 117 | n.IsOnline = cfg.Online |
| 118 |