1
0
mirror of https://github.com/go-task/task.git synced 2025-05-31 23:19:42 +02:00

Cancel and restart long running process on file change by using context.WithCancel(..)

Closes #59
This commit is contained in:
Jack Mordaunt 2017-08-04 23:48:15 +08:00 committed by Andrey Nering
parent a2c96e9cdd
commit ddd063f29e

View File

@ -13,12 +13,22 @@ import (
func (e *Executor) watchTasks(args ...string) error {
e.printfln("task: Started watching for tasks: %s", strings.Join(args, ", "))
// run tasks on init
for _, a := range args {
if err := e.RunTask(context.Background(), Call{Task: a}); err != nil {
e.println(err)
break
var isCtxErr = func(err error) bool {
switch err {
case context.Canceled, context.DeadlineExceeded:
return true
}
return false
}
ctx, cancel := context.WithCancel(context.Background())
for _, a := range args {
a := a
go func() {
if err := e.RunTask(ctx, Call{Task: a}); err != nil && !isCtxErr(err) {
e.println(err)
}
}()
}
watcher, err := fsnotify.NewWatcher()
@ -36,19 +46,21 @@ func (e *Executor) watchTasks(args ...string) error {
}
}()
loop:
for {
select {
case <-watcher.Events:
cancel()
ctx, cancel = context.WithCancel(context.Background())
for _, a := range args {
if err := e.RunTask(context.Background(), Call{Task: a}); err != nil {
e.println(err)
continue loop
}
a := a
go func() {
if err := e.RunTask(ctx, Call{Task: a}); err != nil && !isCtxErr(err) {
e.println(err)
}
}()
}
case err := <-watcher.Errors:
e.println(err)
continue loop
}
}
}