-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.go
251 lines (221 loc) · 5.17 KB
/
scheduler.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
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
package main
import (
"bufio"
"context"
"fmt"
"os"
"os/exec"
"strings"
"time"
"github.com/brotherlogic/goserver/utils"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
type rCommand struct {
command *exec.Cmd
key string
output string
startTime int64
endTime int64
err error
mainOut string
status string
crash1 bool
crash2 bool
base string
block bool
comp chan bool
}
//Scheduler the main task scheduler
type Scheduler struct {
Log func(context.Context, string)
blockingQueue chan *rCommand
nonblockingQueue chan *rCommand
complete []*rCommand
}
func (s *Scheduler) getState(key string) string {
for _, c := range s.complete {
if c.key == key && c.endTime > 0 {
return fmt.Sprintf("%v -> %v", c.endTime, c.output)
}
}
return ""
}
// Schedule schedules a task
func (s *Scheduler) Schedule(c *rCommand) string {
ctx, cancel := utils.ManualContext("gbs-schedule", time.Minute)
defer cancel()
key := fmt.Sprintf("%v", time.Now().UnixNano())
s.complete = append(s.complete, c)
c.status = "InQueue"
c.key = key
c.comp = make(chan bool)
s.Log(ctx, fmt.Sprintf("running %+v with %v", c.command, c.block))
if c.block {
s.blockingQueue <- c
bqSize.Set(float64(len(s.blockingQueue)))
} else {
s.nonblockingQueue <- c
nbqSize.Set(float64(len(s.nonblockingQueue)))
}
return key
}
func (s *Scheduler) getOutput(key string) (string, error) {
for _, c := range s.complete {
if c.key == key {
return c.output, nil
}
}
return key, fmt.Errorf("KEY NOT_IN_MAP: %v", key)
}
func (s *Scheduler) getErrOutput(key string) (string, error) {
for _, c := range s.complete {
if c.key == key {
return c.mainOut, nil
}
}
return "", fmt.Errorf("KEY NOT_IN_MAP: %v", key)
}
func (s *Scheduler) wait(key string) {
for _, c := range s.complete {
if c.key == key {
<-c.comp
return
}
}
}
func (s *Scheduler) getStatus(key string) string {
for _, val := range s.complete {
if val.key == key {
return val.status
}
}
return fmt.Sprintf("KEY NOT_IN_MAP: %v", key)
}
func (s *Scheduler) killJob(key string) {
for _, val := range s.complete {
if val.key == key {
if val.command.Process != nil {
val.command.Process.Kill()
val.command.Process.Wait()
}
}
}
}
func (s *Scheduler) processBlockingCommands() {
for c := range s.blockingQueue {
bqSize.Set(float64(len(s.blockingQueue)))
err := s.run(c)
if err != nil {
c.endTime = time.Now().Unix()
}
}
}
func (s *Scheduler) processNonblockingCommands() {
for c := range s.nonblockingQueue {
ctx, cancel := utils.ManualContext("gbs-pnbc", time.Minute)
s.Log(ctx, fmt.Sprintf("Running Command: %+v", c))
nbqSize.Set(float64(len(s.nonblockingQueue)))
err := s.run(c)
if err != nil {
c.endTime = time.Now().Unix()
}
cancel()
}
}
var (
outputsize = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "gobuildslave_outputsize",
Help: "The size of the scheduler output",
}, []string{"job", "dest"})
nbqSize = promauto.NewGauge(prometheus.GaugeOpts{
Name: "gobuildslave_nbqsize",
Help: "The size of the scheduler output",
})
bqSize = promauto.NewGauge(prometheus.GaugeOpts{
Name: "gobuildslave_bqsize",
Help: "The size of the scheduler output",
})
)
func (s *Scheduler) run(c *rCommand) error {
c.status = "Running"
env := os.Environ()
home := ""
for _, s := range env {
if strings.HasPrefix(s, "HOME=") {
home = s[5:]
}
}
gpath := home + "/gobuild"
c.command.Path = strings.Replace(c.command.Path, "$GOPATH", gpath, -1)
for i := range c.command.Args {
c.command.Args[i] = strings.Replace(c.command.Args[i], "$GOPATH", gpath, -1)
}
path := fmt.Sprintf("GOPATH=" + home + "/gobuild")
pathbin := fmt.Sprintf("GOBIN=" + home + "/gobuild/bin")
found := false
for i, blah := range env {
if strings.HasPrefix(blah, "GOPATH") {
env[i] = path
found = true
}
if strings.HasPrefix(blah, "GOBIN") {
env[i] = pathbin
found = true
}
}
if !found {
env = append(env, path)
}
c.command.Env = env
out, err1 := c.command.StderrPipe()
outr, err2 := c.command.StdoutPipe()
if c.crash1 || err1 != nil {
return err1
}
if c.crash2 || err2 != nil {
return err2
}
scanner := bufio.NewScanner(out)
go func() {
for scanner != nil && scanner.Scan() {
c.output += scanner.Text()
outputsize.With(prometheus.Labels{"dest": "err", "job": c.command.Path}).Add(float64(len(scanner.Text())))
}
out.Close()
}()
scanner2 := bufio.NewScanner(outr)
go func() {
for scanner2 != nil && scanner2.Scan() {
c.mainOut += scanner2.Text()
outputsize.With(prometheus.Labels{"dest": "out", "job": c.command.Path}).Add(float64(len(scanner2.Text())))
}
outr.Close()
}()
c.status = "StartCommand"
err := c.command.Start()
if err != nil {
c.endTime = time.Now().Unix()
c.comp <- true
c.err = err
return err
}
c.startTime = time.Now().Unix()
// Monitor the job and report completion
r := func() {
c.status = "Entering Wait"
err := c.command.Wait()
c.status = "Completed Wait"
if err != nil {
c.err = err
}
c.endTime = time.Now().Unix()
c.comp <- true
}
if c.block {
r()
} else {
go r()
}
return nil
}