-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
job.go
298 lines (266 loc) · 11.1 KB
/
job.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
package presenters
import (
"time"
"github.com/lib/pq"
uuid "github.com/satori/go.uuid"
"github.com/smartcontractkit/chainlink/core/assets"
clnull "github.com/smartcontractkit/chainlink/core/null"
"github.com/smartcontractkit/chainlink/core/services/job"
"github.com/smartcontractkit/chainlink/core/services/keystore/keys/ethkey"
"github.com/smartcontractkit/chainlink/core/services/keystore/keys/p2pkey"
"github.com/smartcontractkit/chainlink/core/services/pipeline"
"github.com/smartcontractkit/chainlink/core/services/signatures/secp256k1"
"github.com/smartcontractkit/chainlink/core/store/models"
)
// JobSpecType defines the the the spec type of the job
type JobSpecType string
func (t JobSpecType) String() string {
return string(t)
}
const (
DirectRequestJobSpec JobSpecType = "directrequest"
FluxMonitorJobSpec JobSpecType = "fluxmonitor"
OffChainReportingJobSpec JobSpecType = "offchainreporting"
KeeperJobSpec JobSpecType = "keeper"
CronJobSpec JobSpecType = "cron"
VRFJobSpec JobSpecType = "vrf"
WebhookJobSpec JobSpecType = "webhook"
)
// DirectRequestSpec defines the spec details of a DirectRequest Job
type DirectRequestSpec struct {
ContractAddress ethkey.EIP55Address `json:"contractAddress"`
MinIncomingConfirmations clnull.Uint32 `json:"minIncomingConfirmations"`
Initiator string `json:"initiator"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
// NewDirectRequestSpec initializes a new DirectRequestSpec from a
// job.DirectRequestSpec
func NewDirectRequestSpec(spec *job.DirectRequestSpec) *DirectRequestSpec {
return &DirectRequestSpec{
ContractAddress: spec.ContractAddress,
MinIncomingConfirmations: spec.MinIncomingConfirmations,
// This is hardcoded to runlog. When we support other intiators, we need
// to change this
Initiator: "runlog",
CreatedAt: spec.CreatedAt,
UpdatedAt: spec.UpdatedAt,
}
}
// FluxMonitorSpec defines the spec details of a FluxMonitor Job
type FluxMonitorSpec struct {
ContractAddress ethkey.EIP55Address `json:"contractAddress"`
Precision int32 `json:"precision"`
Threshold float32 `json:"threshold"`
AbsoluteThreshold float32 `json:"absoluteThreshold"`
PollTimerPeriod string `json:"pollTimerPeriod"`
PollTimerDisabled bool `json:"pollTimerDisabled"`
IdleTimerPeriod string `json:"idleTimerPeriod"`
IdleTimerDisabled bool `json:"idleTimerDisabled"`
MinPayment *assets.Link `json:"minPayment"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
// NewFluxMonitorSpec initializes a new DirectFluxMonitorSpec from a
// job.FluxMonitorSpec
func NewFluxMonitorSpec(spec *job.FluxMonitorSpec) *FluxMonitorSpec {
return &FluxMonitorSpec{
ContractAddress: spec.ContractAddress,
Precision: spec.Precision,
Threshold: spec.Threshold,
AbsoluteThreshold: spec.AbsoluteThreshold,
PollTimerPeriod: spec.PollTimerPeriod.String(),
PollTimerDisabled: spec.PollTimerDisabled,
IdleTimerPeriod: spec.IdleTimerPeriod.String(),
IdleTimerDisabled: spec.IdleTimerDisabled,
MinPayment: spec.MinPayment,
CreatedAt: spec.CreatedAt,
UpdatedAt: spec.UpdatedAt,
}
}
// OffChainReportingSpec defines the spec details of a OffChainReporting Job
type OffChainReportingSpec struct {
ContractAddress ethkey.EIP55Address `json:"contractAddress"`
P2PPeerID *p2pkey.PeerID `json:"p2pPeerID"`
P2PBootstrapPeers pq.StringArray `json:"p2pBootstrapPeers"`
IsBootstrapPeer bool `json:"isBootstrapPeer"`
EncryptedOCRKeyBundleID *models.Sha256Hash `json:"keyBundleID"`
TransmitterAddress *ethkey.EIP55Address `json:"transmitterAddress"`
ObservationTimeout models.Interval `json:"observationTimeout"`
BlockchainTimeout models.Interval `json:"blockchainTimeout"`
ContractConfigTrackerSubscribeInterval models.Interval `json:"contractConfigTrackerSubscribeInterval"`
ContractConfigTrackerPollInterval models.Interval `json:"contractConfigTrackerPollInterval"`
ContractConfigConfirmations uint16 `json:"contractConfigConfirmations"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
// NewOffChainReportingSpec initializes a new OffChainReportingSpec from a
// job.OffchainReportingOracleSpec
func NewOffChainReportingSpec(spec *job.OffchainReportingOracleSpec) *OffChainReportingSpec {
return &OffChainReportingSpec{
ContractAddress: spec.ContractAddress,
P2PPeerID: spec.P2PPeerID,
P2PBootstrapPeers: spec.P2PBootstrapPeers,
IsBootstrapPeer: spec.IsBootstrapPeer,
EncryptedOCRKeyBundleID: spec.EncryptedOCRKeyBundleID,
TransmitterAddress: spec.TransmitterAddress,
ObservationTimeout: spec.ObservationTimeout,
BlockchainTimeout: spec.BlockchainTimeout,
ContractConfigTrackerSubscribeInterval: spec.ContractConfigTrackerSubscribeInterval,
ContractConfigTrackerPollInterval: spec.ContractConfigTrackerPollInterval,
ContractConfigConfirmations: spec.ContractConfigConfirmations,
CreatedAt: spec.CreatedAt,
UpdatedAt: spec.UpdatedAt,
}
}
// PipelineSpec defines the spec details of the pipeline
type PipelineSpec struct {
ID int32 `json:"id"`
DotDAGSource string `json:"dotDagSource"`
}
// NewPipelineSpec generates a new PipelineSpec from a pipeline.Spec
func NewPipelineSpec(spec *pipeline.Spec) PipelineSpec {
return PipelineSpec{
ID: spec.ID,
DotDAGSource: spec.DotDagSource,
}
}
// KeeperSpec defines the spec details of a Keeper Job
type KeeperSpec struct {
ContractAddress ethkey.EIP55Address `json:"contractAddress"`
FromAddress ethkey.EIP55Address `json:"fromAddress"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
// NewKeeperSpec generates a new KeeperSpec from a job.KeeperSpec
func NewKeeperSpec(spec *job.KeeperSpec) *KeeperSpec {
return &KeeperSpec{
ContractAddress: spec.ContractAddress,
FromAddress: spec.FromAddress,
CreatedAt: spec.CreatedAt,
UpdatedAt: spec.UpdatedAt,
}
}
// WebhookSpec defines the spec details of a Webhook Job
type WebhookSpec struct {
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
// NewWebhookSpec generates a new WebhookSpec from a job.WebhookSpec
func NewWebhookSpec(spec *job.WebhookSpec) *WebhookSpec {
return &WebhookSpec{
CreatedAt: spec.CreatedAt,
UpdatedAt: spec.UpdatedAt,
}
}
// CronSpec defines the spec details of a Cron Job
type CronSpec struct {
CronSchedule string `json:"schedule" tom:"schedule"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
// NewCronSpec generates a new CronSpec from a job.CronSpec
func NewCronSpec(spec *job.CronSpec) *CronSpec {
return &CronSpec{
CronSchedule: spec.CronSchedule,
CreatedAt: spec.CreatedAt,
UpdatedAt: spec.UpdatedAt,
}
}
type VRFSpec struct {
CoordinatorAddress ethkey.EIP55Address `json:"coordinatorAddress"`
PublicKey secp256k1.PublicKey `json:"publicKey"`
Confirmations uint32 `json:"confirmations"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
func NewVRFSpec(spec *job.VRFSpec) *VRFSpec {
return &VRFSpec{
CoordinatorAddress: spec.CoordinatorAddress,
PublicKey: spec.PublicKey,
Confirmations: spec.Confirmations,
CreatedAt: spec.CreatedAt,
UpdatedAt: spec.UpdatedAt,
}
}
// JobError represents errors on the job
type JobError struct {
ID int64 `json:"id"`
Description string `json:"description"`
Occurrences uint `json:"occurrences"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
func NewJobError(e job.SpecError) JobError {
return JobError{
ID: e.ID,
Description: e.Description,
Occurrences: e.Occurrences,
CreatedAt: e.CreatedAt,
UpdatedAt: e.UpdatedAt,
}
}
// JobResource represents a JobResource
type JobResource struct {
JAID
Name string `json:"name"`
Type JobSpecType `json:"type"`
SchemaVersion uint32 `json:"schemaVersion"`
MaxTaskDuration models.Interval `json:"maxTaskDuration"`
ExternalJobID uuid.UUID `json:"externalJobID"`
DirectRequestSpec *DirectRequestSpec `json:"directRequestSpec"`
FluxMonitorSpec *FluxMonitorSpec `json:"fluxMonitorSpec"`
CronSpec *CronSpec `json:"cronSpec"`
OffChainReportingSpec *OffChainReportingSpec `json:"offChainReportingOracleSpec"`
KeeperSpec *KeeperSpec `json:"keeperSpec"`
VRFSpec *VRFSpec `json:"vrfSpec"`
WebhookSpec *WebhookSpec `json:"webhookSpec"`
PipelineSpec PipelineSpec `json:"pipelineSpec"`
Errors []JobError `json:"errors"`
}
// NewJobResource initializes a new JSONAPI job resource
func NewJobResource(j job.Job) *JobResource {
resource := &JobResource{
JAID: NewJAIDInt32(j.ID),
Name: j.Name.ValueOrZero(),
Type: JobSpecType(j.Type),
SchemaVersion: j.SchemaVersion,
MaxTaskDuration: j.MaxTaskDuration,
PipelineSpec: NewPipelineSpec(j.PipelineSpec),
ExternalJobID: j.ExternalJobID,
}
switch j.Type {
case job.DirectRequest:
resource.DirectRequestSpec = NewDirectRequestSpec(j.DirectRequestSpec)
case job.FluxMonitor:
resource.FluxMonitorSpec = NewFluxMonitorSpec(j.FluxMonitorSpec)
case job.Cron:
resource.CronSpec = NewCronSpec(j.CronSpec)
case job.OffchainReporting:
resource.OffChainReportingSpec = NewOffChainReportingSpec(j.OffchainreportingOracleSpec)
case job.Keeper:
resource.KeeperSpec = NewKeeperSpec(j.KeeperSpec)
case job.VRF:
resource.VRFSpec = NewVRFSpec(j.VRFSpec)
case job.Webhook:
resource.WebhookSpec = NewWebhookSpec(j.WebhookSpec)
}
jes := []JobError{}
for _, e := range j.JobSpecErrors {
jes = append(jes, NewJobError((e)))
}
resource.Errors = jes
return resource
}
// NewJobResources initializes a slice of JSONAPI job resources
func NewJobResources(js []job.Job) []JobResource {
rs := []JobResource{}
for _, j := range js {
rs = append(rs, *NewJobResource(j))
}
return rs
}
// GetName implements the api2go EntityNamer interface
func (r JobResource) GetName() string {
return "jobs"
}