1
0
mirror of https://github.com/go-task/task.git synced 2025-01-20 04:59:37 +02:00

Use context together with errgroup

This will let other deps to be killed when one of the deps returns an
error.

Before this change, the process could keep running even after Task
exited.
This commit is contained in:
Andrey Nering 2017-04-12 20:53:41 -03:00
parent 2ec6b03022
commit c591ea4185
2 changed files with 13 additions and 12 deletions

20
task.go
View File

@ -77,20 +77,20 @@ func Run() {
}
for _, a := range args {
if err = RunTask(a); err != nil {
if err = RunTask(context.Background(), a); err != nil {
log.Fatal(err)
}
}
}
// RunTask runs a task by its name
func RunTask(name string) error {
func RunTask(ctx context.Context, name string) error {
t, ok := Tasks[name]
if !ok {
return &taskNotFoundError{name}
}
if err := t.runDeps(); err != nil {
if err := t.runDeps(ctx); err != nil {
return err
}
@ -100,20 +100,20 @@ func RunTask(name string) error {
}
for i := range t.Cmds {
if err := t.runCommand(i); err != nil {
if err := t.runCommand(ctx, i); err != nil {
return &taskRunError{name, err}
}
}
return nil
}
func (t *Task) runDeps() error {
func (t *Task) runDeps(ctx context.Context) error {
vars, err := t.handleVariables()
if err != nil {
return err
}
var g errgroup.Group
g, ctx := errgroup.WithContext(ctx)
for _, d := range t.Deps {
dep := d
@ -124,7 +124,7 @@ func (t *Task) runDeps() error {
return err
}
if err = RunTask(dep); err != nil {
if err = RunTask(ctx, dep); err != nil {
return err
}
return nil
@ -155,7 +155,7 @@ func (t *Task) isUpToDate() bool {
return generatesMinTime.After(sourcesMaxTime)
}
func (t *Task) runCommand(i int) error {
func (t *Task) runCommand(ctx context.Context, i int) error {
vars, err := t.handleVariables()
if err != nil {
return err
@ -167,7 +167,7 @@ func (t *Task) runCommand(i int) error {
if strings.HasPrefix(c, "^") {
c = strings.TrimPrefix(c, "^")
if err = RunTask(c); err != nil {
if err = RunTask(ctx, c); err != nil {
return err
}
return nil
@ -177,7 +177,7 @@ func (t *Task) runCommand(i int) error {
if err != nil {
return err
}
cmd := execext.NewCommand(context.Background(), c)
cmd := execext.NewCommand(ctx, c)
if dir != "" {
cmd.Dir = dir
}

View File

@ -1,6 +1,7 @@
package task
import (
"context"
"fmt"
"log"
"strings"
@ -16,7 +17,7 @@ func WatchTasks(args []string) error {
// run tasks on init
for _, a := range args {
if err := RunTask(a); err != nil {
if err := RunTask(context.Background(), a); err != nil {
fmt.Println(err)
break
}
@ -42,7 +43,7 @@ loop:
select {
case <-watcher.Events:
for _, a := range args {
if err := RunTask(a); err != nil {
if err := RunTask(context.Background(), a); err != nil {
fmt.Println(err)
continue loop
}