Skip to content

Commit

Permalink
Merge pull request #598 from mesg-foundation/fix/service-start
Browse files Browse the repository at this point in the history
Start service dependencies one by one
  • Loading branch information
NicolasMahe authored Nov 29, 2018
2 parents 369e403 + 45927ad commit fcea30b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 25 deletions.
35 changes: 10 additions & 25 deletions service/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package service
import (
"strconv"
"strings"
"sync"

"github.com/mesg-foundation/core/config"
"github.com/mesg-foundation/core/container"
Expand All @@ -27,30 +26,16 @@ func (s *Service) Start() (serviceIDs []string, err error) {
if err != nil {
return nil, err
}
var (
mutex sync.Mutex
wg sync.WaitGroup
)
serviceIDs = make([]string, 0, len(s.Dependencies))
for i, dependency := range s.Dependencies {
wg.Add(1)
go func(dep *Dependency, i int) {
defer wg.Done()
serviceID, errStart := dep.Start(networkID)
mutex.Lock()
defer mutex.Unlock()
if errStart == nil {
serviceIDs = append(serviceIDs, serviceID)
}
if errStart != nil && err == nil {
err = errStart
}
}(dependency, i)
}
wg.Wait()
// Gracefully stop the service because there is an error
if err != nil {
s.Stop()
// BUG: https://github.com/mesg-foundation/core/issues/382
// After solving this by docker, switch back to deploy in parallel
serviceIDs = make([]string, len(s.Dependencies))
for i, dep := range s.Dependencies {
serviceID, err := dep.Start(networkID)
if err != nil {
s.Stop()
return nil, err
}
serviceIDs[i] = serviceID
}
return serviceIDs, err
}
Expand Down
21 changes: 21 additions & 0 deletions service/start_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,24 @@ func TestIntegrationServiceDependenciesListensFromSamePort(t *testing.T) {
require.NotZero(t, err)
require.Contains(t, err.Error(), `port '80' is already in use`)
}

func TestStartWithSamePorts(t *testing.T) {
c := newIntegrationContainer(t)
service, _ := FromService(&Service{
Name: "TestStartWithSamePorts",
Dependencies: []*Dependency{
{
Key: "1",
Image: "nginx",
Ports: []string{"80"},
},
{
Key: "2",
Image: "nginx",
Ports: []string{"80"},
},
},
}, ContainerOption(c))
_, err := service.Start()
require.Error(t, err)
}

0 comments on commit fcea30b

Please sign in to comment.