-
Notifications
You must be signed in to change notification settings - Fork 1
/
executor.go
475 lines (390 loc) · 9.98 KB
/
executor.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
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
// Copyright (c) 2016 Niklas Wolber
// This file is licensed under the MIT license.
// See the LICENSE file for more information.
package xCUTEr
import (
"bytes"
"context"
"fmt"
"io"
"log"
"sync"
"time"
"github.com/DataDog/datadog-go/statsd"
sched "github.com/nwolber/cron"
"github.com/nwolber/xCUTEr/flunc"
"github.com/nwolber/xCUTEr/job"
"github.com/nwolber/xCUTEr/telemetry"
)
type jobInfo struct {
file string
f flunc.Flunc
c *job.Config
telemetry bool
events *telemetry.EventStore
}
type schedInfo struct {
id string
j *jobInfo
}
func (info *schedInfo) Config() *job.Config {
return info.j.c
}
// runInfo holds information about a single run of a job.
type runInfo struct {
// The executor the job runs on.
e *executor
// jobInfo about the running job.
j *jobInfo
// Function to cancel the job early.
cancel context.CancelFunc
// Start and finish time of the job.
start, stop time.Time
// Job output.
output bytes.Buffer
}
// Config returns the running Config .
func (info *runInfo) Config() *job.Config {
return info.j.c
}
// Start time.
func (info *runInfo) Start() time.Time {
return info.start
}
// Finish time.
func (info *runInfo) Stop() time.Time {
return info.stop
}
// Job output.
func (info *runInfo) Output() string {
return info.output.String()
}
func (info *runInfo) run() {
ctx, cancel := context.WithCancel(info.e.mainCtx)
output, ok := ctx.Value(outputKey).(io.Writer)
if ok {
ctx = context.WithValue(ctx, outputKey, io.MultiWriter(output, &info.output))
} else {
// ctx = context.WithValue(ctx, "output", &info.output)
}
info.cancel = cancel
if !info.e.addRunning(info) {
log.Printf("another instance of %q is still running, consider adding/lowering the timeout", info.j.c.Name)
return
}
defer func() {
// release resources
cancel()
info.stop = time.Now()
if info.j.telemetry {
events := info.j.events.Reset()
info.e.sendTelemetry(info.j.c, &events)
}
info.e.removeRunning(info)
info.e.addComplete(info)
}()
info.start = time.Now()
_, err := info.j.f(ctx)
if err != nil {
log.Println(info.Config().Name, "ended with an error:", err)
}
}
type executor struct {
// The main context used to cancel all jobs at once.
mainCtx context.Context
// Whether jobs need to be activated manually.
manualActive bool
// Number of completed runInfos kept.
maxCompleted uint32
// Functions to start and stop the scheduler.
Start, Stop func()
// Function to run a runInfo.
run func(info *runInfo)
// Function to schedule a new runInfo.
schedule func(schedule string, f func()) (string, error)
// Function to remove an existing runInfo.
remove func(string)
// List of inactive scheduled jobs. Only used if manualActive is true.
inactive map[string]*schedInfo
mInactive sync.Mutex
// List of scheduled jobs.
scheduled map[string]*schedInfo
mSched sync.Mutex
// List of currently running jobs.
running map[string]*runInfo
mRun sync.Mutex
// List of completed runInfos. A maximum of maxCompleted runInfos is kept.
completed []*runInfo
mCompleted sync.Mutex
statsdClient *statsd.Client
}
func newExecutor(ctx context.Context, telemetryEndpoint string) (*executor, error) {
run := func(info *runInfo) { info.run() }
cron := sched.New()
e := &executor{
mainCtx: ctx,
maxCompleted: 10,
Start: cron.Start,
Stop: cron.Stop,
run: run,
schedule: cron.AddFunc,
remove: cron.Remove,
inactive: make(map[string]*schedInfo),
scheduled: make(map[string]*schedInfo),
running: make(map[string]*runInfo),
}
if telemetryEndpoint != "" {
var err error
e.statsdClient, err = statsd.New(telemetryEndpoint)
if err != nil {
return nil, err
}
e.statsdClient.Namespace = "xCUTEr."
go func(client *statsd.Client) {
<-ctx.Done()
client.Close()
}(e.statsdClient)
}
return e, nil
}
func (e *executor) sendTelemetry(c *job.Config, events *[]telemetry.Event) {
timing, err := telemetry.NewTiming(c)
if err != nil {
log.Println("Error creating timing:", err)
return
}
timing.ApplyStore(*events)
log.Println("sending telemetry data for job", c.Name)
if err := e.statsdClient.Timing(c.Name+".runtime", timing.JobRuntime, nil, 1.0); err != nil {
log.Println("error sending telemetry data for job", c.Name, err)
}
for host, stats := range timing.Hosts {
if err := e.statsdClient.Timing(fmt.Sprintf("%s.%s.runtime", c.Name, host.Name), stats.Runtime, nil, 1.0); err != nil {
log.Println("error sending telemetry data for job", c.Name, "host", host.Name, err)
}
}
}
// parse parses the given file and stores the execution tree in the jobInfo.
func (e *executor) parse(file string) (*jobInfo, error) {
c, err := job.ReadConfig(file)
if err != nil {
return nil, err
}
useTelemetry := c.Telemetry && e.statsdClient != nil
start := time.Now()
var (
f flunc.Flunc
events *telemetry.EventStore
)
if useTelemetry {
f, events, err = telemetry.Instrument(c)
} else {
f, err = c.ExecutionTree()
}
if err != nil {
return nil, err
}
stop := time.Now()
log.Println("job preparation took", stop.Sub(start))
return &jobInfo{
file: file,
c: c,
f: f,
telemetry: useTelemetry,
events: events,
}, nil
}
// scheduleBody returns a function that can be used by the cron
// scheduler to execute the Job.
func scheduleBody(e *executor, j *jobInfo) func() {
return func() {
log.Println(j.c.Name, "woke up")
info := &runInfo{
e: e,
j: j,
}
e.run(info)
log.Println(j.c.Name, "finished in", time.Now().Sub(info.start))
}
}
// Run either executes the job directly if either the job's schedule
// is "once" or the once parameter is true. Otherwise the job is
// scheduled as if Add would have been called.
func (e *executor) Run(j *jobInfo, once bool) {
if j.c.Schedule == "once" || once {
info := &runInfo{
e: e,
j: j,
}
e.run(info)
} else {
e.Add(j)
}
}
// Add schedules the job.
func (e *executor) Add(j *jobInfo) error {
if j.c.Schedule == "once" {
info := &runInfo{
e: e,
j: j,
}
go e.run(info)
} else {
if e.manualActive {
e.addInactive(&schedInfo{
j: j,
})
return nil
}
id, err := e.schedule(j.c.Schedule, scheduleBody(e, j))
if err != nil {
return err
}
s := &schedInfo{
id: id,
j: j,
}
log.Println(j.c.Name, "scheduled")
e.addScheduled(s)
}
return nil
}
// Remove removes all resources associated with the given job file.
func (e *executor) Remove(file string) {
log.Println("remove", file)
if info := e.isRunning(file); info != nil {
e.removeRunning(info)
info.cancel()
log.Println("found running", info.j.c.Name)
}
if info := e.isScheduled(file); info != nil {
e.removeScheduled(info)
e.remove(info.id)
log.Println("found scheduled", info.j.c.Name)
}
if info := e.isInactive(file); info != nil {
e.removeInactive(info)
log.Println("found inactive", info.j.c.Name)
}
}
// Activates the job associated with the job file.
func (e *executor) Activate(file string) {
log.Println("activate", file)
if info := e.isInactive(file); info != nil {
e.removeInactive(info)
e.schedule(info.j.c.Schedule, scheduleBody(e, info.j))
e.addScheduled(info)
} else {
log.Println("didn't find ", file, "in inactive list")
}
}
// Deactivates the job associated with the job file.
func (e *executor) Deactivate(file string) {
log.Println("deactivate", file)
if info := e.isScheduled(file); info != nil {
e.removeScheduled(info)
e.remove(info.id)
e.addInactive(info)
} else {
log.Println("didn't find ", file, "in active list")
}
}
// Start runs a job. If the return value is false,
// another instance of this job is still running.
func (e *executor) addRunning(info *runInfo) bool {
e.mRun.Lock()
defer e.mRun.Unlock()
if _, ok := e.running[info.j.file]; ok {
return false
}
e.running[info.j.file] = info
return true
}
// Stop halts execution of a job.
func (e *executor) removeRunning(info *runInfo) {
e.mRun.Lock()
defer e.mRun.Unlock()
delete(e.running, info.j.file)
}
// GetRunning returns a runInfo, if there is a running job.
func (e *executor) isRunning(file string) *runInfo {
e.mRun.Lock()
defer e.mRun.Unlock()
return e.running[file]
}
func (e *executor) GetRunning() []*runInfo {
e.mRun.Lock()
defer e.mRun.Unlock()
running := make([]*runInfo, len(e.running))
i := 0
for _, info := range e.running {
running[i] = info
i++
}
return running
}
func (e *executor) addComplete(info *runInfo) {
e.mCompleted.Lock()
defer e.mCompleted.Unlock()
e.completed = append(e.completed, info)
if l, max := len(e.completed), int(e.maxCompleted); max > 0 && max < l {
e.completed = e.completed[l-max:]
}
}
func (e *executor) GetCompleted() []*runInfo {
e.mCompleted.Lock()
defer e.mCompleted.Unlock()
completed := make([]*runInfo, len(e.completed))
copy(completed, e.completed)
return completed
}
func (e *executor) addScheduled(info *schedInfo) {
e.mSched.Lock()
defer e.mSched.Unlock()
e.scheduled[info.j.file] = info
}
func (e *executor) removeScheduled(info *schedInfo) {
e.mSched.Lock()
defer e.mSched.Unlock()
delete(e.scheduled, info.j.file)
}
func (e *executor) isScheduled(file string) *schedInfo {
e.mSched.Lock()
defer e.mSched.Unlock()
return e.scheduled[file]
}
func (e *executor) GetScheduled() []*schedInfo {
e.mSched.Lock()
defer e.mSched.Unlock()
scheduled := make([]*schedInfo, len(e.scheduled))
i := 0
for _, info := range e.scheduled {
scheduled[i] = info
}
return scheduled
}
func (e *executor) addInactive(info *schedInfo) {
e.mInactive.Lock()
defer e.mInactive.Unlock()
e.inactive[info.j.file] = info
}
func (e *executor) removeInactive(info *schedInfo) {
e.mInactive.Lock()
defer e.mInactive.Unlock()
delete(e.inactive, info.j.file)
}
func (e *executor) isInactive(file string) *schedInfo {
e.mInactive.Lock()
defer e.mInactive.Unlock()
return e.inactive[file]
}
func (e *executor) GetInactive() []*schedInfo {
e.mInactive.Lock()
defer e.mInactive.Unlock()
inactive := make([]*schedInfo, len(e.inactive))
i := 0
for _, info := range e.inactive {
inactive[i] = info
}
return inactive
}