mirror of
https://github.com/edgelesssys/constellation.git
synced 2025-01-27 07:47:02 -05:00
260d2571c1
Co-authored-by: Daniel Weiße <66256922+daniel-weisse@users.noreply.github.com> Co-authored-by: 3u13r <lc@edgeless.systems>
51 lines
795 B
Go
51 lines
795 B
Go
package exit
|
|
|
|
import (
|
|
"sync"
|
|
)
|
|
|
|
type cleaner struct {
|
|
stoppers []stopper
|
|
|
|
cleanupDone bool
|
|
wg sync.WaitGroup
|
|
mux sync.Mutex
|
|
}
|
|
|
|
// New creates a new cleaner.
|
|
func New(stoppers ...stopper) *cleaner {
|
|
return &cleaner{
|
|
stoppers: stoppers,
|
|
}
|
|
}
|
|
|
|
// With adds a new stopper to the cleaner.
|
|
func (c *cleaner) With(stopper stopper) *cleaner {
|
|
c.stoppers = append(c.stoppers, stopper)
|
|
return c
|
|
}
|
|
|
|
// Clean stops all services gracefully.
|
|
func (c *cleaner) Clean() {
|
|
// only cleanup once
|
|
c.mux.Lock()
|
|
defer c.mux.Unlock()
|
|
if c.cleanupDone {
|
|
return
|
|
}
|
|
|
|
c.wg.Add(len(c.stoppers))
|
|
for _, stopItem := range c.stoppers {
|
|
go func(stopItem stopper) {
|
|
stopItem.Stop()
|
|
c.wg.Done()
|
|
}(stopItem)
|
|
}
|
|
c.wg.Wait()
|
|
c.cleanupDone = true
|
|
}
|
|
|
|
type stopper interface {
|
|
Stop()
|
|
}
|