Skip to content

Commit

Permalink
Merge pull request #881 from mesg-foundation/refactoring/service/simp…
Browse files Browse the repository at this point in the history
…lify-deleteVolumes

service: simplify DeleteVolumes()
  • Loading branch information
NicolasMahe authored Apr 17, 2019
2 parents 1ac2359 + c15160b commit 2ebfd6c
Showing 1 changed file with 13 additions and 32 deletions.
45 changes: 13 additions & 32 deletions service/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,20 @@ func (s *Service) DeleteVolumes(c container.Container) error {
wg sync.WaitGroup
errs xerrors.SyncErrors
)
delete := func(d *Dependency) {
defer wg.Done()
if err := d.DeleteVolumes(c, s); err != nil {
errs.Append(err)
for _, d := range append(s.Dependencies, s.Configuration) {
// Service.Configuration can be nil so, here is a check for it.
if d == nil {
continue
}
for _, volume := range d.extractVolumes(s) {
wg.Add(1)
go func(source string) {
defer wg.Done()
if err := c.DeleteVolume(source); err != nil {
errs.Append(err)
}
}(volume.Source)
}
}
for _, d := range s.Dependencies {
wg.Add(1)
go delete(d)
}
if s.Configuration != nil {
wg.Add(1)
go delete(s.Configuration)
}
wg.Wait()
return errs.ErrorOrNil()
}

// DeleteVolumes deletes the data volumes of service's dependency.
func (d *Dependency) DeleteVolumes(c container.Container, s *Service) error {
volumes := d.extractVolumes(s)
var (
wg sync.WaitGroup
errs xerrors.SyncErrors
)
for _, mount := range volumes {
wg.Add(1)
go func(mount container.Mount) {
defer wg.Done()
if err := c.DeleteVolume(mount.Source); err != nil {
errs.Append(err)
}
}(mount)
}
wg.Wait()
return errs.ErrorOrNil()
Expand Down

0 comments on commit 2ebfd6c

Please sign in to comment.