-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
run_saver.go
99 lines (87 loc) · 2.92 KB
/
run_saver.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
package ocrcommon
import (
"context"
"github.com/smartcontractkit/chainlink-common/pkg/services"
"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/services/pg"
"github.com/smartcontractkit/chainlink/v2/core/services/pipeline"
)
type Runner interface {
InsertFinishedRun(run *pipeline.Run, saveSuccessfulTaskRuns bool, qopts ...pg.QOpt) error
}
type RunResultSaver struct {
services.StateMachine
maxSuccessfulRuns uint64
runResults chan *pipeline.Run
pipelineRunner Runner
done chan struct{}
logger logger.Logger
}
func (r *RunResultSaver) HealthReport() map[string]error {
return map[string]error{r.Name(): r.Healthy()}
}
func (r *RunResultSaver) Name() string { return r.logger.Name() }
func NewResultRunSaver(pipelineRunner Runner,
logger logger.Logger, maxSuccessfulRuns uint64, resultsWriteDepth uint64,
) *RunResultSaver {
return &RunResultSaver{
maxSuccessfulRuns: maxSuccessfulRuns,
runResults: make(chan *pipeline.Run, resultsWriteDepth),
pipelineRunner: pipelineRunner,
done: make(chan struct{}),
logger: logger.Named("RunResultSaver"),
}
}
// Save sends the run on the internal `runResults` channel for saving.
// IMPORTANT: if the `runResults` pipeline is full, the run will be dropped.
func (r *RunResultSaver) Save(run *pipeline.Run) {
select {
case r.runResults <- run:
default:
r.logger.Warnw("RunSaver: the write queue was full, dropping run")
}
}
// Start starts RunResultSaver.
func (r *RunResultSaver) Start(context.Context) error {
return r.StartOnce("RunResultSaver", func() error {
go func() {
for {
select {
case run := <-r.runResults:
if !run.HasErrors() && r.maxSuccessfulRuns == 0 {
// optimisation: don't bother persisting it if we don't need to save successful runs
r.logger.Tracew("Skipping save of successful run due to MaxSuccessfulRuns=0", "run", run)
continue
}
r.logger.Tracew("RunSaver: saving job run", "run", run)
// We do not want save successful TaskRuns as OCR runs very frequently so a lot of records
// are produced and the successful TaskRuns do not provide value.
if err := r.pipelineRunner.InsertFinishedRun(run, false); err != nil {
r.logger.Errorw("error inserting finished results", "err", err)
}
case <-r.done:
return
}
}
}()
return nil
})
}
func (r *RunResultSaver) Close() error {
return r.StopOnce("RunResultSaver", func() error {
r.done <- struct{}{}
// In the unlikely event that there are remaining runResults to write,
// drain the channel and save them.
for {
select {
case run := <-r.runResults:
r.logger.Infow("RunSaver: saving job run before exiting", "run", run)
if err := r.pipelineRunner.InsertFinishedRun(run, false); err != nil {
r.logger.Errorw("error inserting finished results", "err", err)
}
default:
return nil
}
}
})
}