1
0
mirror of https://github.com/rancher/norman.git synced 2025-08-31 06:35:09 +00:00
Files
norman/controller/starter.go

41 lines
845 B
Go
Raw Normal View History

2017-11-28 14:28:25 -07:00
package controller
import (
"context"
"golang.org/x/sync/errgroup"
)
type Starter interface {
Sync(ctx context.Context) error
Start(ctx context.Context, threadiness int) error
}
2017-12-19 21:39:57 -07:00
func SyncThenStart(ctx context.Context, threadiness int, starters ...Starter) error {
2017-11-28 18:58:13 -07:00
if err := Sync(ctx, starters...); err != nil {
return err
}
return Start(ctx, threadiness, starters...)
}
2017-11-28 14:28:25 -07:00
func Sync(ctx context.Context, starters ...Starter) error {
2017-11-28 18:24:53 -07:00
eg, _ := errgroup.WithContext(ctx)
2017-11-28 14:28:25 -07:00
for _, starter := range starters {
func(starter Starter) {
eg.Go(func() error {
return starter.Sync(ctx)
})
}(starter)
}
return eg.Wait()
}
func Start(ctx context.Context, threadiness int, starters ...Starter) error {
for _, starter := range starters {
if err := starter.Start(ctx, threadiness); err != nil {
return err
}
}
return nil
}