k3s/vendor/github.com/rancher/wrangler/pkg/start/all.go

41 lines
830 B
Go
Raw Normal View History

2019-05-09 22:03:45 +00:00
package start
2019-01-12 04:58:27 +00:00
import (
"context"
"golang.org/x/sync/errgroup"
)
type Starter interface {
Sync(ctx context.Context) error
Start(ctx context.Context, threadiness int) error
}
2019-05-09 22:03:45 +00:00
func All(ctx context.Context, threadiness int, starters ...Starter) error {
2019-01-12 04:58:27 +00:00
if err := Sync(ctx, starters...); err != nil {
return err
}
return Start(ctx, threadiness, starters...)
}
func Sync(ctx context.Context, starters ...Starter) error {
eg, _ := errgroup.WithContext(ctx)
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
}