Skip to content

Commit

Permalink
Ensure node.StartApp always returns.
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan Lin committed Nov 11, 2019
1 parent 8e3338e commit 78032f5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
3 changes: 2 additions & 1 deletion pkg/visor/rpc_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package visor

import (
"errors"
"fmt"
"net/http"
"os"
Expand Down Expand Up @@ -124,7 +125,7 @@ func TestStartStopApp(t *testing.T) {

err = rpc.StopApp(&unknownApp, nil)
require.Error(t, err)
assert.Equal(t, ErrUnknownApp, err)
assert.Equal(t, errors.New("app is either non-existent, or is already stopped"), err)

require.NoError(t, rpc.StopApp(&app, nil))
time.Sleep(100 * time.Millisecond)
Expand Down
29 changes: 18 additions & 11 deletions pkg/visor/visor.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,18 +400,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

0 comments on commit 78032f5

Please sign in to comment.