forked from XimeraProject/xake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bake.go
122 lines (102 loc) · 2.57 KB
/
bake.go
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
package main
import (
"fmt"
"github.com/sirupsen/logrus"
"gopkg.in/cheggaaa/pb.v1"
"os"
"sync"
)
func Bake(workers int) error {
tasks := make(chan string)
queue := make(chan string)
log.Debug(fmt.Sprintf("Using %d workers", workers))
files, dependencies, err := NeedingCompilation(repository)
// BADBAD: need to display error from compilation if it fails
if err != nil {
return err
}
finished := make(chan string, len(files))
finishedCount := 0
var bar *pb.ProgressBar
if log.Level != logrus.DebugLevel {
bar = pb.StartNew(len(files))
//bar.SetMaxWidth(80)
bar.ShowTimeLeft = true
bar.Start()
}
// Manage a pool of workers
var group sync.WaitGroup
for i := 0; i < workers; i++ {
group.Add(1)
go func(workerId int) {
log.Debug(fmt.Sprintf("Worker %d is running", workerId))
for task := range tasks {
log.Debug(fmt.Sprintf("Worker %d is compiling %s", workerId, task))
_, err := Compile(repository, task)
if err != nil {
log.Error("Could not compile " + task)
os.Exit(1)
} else {
log.Debug(fmt.Sprintf("Worker %d finishes with %s", workerId, task))
finished <- task
finishedCount++
if log.Level != logrus.DebugLevel {
bar.Increment()
} else {
log.Info(fmt.Sprintf("Finished %d/%d tasks", finishedCount, len(files)))
}
}
}
group.Done()
}(i + 1)
}
// As things arrive in the queue, send them out to the various workers
go func() {
for file := range queue {
log.Debug("Sending " + file + " to workers")
tasks <- file
}
close(tasks)
}()
compiled := make(map[string]bool)
enqueued := make(map[string]bool)
enqueuedCount := 0
for {
// add everything that can be compiled given what has been completed
for _, file := range files {
if enqueued[file] {
continue
}
good := true
for _, dependency := range dependencies[file] {
if !compiled[dependency] {
good = false
break
}
}
if good {
log.Debug("Placing " + file + " into the queue")
queue <- file
enqueued[file] = true
enqueuedCount++
}
}
log.Debug(fmt.Sprintf("%d tasks have been placed in the queue.", enqueuedCount))
if enqueuedCount >= len(files) {
log.Debug("Everything is in the queue.")
break
}
log.Debug("Wait for things to finish before placing more into the queue.")
// get more finished things
compiled[<-finished] = true
}
close(queue)
log.Debug("Waiting for the workers to finish.")
group.Wait()
if log.Level != logrus.DebugLevel {
bar.FinishPrint("The xake is made.")
} else {
log.Info("The xake is made.")
}
return nil
}