-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
run_saver.go
84 lines (74 loc) · 2.43 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
package ocrcommon
import (
"context"
"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/services/pipeline"
"github.com/smartcontractkit/chainlink/v2/core/utils"
)
type RunResultSaver struct {
utils.StartStopOnce
maxSuccessfulRuns uint64
runResults <-chan *pipeline.Run
pipelineRunner pipeline.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(runResults <-chan *pipeline.Run, pipelineRunner pipeline.Runner, done chan struct{},
logger logger.Logger, maxSuccessfulRuns uint64,
) *RunResultSaver {
return &RunResultSaver{
maxSuccessfulRuns: maxSuccessfulRuns,
runResults: runResults,
pipelineRunner: pipelineRunner,
done: done,
logger: logger.Named("RunResultSaver"),
}
}
// 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
}
}
})
}