2018-05-27 07:02:29 -04:00
|
|
|
package task
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2018-05-27 08:42:53 -04:00
|
|
|
"v2ray.com/core/common/signal/semaphore"
|
2018-05-27 07:02:29 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type Task func() error
|
|
|
|
|
|
|
|
type executionContext struct {
|
|
|
|
ctx context.Context
|
2018-06-04 08:29:05 -04:00
|
|
|
tasks []Task
|
2018-05-27 07:02:29 -04:00
|
|
|
onSuccess Task
|
|
|
|
onFailure Task
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *executionContext) executeTask() error {
|
2018-06-04 08:29:05 -04:00
|
|
|
if len(c.tasks) == 0 {
|
2018-05-27 07:02:29 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-15 10:04:02 -04:00
|
|
|
// Reuse current goroutine if we only have one task to run.
|
|
|
|
if len(c.tasks) == 1 && c.ctx == nil {
|
|
|
|
return c.tasks[0]()
|
|
|
|
}
|
|
|
|
|
2018-06-04 08:29:05 -04:00
|
|
|
ctx := context.Background()
|
2018-05-27 07:02:29 -04:00
|
|
|
|
2018-06-04 08:29:05 -04:00
|
|
|
if c.ctx != nil {
|
|
|
|
ctx = c.ctx
|
2018-05-27 07:02:29 -04:00
|
|
|
}
|
|
|
|
|
2018-06-04 08:29:05 -04:00
|
|
|
return executeParallel(ctx, c.tasks)
|
2018-05-27 07:02:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *executionContext) run() error {
|
|
|
|
err := c.executeTask()
|
|
|
|
if err == nil && c.onSuccess != nil {
|
|
|
|
return c.onSuccess()
|
|
|
|
}
|
|
|
|
if err != nil && c.onFailure != nil {
|
|
|
|
return c.onFailure()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
type ExecutionOption func(*executionContext)
|
|
|
|
|
|
|
|
func WithContext(ctx context.Context) ExecutionOption {
|
|
|
|
return func(c *executionContext) {
|
|
|
|
c.ctx = ctx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Parallel(tasks ...Task) ExecutionOption {
|
|
|
|
return func(c *executionContext) {
|
2018-06-04 08:29:05 -04:00
|
|
|
c.tasks = append(c.tasks, tasks...)
|
2018-05-27 07:02:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Sequential(tasks ...Task) ExecutionOption {
|
|
|
|
return func(c *executionContext) {
|
2018-06-07 00:38:45 -04:00
|
|
|
if len(tasks) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(tasks) == 1 {
|
|
|
|
c.tasks = append(c.tasks, tasks[0])
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-04 08:29:05 -04:00
|
|
|
c.tasks = append(c.tasks, func() error {
|
2018-05-27 07:02:29 -04:00
|
|
|
return execute(tasks...)
|
2018-06-04 08:29:05 -04:00
|
|
|
})
|
2018-05-27 07:02:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func OnSuccess(task Task) ExecutionOption {
|
|
|
|
return func(c *executionContext) {
|
|
|
|
c.onSuccess = task
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func OnFailure(task Task) ExecutionOption {
|
|
|
|
return func(c *executionContext) {
|
|
|
|
c.onFailure = task
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Single(task Task, opts ExecutionOption) Task {
|
|
|
|
return Run(append([]ExecutionOption{Sequential(task)}, opts)...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Run(opts ...ExecutionOption) Task {
|
|
|
|
var c executionContext
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(&c)
|
|
|
|
}
|
|
|
|
return func() error {
|
|
|
|
return c.run()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// execute runs a list of tasks sequentially, returns the first error encountered or nil if all tasks pass.
|
|
|
|
func execute(tasks ...Task) error {
|
|
|
|
for _, task := range tasks {
|
|
|
|
if err := task(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// executeParallel executes a list of tasks asynchronously, returns the first error encountered or nil if all tasks pass.
|
2018-06-04 08:29:05 -04:00
|
|
|
func executeParallel(ctx context.Context, tasks []Task) error {
|
2018-05-27 07:02:29 -04:00
|
|
|
n := len(tasks)
|
2018-05-27 08:42:53 -04:00
|
|
|
s := semaphore.New(n)
|
2018-05-27 07:02:29 -04:00
|
|
|
done := make(chan error, 1)
|
|
|
|
|
|
|
|
for _, task := range tasks {
|
|
|
|
<-s.Wait()
|
|
|
|
go func(f func() error) {
|
|
|
|
if err := f(); err != nil {
|
|
|
|
select {
|
|
|
|
case done <- err:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.Signal()
|
|
|
|
}(task)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
select {
|
|
|
|
case err := <-done:
|
|
|
|
return err
|
2018-06-04 08:29:05 -04:00
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
2018-05-27 07:02:29 -04:00
|
|
|
case <-s.Wait():
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|