-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrun.go
More file actions
563 lines (492 loc) · 15.6 KB
/
run.go
File metadata and controls
563 lines (492 loc) · 15.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
package internal
import (
"context"
"errors"
"fmt"
"io"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"time"
"github.com/fsnotify/fsnotify"
"github.com/kitproj/kit/internal/proc"
"github.com/kitproj/kit/internal/types"
"github.com/kitproj/kit/internal/util"
"github.com/pkg/browser"
"k8s.io/utils/strings/slices"
)
var poisonPill = struct{}{}
func RunSubgraph(ctx context.Context, cancel context.CancelFunc, port int, openBrowser bool, logger *log.Logger, wf *types.Workflow, taskNames []string, tasksToSkip []string) error {
// check that the task names are valid
for _, name := range taskNames {
if _, ok := wf.Tasks[name]; !ok {
return fmt.Errorf("task %q not found in workflow", name)
}
}
// validate all tasks
for name, task := range wf.Tasks {
if err := task.Validate(); err != nil {
return fmt.Errorf("task %q is invalid: %w", name, err)
}
}
// check skipped tasks are valid
for _, name := range tasksToSkip {
if _, ok := wf.Tasks[name]; !ok {
return fmt.Errorf("skipped task %q not found in workflow", name)
}
}
// name is last part of pwd
pwd := os.Getenv("PWD")
name := filepath.Base(pwd)
dag := NewDAG[bool](name)
for name, t := range wf.Tasks {
dag.AddNode(name, true)
for _, dependency := range t.Dependencies {
dag.AddEdge(dependency, name)
}
}
visited := dag.Subgraph(taskNames)
taskByName := wf.Tasks
subgraph := NewDAG[*TaskNode](name)
for name := range visited {
task := taskByName[name]
logFile := filepath.Join("logs", fmt.Sprintf("%s.log", name))
if task.Log != "" {
logFile = task.Log
}
subgraph.AddNode(name, &TaskNode{
Name: name,
logFile: logFile,
Task: task,
Phase: "pending",
cancel: func() {},
mu: &sync.Mutex{}})
for _, parent := range dag.Parents[name] {
subgraph.AddEdge(parent, name)
}
}
events := make(chan any, len(subgraph.Nodes)*2)
// schedule the tasks in the subgraph that are ready to run , this is done by sending the task name to the events channel of any task that does not have any parents
for taskName := range subgraph.Nodes {
if len(subgraph.Parents[taskName]) == 0 {
events <- taskName
}
}
if len(subgraph.Nodes) == 0 {
logger.Println("no tasks to run")
return nil
}
// create logs directory
if err := os.MkdirAll("logs", 0755); err != nil && !errors.Is(err, os.ErrExist) {
return fmt.Errorf("failed to create logs directory: %w", err)
}
// start a file watcher for each task
for _, node := range subgraph.Nodes {
// start watching files for changes
watcher, err := fsnotify.NewWatcher()
if err != nil {
return fmt.Errorf("failed to create watcher: %w", err)
}
for _, source := range node.Task.Watch {
watchPath := filepath.Join(node.Task.WorkingDir, source)
info, err := os.Stat(watchPath)
if err != nil {
return fmt.Errorf("failed to stat %q: %w", source, err)
}
if info.IsDir() {
// Walk the directory tree to add the directory and all subdirectories
err := filepath.Walk(watchPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
if err := watcher.Add(path); err != nil {
return fmt.Errorf("failed to watch %q: %w", path, err)
}
}
return nil
})
if err != nil {
return fmt.Errorf("failed to walk %q: %w", source, err)
}
} else {
// It's a file, watch it directly
if err := watcher.Add(watchPath); err != nil {
return fmt.Errorf("failed to watch %q: %w", source, err)
}
}
}
defer watcher.Close()
go func() {
debounceTimer := time.AfterFunc(0, func() {})
defer debounceTimer.Stop()
for {
select {
case <-ctx.Done():
return
case event := <-watcher.Events:
// Handle file writes
if event.Op&fsnotify.Write == fsnotify.Write {
debounceTimer.Stop()
debounceTimer = time.AfterFunc(100*time.Millisecond, func() {
logger.Printf("[%s] %s changed, re-running\n", node.Name, event.Name)
events <- node.Name
})
}
// Handle new directories being created - add them to the watcher
if event.Op&fsnotify.Create == fsnotify.Create {
if info, err := os.Stat(event.Name); err == nil && info.IsDir() {
_ = watcher.Add(event.Name)
}
debounceTimer.Stop()
debounceTimer = time.AfterFunc(100*time.Millisecond, func() {
logger.Printf("[%s] %s created, re-running\n", node.Name, event.Name)
events <- node.Name
})
}
}
}
}()
}
semaphores := util.NewSemaphores(wf.Semaphores)
wg := &sync.WaitGroup{}
statusEvents := make(chan *TaskNode, 100)
if port > 0 {
go StartServer(ctx, port, wg, subgraph, statusEvents)
if openBrowser {
if err := browser.OpenURL(fmt.Sprintf("http://localhost:%d", port)); err != nil {
return fmt.Errorf("failed to open browser: %v", err)
}
}
}
stallTimers := map[string]*time.Timer{}
for name, taskNode := range subgraph.Nodes {
stalledTime := taskNode.Task.GetStalledTimeout()
stallTimers[name] = time.AfterFunc(stalledTime, func() {
if taskNode.Phase == "starting" || taskNode.Phase == "running" {
// we suffix the message with "starting" so we can differentiate between a task that is starting and one that is running, later on we can change the message to "output received"
// and restore the phase to "running" or "starting"
taskNode.Message = fmt.Sprintf("no output for %s or more while %s", stalledTime, taskNode.Phase)
taskNode.Phase = "stalled"
logger.Printf("[%s] %s\n", taskNode.Name, taskNode.Message)
statusEvents <- taskNode
}
})
}
allRunning := false
graphCompleted := false
for {
select {
case <-ctx.Done():
logger.Println("waiting for all tasks to complete")
wg.Wait()
// if any task failed, we will return an error
var failures []string
for _, node := range subgraph.Nodes {
color := 30
faint := 0
switch node.Phase {
case "failed":
// red
color = 31
faint = 1
failures = append(failures, node.Name)
case "pending", "waiting":
faint = 2
}
logger.Printf("\033[%d;%dm[%s] (%s) %s\033[0m\n", faint, color, node.Name, node.Phase, node.Message)
}
if len(failures) > 0 {
runLifecycleHook(context.Background(), types.Task{}, wf.Lifecycle.GetOnFailureHook(), os.Stdout, logger)
return fmt.Errorf("failed tasks: %v", failures)
}
if graphCompleted {
runLifecycleHook(context.Background(), types.Task{}, wf.Lifecycle.GetOnSuccessHook(), os.Stdout, logger)
}
return nil
case event := <-events:
switch x := event.(type) {
// if we get the poison pill, we should see if any job tasks are failed, if so we must exist
// if all jobs are either succeeded or skipped, we can exit
case struct{}:
remainingTasks := map[string]bool{}
pendingTasks := map[string]bool{}
for _, x := range taskNames {
remainingTasks[x] = true
pendingTasks[x] = true
}
for _, node := range subgraph.Nodes {
// Check if task should cause immediate exit
if node.Phase == "failed" && node.Task.GetRestartPolicy() == "Never" {
logger.Printf("🚫 exiting because task %q failed and should not be restarted", node.Name)
cancel()
continue
}
// Check if task is complete and should be removed from tracking
isComplete := false
switch node.Phase {
case "succeeded", "skipped":
isComplete = true
case "running", "stalled":
// Services are complete when running (ready to serve)
// Jobs are only complete when succeeded
isComplete = node.Task.GetType() == types.TaskTypeService
}
if isComplete {
delete(remainingTasks, node.Name)
if node.Task.GetRestartPolicy() != "Always" {
delete(pendingTasks, node.Name)
}
}
}
if len(pendingTasks) == 0 {
logger.Println("✅ exiting because all requested tasks completed and none should be restarted")
graphCompleted = true
cancel()
} else if len(remainingTasks) == 0 {
if !allRunning {
allRunning = true
logger.Println("🔵 all requested tasks are running:")
// print a list of running tasks, and their ports
for _, node := range subgraph.Nodes {
if (node.Phase == "running" || node.Phase == "stalled") && node.Task.Ports != nil {
for _, port := range node.Task.Ports {
logger.Printf(" - %s: http://localhost:%d\n", node.Name, port.HostPort)
}
}
}
}
} else {
allRunning = false
}
// if the event is a string, it is the name of the task to run
case string:
taskName := x
// we will only execute this task, if its parents are "succeeded" or "skipped" or ("running" and the task is a service)
blocked := false
for _, parentName := range subgraph.Parents[taskName] {
parent := subgraph.Nodes[parentName]
if parent.blocked() {
logger.Printf("task %q is blocked by %q (%s): %s\n", taskName, parentName, parent.Phase, parent.Message)
blocked = true
}
}
if blocked {
continue
}
// we might already be pending, waiting, starting or running this task, so we don't want to start it again
node := subgraph.Nodes[taskName]
node.cancel()
allRunning = false
// each task is executed in a separate goroutine
wg.Add(1)
go func(node *TaskNode) {
// lock the task, so we do not run two instances of it at the same time
node.mu.Lock()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
node.cancel = cancel
// send a poison pill to indicate that we've finish and the main loop must check to see if we need to exit
defer func() { events <- poisonPill }()
defer wg.Done()
defer node.mu.Unlock()
t := node.Task
var out io.Writer = &logWriter{
logger: logger,
prefixSuffixProvider: func() (string, string) {
return fmt.Sprintf("%s[%s] (%s) ", color(node.Name), node.Name, node.Phase), "\033[0m"
},
}
logger := log.New(out, "", 0)
setNodeStatus := func(node *TaskNode, phase string, message string) {
node.Phase = phase
node.Message = message
stallTimers[node.Name].Reset(node.Task.GetStalledTimeout())
logger.Println(node.Message)
statusEvents <- node
events <- poisonPill
}
setNodeStatus(node, "waiting", "")
queueChildren := func() {
for _, child := range subgraph.Children[node.Name] {
// only queue tasks in the subgraph
if _, ok := subgraph.Nodes[child]; ok {
logger.Printf("queuing %q\n", child)
events <- child
}
}
}
// if the task can be skipped, lets exit early
if t.Skip() || slices.Contains(tasksToSkip, node.Name) {
setNodeStatus(node, "skipped", "")
queueChildren()
return
}
// if the task needs a mutex, lets wait for it
if t.Mutex != "" {
mu := util.GetMutex(t.Mutex)
setNodeStatus(node, "waiting", "waiting for mutex")
mu.Lock()
setNodeStatus(node, "waiting", "acquired mutex")
defer mu.Unlock()
}
// if the task needs a semaphore, lets wait for it
if t.Semaphore != "" {
sema := semaphores.Get(t.Semaphore)
setNodeStatus(node, "waiting", "waiting for semaphore")
if err := sema.Acquire(ctx, 1); err != nil {
setNodeStatus(node, "failed", fmt.Sprintf("failed to acquire semaphore: %v", err))
return
}
setNodeStatus(node, "waiting", "acquired semaphore")
defer sema.Release(1)
}
p := proc.New(taskName, t, logger, types.Spec(*wf))
if probe := t.GetLivenessProbe(); probe != nil {
liveFunc := func(live bool, err error) {
if !live {
setNodeStatus(node, "failed", fmt.Sprintf("liveness probe failed: %v", err))
cancel()
}
}
go probeLoop(ctx, *probe, liveFunc)
}
if probe := t.GetReadinessProbe(); probe != nil {
readyFunc := func(ready bool, err error) {
if ready {
setNodeStatus(node, "running", "readiness probe succeeded")
queueChildren()
} else {
setNodeStatus(node, "failed", fmt.Sprintf("readiness probe failed: %v", err))
cancel()
}
}
go probeLoop(ctx, *probe, readyFunc)
}
if t.GetType() == types.TaskTypeService {
if t.Ports != nil {
setNodeStatus(node, "starting", "service starting")
} else {
setNodeStatus(node, "running", "no ports to expose")
queueChildren()
}
} else {
// non a service, must be a job
setNodeStatus(node, "running", "job running")
}
restart := func() {
select {
case <-ctx.Done():
case <-time.After(3 * time.Second):
logger.Println("restarting")
cancel()
events <- node.Name
}
}
file, err := os.Create(node.logFile)
if err != nil {
setNodeStatus(node, "failed", fmt.Sprintf("failed to create log file: %v", err))
return
}
defer file.Close()
// if the task has a log file, we will write to that file, we sync after each write
// so when we tail the log file, we see the output immediately
buf := funcWriter(func(p []byte) (int, error) {
stallTimers[node.Name].Reset(node.Task.GetStalledTimeout())
if node.Phase == "stalled" {
if strings.HasSuffix(node.Message, "starting") {
setNodeStatus(node, "starting", "output received")
} else {
setNodeStatus(node, "running", "output received")
}
}
n, err := file.Write(p)
if err != nil {
return n, err
}
if err := file.Sync(); err != nil {
return n, err
}
return n, nil
})
if t.Log != "" {
out = buf
} else {
out = io.MultiWriter(out, buf)
}
go func() {
ticker := time.NewTicker(20 * time.Second)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
if node.Phase != "running" && node.Phase != "stalled" {
continue
}
metrics, err := p.GetMetrics(ctx)
if err != nil {
logger.Printf("failed to get metrics: %v", err)
continue
}
node.Metrics = metrics
statusEvents <- node
}
}
}()
err = p.Run(ctx, out, out)
// if the task was cancelled, we don't want to restart it, this is normal exit
if errors.Is(ctx.Err(), context.Canceled) {
setNodeStatus(node, "cancelled", "")
return
}
if err != nil {
setNodeStatus(node, "failed", fmt.Sprint(err))
runLifecycleHook(ctx, t, t.GetOnFailureHook(), out, logger)
if t.GetRestartPolicy() != "Never" {
restart()
}
return
}
setNodeStatus(node, "succeeded", "")
runLifecycleHook(ctx, t, t.GetOnSuccessHook(), out, logger)
if t.GetRestartPolicy() == "Always" {
restart()
}
queueChildren()
}(node)
default:
panic(fmt.Sprintf("unexpected event: %v", event))
}
}
}
}
// runLifecycleHook runs the given lifecycle hook command, logging any errors.
// It is a best-effort operation: if the hook command fails, the error is logged
// but does not affect the task's outcome.
func runLifecycleHook(ctx context.Context, t types.Task, hook *types.LifecycleHook, out io.Writer, logger *log.Logger) {
if hook == nil {
return
}
cmd := hook.GetCommand()
if len(cmd) == 0 {
return
}
environ, err := types.Environ(types.Spec{}, t)
if err != nil {
logger.Printf("lifecycle hook: failed to get environment: %v", err)
return
}
c := exec.CommandContext(ctx, cmd[0], cmd[1:]...)
c.Dir = t.WorkingDir
c.Stdout = out
c.Stderr = out
c.Env = append(environ, os.Environ()...)
if err := c.Run(); err != nil {
logger.Printf("lifecycle hook failed: %v", err)
}
}