1
0
mirror of https://github.com/go-task/task.git synced 2025-08-10 22:42:19 +02:00

Only run task once for #53

This commit is contained in:
Ross Hammermeister
2020-08-17 13:25:17 -06:00
committed by Andrey Nering
parent a7b59e5b12
commit 97c85e39c3
12 changed files with 173 additions and 0 deletions

41
task.go
View File

@@ -58,6 +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
}
// Run runs Task
@@ -225,6 +227,10 @@ func (e *Executor) Setup() error {
}
}
if e.Taskfile.Run == "" {
e.Taskfile.Run = "always"
}
if v <= 2.1 {
err := errors.New(`task: Taskfile option "ignore_error" is only available starting on Taskfile version v2.1`)
@@ -260,6 +266,8 @@ func (e *Executor) Setup() error {
}
}
e.execution = 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))
for k := range e.Taskfile.Tasks {
@@ -286,6 +294,17 @@ 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
}
if err := e.runDeps(ctx, t); err != nil {
return err
}
@@ -445,3 +464,25 @@ func getEnviron(t *taskfile.Task) []string {
return environ
}
func (e *Executor) startExecution(innerCtx context.Context, t *taskfile.Task) (bool, context.Context, context.CancelFunc, error) {
h, err := e.GetHash(t)
if err != nil {
return true, nil, nil, err
}
if h == "" {
return true, innerCtx, func() {}, nil
}
e.executionMutex.Lock()
defer e.executionMutex.Unlock()
ctx, ok := e.execution[h]
if ok {
return false, ctx, func() {}, nil
}
ctx, cancel := context.WithCancel(innerCtx)
e.execution[h] = ctx
return true, ctx, cancel, nil
}