sing/common/task/task.go
2022-03-22 16:46:31 +08:00

36 lines
525 B
Go

package task
import (
"context"
"sync"
"sing/common"
)
func Run(ctx context.Context, tasks ...func() error) error {
ctx, cancel := context.WithCancel(ctx)
wg := new(sync.WaitGroup)
wg.Add(len(tasks))
var retErr error
for _, task := range tasks {
task := task
go func() {
if err := task(); err != nil {
if !common.Done(ctx) {
retErr = err
}
cancel()
}
wg.Done()
}()
}
go func() {
wg.Wait()
cancel()
}()
<-ctx.Done()
if retErr != nil {
return retErr
}
return ctx.Err()
}