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

service: simplify Stop() & improve errors #884

Merged
merged 3 commits into from
Apr 17, 2019
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion service/start_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func TestIntegrationStartDependency(t *testing.T) {
defer c.DeleteNetwork(service.namespace())
dep := service.Dependencies[0]
serviceID, err := dep.Start(c, service, networkID)
defer dep.Stop(c, service)
defer service.Stop(c)
require.NoError(t, err)
require.NotEqual(t, "", serviceID)
status, _ := dep.Status(c, service)
Expand Down
49 changes: 19 additions & 30 deletions service/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"sync"

"github.com/mesg-foundation/core/container"
"github.com/mesg-foundation/core/x/xerrors"
)

// Stop stops a service.
Expand All @@ -13,39 +14,27 @@ func (s *Service) Stop(c container.Container) error {
return err
}

if err := s.StopDependencies(c); err != nil {
return err
}
return c.DeleteNetwork(s.namespace())
}

// StopDependencies stops all dependencies.
func (s *Service) StopDependencies(c container.Container) error {
var mutex sync.Mutex
var wg sync.WaitGroup
var err error
stop := func(d *Dependency) {
defer wg.Done()
errStop := d.Stop(c, s)
mutex.Lock()
defer mutex.Unlock()
if errStop != nil && err == nil {
err = errStop
var (
wg sync.WaitGroup
errs xerrors.SyncErrors
)
for _, d := range append(s.Dependencies, s.Configuration) {
ilgooz marked this conversation as resolved.
Show resolved Hide resolved
// Service.Configuration can be nil so, here is a check for it.
if d == nil {
continue
}
}
if s.Configuration != nil {
wg.Add(1)
go stop(s.Configuration)
}
for _, dep := range s.Dependencies {
wg.Add(1)
go stop(dep)
go func(namespace []string) {
defer wg.Done()
if err := c.StopService(namespace); err != nil {
errs.Append(err)
}
}(d.namespace(s.namespace()))
}
wg.Wait()
return err
}
if err := errs.ErrorOrNil(); err != nil {
return err
}

// Stop stops a dependency.
func (d *Dependency) Stop(c container.Container, s *Service) error {
return c.StopService(d.namespace(s.namespace()))
return c.DeleteNetwork(s.namespace())
}
27 changes: 0 additions & 27 deletions service/stop_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package service
import (
"testing"

"github.com/mesg-foundation/core/container"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -52,32 +51,6 @@ func TestIntegrationStopNonRunningService(t *testing.T) {
require.Equal(t, STOPPED, status)
}

func TestIntegrationStopDependency(t *testing.T) {
var (
service = &Service{
Hash: "1",
Name: "TestStopDependency",
Dependencies: []*Dependency{
{
Key: "test",
Image: "http-server",
},
},
}
c = newIntegrationContainer(t)
)

networkID, err := c.CreateNetwork(service.namespace())
require.NoError(t, err)
defer c.DeleteNetwork(service.namespace())
dep := service.Dependencies[0]
dep.Start(c, service, networkID)
err = dep.Stop(c, service)
require.NoError(t, err)
status, _ := dep.Status(c, service)
require.Equal(t, container.STOPPED, status)
}

func TestIntegrationNetworkDeleted(t *testing.T) {
var (
service = &Service{
Expand Down
22 changes: 0 additions & 22 deletions service/stop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,3 @@ func TestStopRunningService(t *testing.T) {

mc.AssertExpectations(t)
}

func TestStopDependency(t *testing.T) {
var (
dependencyKey = "1"
s = &Service{
Hash: "1",
Name: "TestStopService",
Dependencies: []*Dependency{
{
Key: dependencyKey,
Image: "http-server",
},
},
}
mc = &mocks.Container{}
)

d, _ := s.getDependency(dependencyKey)
mc.On("StopService", d.namespace(s.namespace())).Once().Return(nil)
require.NoError(t, d.Stop(mc, s))
mc.AssertExpectations(t)
}