-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
job.go
104 lines (85 loc) · 2.69 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
package loader
import (
"context"
"errors"
"github.com/graph-gophers/dataloader"
uuid "github.com/satori/go.uuid"
"github.com/smartcontractkit/chainlink/core/services/chainlink"
"github.com/smartcontractkit/chainlink/core/services/job"
"github.com/smartcontractkit/chainlink/core/utils/stringutils"
)
type jobBatcher struct {
app chainlink.Application
}
func (b *jobBatcher) loadByExternalJobIDs(_ context.Context, keys dataloader.Keys) []*dataloader.Result {
// Create a map for remembering the order of keys passed in
keyOrder := make(map[string]int, len(keys))
// Collect the keys to search for
var jobIDs []uuid.UUID
for ix, key := range keys {
id, err := uuid.FromString(key.String())
if err == nil {
jobIDs = append(jobIDs, id)
}
keyOrder[key.String()] = ix
}
// Fetch the jobs
var jobs []job.Job
for _, id := range jobIDs {
job, err := b.app.JobORM().FindJobByExternalJobID(id)
if err != nil {
return []*dataloader.Result{{Data: nil, Error: err}}
}
jobs = append(jobs, job)
}
// Construct the output array of dataloader results
results := make([]*dataloader.Result, len(keys))
for _, j := range jobs {
id := j.ExternalJobID.String()
ix, ok := keyOrder[id]
// if found, remove from index lookup map, so we know elements were found
if ok {
results[ix] = &dataloader.Result{Data: j, Error: nil}
delete(keyOrder, id)
}
}
// fill array positions without any feeds managers
for _, ix := range keyOrder {
results[ix] = &dataloader.Result{Data: nil, Error: errors.New("feeds manager not found")}
}
return results
}
func (b *jobBatcher) loadByPipelineSpecIDs(_ context.Context, keys dataloader.Keys) []*dataloader.Result {
// Create a map for remembering the order of keys passed in
keyOrder := make(map[string]int, len(keys))
// Collect the keys to search for
var plSpecIDs []int32
for ix, key := range keys {
id, err := stringutils.ToInt32(key.String())
if err == nil {
plSpecIDs = append(plSpecIDs, id)
}
keyOrder[key.String()] = ix
}
// Fetch the jobs
jobs, err := b.app.JobORM().FindJobsByPipelineSpecIDs(plSpecIDs)
if err != nil {
return []*dataloader.Result{{Data: nil, Error: err}}
}
// Construct the output array of dataloader results
results := make([]*dataloader.Result, len(keys))
for _, j := range jobs {
id := stringutils.FromInt32(j.PipelineSpecID)
ix, ok := keyOrder[id]
// if found, remove from index lookup map, so we know elements were found
if ok {
results[ix] = &dataloader.Result{Data: j, Error: nil}
delete(keyOrder, id)
}
}
// fill array positions without any jobs
for _, ix := range keyOrder {
results[ix] = &dataloader.Result{Data: nil, Error: errors.New("job not found")}
}
return results
}