1
0
mirror of https://github.com/go-task/task.git synced 2025-11-25 22:32:55 +02:00

Refactoring startExecution

This commit is contained in:
Ross Hammermeister
2021-07-28 14:39:00 -06:00
committed by Andrey Nering
parent 3e16ca37bc
commit d28649b13d

41
task.go
View File

@@ -58,8 +58,8 @@ type Executor struct {
concurrencySemaphore chan struct{}
taskCallCount map[string]*int32
mkdirMutexMap map[string]*sync.Mutex
execution map[string]context.Context
executionMutex sync.Mutex
executionHashes map[string]context.Context
executionHashesMutex sync.Mutex
}
// Run runs Task
@@ -278,7 +278,7 @@ func (e *Executor) Setup() error {
e.Taskfile.Run = "always"
}
e.execution = make(map[string]context.Context)
e.executionHashes = make(map[string]context.Context)
e.taskCallCount = make(map[string]*int32, len(e.Taskfile.Tasks))
e.mkdirMutexMap = make(map[string]*sync.Mutex, len(e.Taskfile.Tasks))
@@ -306,17 +306,7 @@ func (e *Executor) RunTask(ctx context.Context, call taskfile.Call) error {
release := e.acquireConcurrencyLimit()
defer release()
started, ctx, cancel, err := e.startExecution(ctx, t)
if err != nil {
return err
}
defer cancel()
if !started {
<-ctx.Done()
return nil
}
return e.startExecution(ctx, t, func(ctx context.Context) error {
if err := e.runDeps(ctx, t); err != nil {
return err
}
@@ -359,6 +349,7 @@ func (e *Executor) RunTask(ctx context.Context, call taskfile.Call) error {
}
}
return nil
})
}
func (e *Executor) mkdir(t *taskfile.Task) error {
@@ -477,24 +468,26 @@ func getEnviron(t *taskfile.Task) []string {
return environ
}
func (e *Executor) startExecution(innerCtx context.Context, t *taskfile.Task) (bool, context.Context, context.CancelFunc, error) {
func (e *Executor) startExecution(ctx context.Context, t *taskfile.Task, execute func(ctx context.Context) error) error {
h, err := e.GetHash(t)
if err != nil {
return true, nil, nil, err
return err
}
if h == "" {
return true, innerCtx, func() {}, nil
return execute(ctx)
}
e.executionMutex.Lock()
defer e.executionMutex.Unlock()
ctx, ok := e.execution[h]
e.executionHashesMutex.Lock()
otherExecutionCtx, ok := e.executionHashes[h]
e.executionHashesMutex.Unlock()
if ok {
return false, ctx, func() {}, nil
<-otherExecutionCtx.Done()
return nil
}
ctx, cancel := context.WithCancel(innerCtx)
e.execution[h] = ctx
return true, ctx, cancel, nil
ctx, cancel := context.WithCancel(ctx)
defer cancel()
e.executionHashes[h] = ctx
return execute(ctx)
}