MCPcopy
hub / github.com/go-task/task / watchTasks

Method watchTasks

watch.go:29–145  ·  view source on GitHub ↗

watchTasks start watching the given tasks

(calls ...*Call)

Source from the content-addressed store, hash-verified

27
28// watchTasks start watching the given tasks
29func (e *Executor) watchTasks(calls ...*Call) error {
30 tasks := make([]string, len(calls))
31 for i, c := range calls {
32 tasks[i] = c.Task
33 }
34
35 e.Logger.Errf(logger.Green, "task: Started watching for tasks: %s\n", strings.Join(tasks, ", "))
36
37 ctx, cancel := context.WithCancel(context.Background())
38 for _, c := range calls {
39 go func() {
40 err := e.RunTask(ctx, c)
41 if err == nil {
42 e.Logger.Errf(logger.Green, "task: task \"%s\" finished running\n", c.Task)
43 } else if !isContextError(err) {
44 e.Logger.Errf(logger.Red, "%v\n", err)
45 }
46 }()
47 }
48
49 var waitTime time.Duration
50 switch {
51 case e.Interval != 0:
52 waitTime = e.Interval
53 case e.Taskfile.Interval != 0:
54 waitTime = e.Taskfile.Interval
55 default:
56 waitTime = defaultWaitTime
57 }
58
59 w, err := fsnotify.NewWatcher()
60 if err != nil {
61 cancel()
62 return err
63 }
64 defer w.Close()
65
66 deduper := fsnotifyext.NewDeduper(w, waitTime)
67 eventsChan := deduper.GetChan()
68
69 closeOnInterrupt(w)
70
71 go func() {
72 for {
73 select {
74 case event, ok := <-eventsChan:
75 if !ok {
76 cancel()
77 return
78 }
79 e.Logger.VerboseErrf(logger.Magenta, "task: received watch event: %v\n", event)
80
81 cancel()
82 ctx, cancel = context.WithCancel(context.Background())
83
84 e.Compiler.ResetCache()
85
86 for _, c := range calls {

Callers 1

RunMethod · 0.95

Calls 14

RunTaskMethod · 0.95
GetChanMethod · 0.95
GetTaskMethod · 0.95
collectSourcesMethod · 0.95
registerWatchedDirsMethod · 0.95
NewDeduperFunction · 0.92
SmartJoinFunction · 0.92
isContextErrorFunction · 0.85
closeOnInterruptFunction · 0.85
ShouldIgnoreFunction · 0.85
ErrfMethod · 0.80
CloseMethod · 0.80

Tested by

no test coverage detected