Skip to content

Commit

Permalink
Merge pull request #62 from evanlinjin/bug/hypervisor-#27
Browse files Browse the repository at this point in the history
Stop hypervisor StartApp from hanging.
  • Loading branch information
jdknives authored Nov 12, 2019
2 parents 503dd8d + a4e6b9a commit c10914c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
2 changes: 1 addition & 1 deletion pkg/visor/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func TestStartStopApp(t *testing.T) {

err = rpc.StopApp(&unknownApp, nil)
require.Error(t, err)
assert.Equal(t, ErrUnknownApp, err)
assert.Equal(t, ErrAppNotRunning, err)

require.NoError(t, rpc.StopApp(&app, nil))
time.Sleep(100 * time.Millisecond)
Expand Down
34 changes: 22 additions & 12 deletions pkg/visor/visor.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ const (
// ErrUnknownApp represents lookup error for App related calls.
var ErrUnknownApp = errors.New("unknown app")

// ErrAppNotRunning occurs when an app is attempted to be stopped when it was not running.
var ErrAppNotRunning = errors.New("app is not running")

// Version is the node version.
const Version = "0.0.1"

Expand Down Expand Up @@ -400,18 +403,25 @@ func (node *Node) Apps() []*AppState {

// StartApp starts registered App.
func (node *Node) StartApp(appName string) error {
for _, app := range node.appsConf {
if app.App == appName {
startCh := make(chan struct{})
go func(app AppConfig) {
if err := node.SpawnApp(&app, startCh); err != nil {
node.logger.Warnf("Failed to start app %s: %s", appName, err)
}
}(app)

<-startCh
return nil
for _, appC := range node.appsConf {
if appC.App != appName {
continue
}

done := make(chan struct{}, 1)
var err error

go func(appC AppConfig) {
if err = node.SpawnApp(&appC, done); err != nil {
done <- struct{}{}
node.logger.Warnf("Failed to start app %s: %s", appName, err)
}
}(appC)

<-done
close(done)

return err
}

return ErrUnknownApp
Expand Down Expand Up @@ -525,7 +535,7 @@ func (node *Node) StopApp(appName string) error {
node.startedMu.Unlock()

if bind == nil {
return ErrUnknownApp
return ErrAppNotRunning
}

return node.stopApp(appName, bind)
Expand Down

0 comments on commit c10914c

Please sign in to comment.