From ff141a4efaa83ff467eb504f53a964d240438d49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?I=CC=87lker=20Go=CC=88ktug=CC=86=20O=CC=88ztu=CC=88rk?= Date: Tue, 16 Apr 2019 12:40:15 +0300 Subject: [PATCH] service: simplify DeleteVolumes() * moved Dependency.DeleteVolumes() logic into Service.DeleteVolumes(). --- service/volume.go | 45 +++++++++++++-------------------------------- 1 file changed, 13 insertions(+), 32 deletions(-) diff --git a/service/volume.go b/service/volume.go index 4087251b2..e09d6eb0c 100644 --- a/service/volume.go +++ b/service/volume.go @@ -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()