-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
validate.go
90 lines (76 loc) · 2.52 KB
/
validate.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
package fluxmonitorv2
import (
"time"
"github.com/google/uuid"
"github.com/pelletier/go-toml"
"github.com/pkg/errors"
commonconfig "github.com/smartcontractkit/chainlink-common/pkg/config"
"github.com/smartcontractkit/chainlink/v2/core/services/job"
"github.com/smartcontractkit/chainlink/v2/core/utils"
)
type ValidationConfig interface {
DefaultHTTPTimeout() commonconfig.Duration
}
func ValidatedFluxMonitorSpec(config ValidationConfig, ts string) (job.Job, error) {
var jb = job.Job{
ExternalJobID: uuid.New(), // Default to generating a uuid, can be overwritten by the specified one in tomlString.
}
var spec job.FluxMonitorSpec
tree, err := toml.Load(ts)
if err != nil {
return jb, err
}
err = tree.Unmarshal(&jb)
if err != nil {
return jb, err
}
err = tree.Unmarshal(&spec)
if err != nil {
return jb, err
}
jb.FluxMonitorSpec = &spec
if jb.Type != job.FluxMonitor {
return jb, errors.Errorf("unsupported type %s", jb.Type)
}
// Find the smallest of all the timeouts
// and ensure the polling period is greater than that.
minTaskTimeout, aTimeoutSet, err := jb.Pipeline.MinTimeout()
if err != nil {
return jb, err
}
timeouts := []time.Duration{
config.DefaultHTTPTimeout().Duration(),
time.Duration(jb.MaxTaskDuration),
}
if aTimeoutSet {
timeouts = append(timeouts, minTaskTimeout)
}
var minTimeout time.Duration = 1<<63 - 1
for _, timeout := range timeouts {
if timeout < minTimeout {
minTimeout = timeout
}
}
if jb.FluxMonitorSpec.DrumbeatEnabled {
err := utils.ValidateCronSchedule(jb.FluxMonitorSpec.DrumbeatSchedule)
if err != nil {
return jb, errors.Wrap(err, "while validating drumbeat schedule")
}
if !spec.IdleTimerDisabled {
return jb, errors.Errorf("When the drumbeat ticker is enabled, the idle timer must be disabled. Please set IdleTimerDisabled to true")
}
}
if !validatePollTimer(jb.FluxMonitorSpec.PollTimerDisabled, minTimeout, jb.FluxMonitorSpec.PollTimerPeriod) {
return jb, errors.Errorf("PollTimerPeriod (%v) must be equal or greater than the smallest value of MaxTaskDuration param, JobPipeline.HTTPRequest.DefaultTimeout config var, or MinTimeout of all tasks (%v)", jb.FluxMonitorSpec.PollTimerPeriod, minTimeout)
}
return jb, nil
}
// validatePollTime validates the period is greater than the min timeout for an
// enabled poll timer.
func validatePollTimer(disabled bool, minTimeout time.Duration, period time.Duration) bool {
// Disabled timers do not need to validate the period
if disabled {
return true
}
return period >= minTimeout
}