From 11ee36bb14b1d79d18fef91bddd345147ffea158 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Jochum?= Date: Sun, 25 Sep 2022 02:34:13 +0200 Subject: [PATCH] Add a Stop() function --- components.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/components.go b/components.go index 4da4c2e..75abe0a 100644 --- a/components.go +++ b/components.go @@ -153,6 +153,34 @@ func (c *Components) Init(context *cli.Context) error { return nil } +func (c *Components) Stop() error { + // Sort Components by Priority DESC + components := make([]Component, len(c.components)) + for _, com := range c.components { + components = append(components, com) + } + sort.SliceStable(components, func(i, j int) bool { + if components[i] == nil || components[j] == nil { + return false + } + + return components[i].Priority() > components[j].Priority() + }) + + // Init them sorted now + for _, component := range components { + if c == nil { + continue + } + + if err := component.Stop(); err != nil { + return err + } + } + + return nil +} + func (c *Components) Health(context context.Context) HealthInfoMap { result := make(HealthInfoMap, len(c.components))