From bb4b99ef4751bc9d9e4a4d423e2adc302a63edf0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Mon, 8 Apr 2019 15:57:20 +0200 Subject: [PATCH] Rename lcProcess.Run to Append, add docs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit License: MIT Signed-off-by: Ɓukasz Magiera --- core/node/helpers.go | 17 +++++++++++++---- core/node/ipns.go | 2 +- go.mod | 1 + 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/core/node/helpers.go b/core/node/helpers.go index bca088f70..4fced7fc6 100644 --- a/core/node/helpers.go +++ b/core/node/helpers.go @@ -6,6 +6,7 @@ import ( config "github.com/ipfs/go-ipfs-config" uio "github.com/ipfs/go-unixfs/io" "github.com/jbenet/goprocess" + "github.com/pkg/errors" "go.uber.org/fx" ) @@ -31,15 +32,23 @@ type lcProcess struct { Proc goprocess.Process } -func (lp *lcProcess) Run(f goprocess.ProcessFunc) { - proc := make(chan goprocess.Process, 1) +// Append wraps ProcessFunc into a goprocess, and appends it to the lifecycle +func (lp *lcProcess) Append(f goprocess.ProcessFunc) { + // Hooks are guaranteed to run in sequence. If a hook fails to start, its + // OnStop won't be executed. + var proc goprocess.Process + lp.LC.Append(fx.Hook{ OnStart: func(ctx context.Context) error { - proc <- lp.Proc.Go(f) + proc = lp.Proc.Go(f) return nil }, OnStop: func(ctx context.Context) error { - return (<-proc).Close() // todo: respect ctx, somehow + if proc == nil { // Theoretically this shouldn't ever happen + return errors.New("lcProcess: proc was nil") + } + + return proc.Close() // todo: respect ctx, somehow }, }) } diff --git a/core/node/ipns.go b/core/node/ipns.go index 1e8511d2c..afd6e678d 100644 --- a/core/node/ipns.go +++ b/core/node/ipns.go @@ -66,6 +66,6 @@ func IpnsRepublisher(lc lcProcess, cfg *config.Config, namesys namesys.NameSyste repub.RecordLifetime = d } - lc.Run(repub.Run) + lc.Append(repub.Run) return nil } diff --git a/go.mod b/go.mod index 21dbcd646..2c32a77f9 100644 --- a/go.mod +++ b/go.mod @@ -100,6 +100,7 @@ require ( github.com/multiformats/go-multibase v0.0.1 github.com/multiformats/go-multihash v0.0.1 github.com/opentracing/opentracing-go v1.0.2 + github.com/pkg/errors v0.8.1 github.com/prometheus/client_golang v0.9.2 github.com/syndtr/goleveldb v1.0.0 github.com/whyrusleeping/base32 v0.0.0-20170828182744-c30ac30633cc