Skip to content

Commit

Permalink
services/serve: simplify concurrency (ignite#111)
Browse files Browse the repository at this point in the history
* services/serve: simplify concurrency

* cleanup.

* fix git version retrieval.

* fix app backend watch
  • Loading branch information
ilgooz authored Aug 4, 2020
1 parent 88f2787 commit e21b45f
Showing 1 changed file with 46 additions and 42 deletions.
88 changes: 46 additions & 42 deletions starport/services/serve/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"time"

"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/pkg/errors"
"github.com/tendermint/starport/starport/pkg/cmdrunner"
"github.com/tendermint/starport/starport/pkg/cmdrunner/step"
Expand Down Expand Up @@ -43,16 +42,19 @@ type version struct {
}

type starportServe struct {
app App
version version
verbose bool
app App
version version
verbose bool
serveCancel context.CancelFunc
serveRefresher chan struct{}
}

// Serve serves user apps.
func Serve(ctx context.Context, app App, verbose bool) error {
s := &starportServe{
app: app,
verbose: verbose,
app: app,
verbose: verbose,
serveRefresher: make(chan struct{}, 1),
}
v, err := s.appVersion()
if err != nil && err != git.ErrRepositoryNotExists {
Expand All @@ -67,43 +69,45 @@ func Serve(ctx context.Context, app App, verbose bool) error {
g.Go(func() error {
return s.runDevServer(ctx)
})

var (
serveCtx context.Context
serveCancel context.CancelFunc
serveErr = make(chan error, 1)
)
serve := func() {
if serveCancel != nil {
serveCancel()
}
serveCtx, serveCancel = context.WithCancel(ctx)
if err := s.serve(serveCtx); err != nil && err != context.Canceled {
serveErr <- err
}
}
go serve()

g.Go(func() error {
select {
case err := <-serveErr:
return err
case <-ctx.Done():
return ctx.Err()
s.refreshServe()
for {
select {
case <-ctx.Done():
return ctx.Err()

case <-s.serveRefresher:
var serveCtx context.Context
serveCtx, s.serveCancel = context.WithCancel(ctx)
if err := s.serve(serveCtx); err != nil && err != context.Canceled {
return err
}
}
}
})
g.Go(func() error {
return fswatcher.Watch(
ctx,
appBackendWatchPaths,
fswatcher.Workdir(app.Path),
fswatcher.OnChange(serve),
fswatcher.IgnoreHidden(),
)
return s.watchAppBackend(ctx)
})
return g.Wait()
}

func (s *starportServe) refreshServe() {
if s.serveCancel != nil {
s.serveCancel()
}
s.serveRefresher <- struct{}{}
}

func (s *starportServe) watchAppBackend(ctx context.Context) error {
return fswatcher.Watch(
ctx,
appBackendWatchPaths,
fswatcher.Workdir(s.app.Path),
fswatcher.OnChange(s.refreshServe),
fswatcher.IgnoreHidden(),
)
}

func (s *starportServe) serve(ctx context.Context) error {
var (
stdout = ioutil.Discard
Expand Down Expand Up @@ -195,7 +199,6 @@ func (s *starportServe) buildSteps() (steps step.Steps) {
var user struct {
Mnemonic string `json:"mnemonic"`
}
fmt.Println(string(mnemonic.Bytes()))
if err := json.Unmarshal(mnemonic.Bytes(), &user); err != nil {
return errors.Wrap(err, "cannot decode mnemonic")
}
Expand Down Expand Up @@ -320,10 +323,11 @@ func (s *starportServe) appVersion() (v version, err error) {
if err != nil {
return version{}, err
}
err = iter.ForEach(func(ref *plumbing.Reference) error {
v.tag = strings.TrimPrefix(ref.Name().Short(), "v")
v.hash = ref.Hash().String()
return nil
})
return
ref, err := iter.Next()
if err != nil {
return version{}, err
}
v.tag = strings.TrimPrefix(ref.Name().Short(), "v")
v.hash = ref.Hash().String()
return v, nil
}

0 comments on commit e21b45f

Please sign in to comment.