-
Notifications
You must be signed in to change notification settings - Fork 0
/
job_run.go
56 lines (45 loc) · 1.37 KB
/
job_run.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
package loader
import (
"context"
"errors"
"github.com/graph-gophers/dataloader"
"github.com/O1MaGnUmO1/erinaceus-vrf/core/services/erinaceus"
"github.com/O1MaGnUmO1/erinaceus-vrf/core/utils/stringutils"
)
type jobRunBatcher struct {
app erinaceus.Application
}
func (b *jobRunBatcher) loadByIDs(_ 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 runIDs []int64
for ix, key := range keys {
id, err := stringutils.ToInt64(key.String())
if err == nil {
runIDs = append(runIDs, id)
}
keyOrder[key.String()] = ix
}
// Fetch the runs
runs, err := b.app.JobORM().FindPipelineRunsByIDs(runIDs)
if err != nil {
return []*dataloader.Result{{Data: nil, Error: err}}
}
// Construct the output array of dataloader results
results := make([]*dataloader.Result, len(keys))
for _, r := range runs {
idStr := stringutils.FromInt64(r.ID)
ix, ok := keyOrder[idStr]
// if found, remove from index lookup map, so we know elements were found
if ok {
results[ix] = &dataloader.Result{Data: r, Error: nil}
delete(keyOrder, idStr)
}
}
// fill array positions without any job runs
for _, ix := range keyOrder {
results[ix] = &dataloader.Result{Data: nil, Error: errors.New("run not found")}
}
return results
}