Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

services/serve: simplify concurrency #111

Merged
merged 2 commits into from
Aug 4, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}