-
Notifications
You must be signed in to change notification settings - Fork 24
/
feed_group.go
153 lines (138 loc) · 3.75 KB
/
feed_group.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
package scheduler
import (
"context"
"encoding/json"
"errors"
"time"
log "github.com/sirupsen/logrus"
"github.com/ossf/package-feeds/pkg/feeds"
"github.com/ossf/package-feeds/pkg/publisher"
)
var (
errPoll = errors.New("error when polling for packages")
errPub = errors.New("error when publishing packages")
)
type feedEntry struct {
feed feeds.ScheduledFeed
lastPoll time.Time
}
type FeedGroup struct {
feeds []*feedEntry
publisher publisher.Publisher
initialCutoff time.Time
}
type groupResult struct {
numPublished int
pollErr error
pubErr error
}
//nolint:lll
func NewFeedGroup(scheduledFeeds []feeds.ScheduledFeed, pub publisher.Publisher, initialCutoff time.Duration) *FeedGroup {
fg := &FeedGroup{
publisher: pub,
initialCutoff: time.Now().UTC().Add(-initialCutoff),
feeds: make([]*feedEntry, 0),
}
for _, feed := range scheduledFeeds {
fg.AddFeed(feed)
}
return fg
}
func (fg *FeedGroup) AddFeed(feed feeds.ScheduledFeed) {
fg.feeds = append(fg.feeds, &feedEntry{
feed: feed,
lastPoll: fg.initialCutoff,
})
}
func (fg *FeedGroup) Run() {
result := fg.pollAndPublish()
if result.pollErr != nil {
log.Error(result.pollErr)
}
if result.pubErr != nil {
log.Error(result.pubErr)
}
}
func (fg *FeedGroup) pollAndPublish() groupResult {
result := groupResult{}
pkgs, err := fg.poll()
result.pollErr = err
// Return early if no packages to process
if len(pkgs) == 0 {
return result
}
log.WithField("num_packages", len(pkgs)).Printf("Publishing packages...")
result.numPublished, result.pubErr = fg.publishPackages(pkgs)
if result.numPublished > 0 {
log.WithField("num_packages", result.numPublished).Printf("Successfully published packages")
}
return result
}
// Poll fetches the latest packages from each registered feed.
func (fg *FeedGroup) poll() ([]*feeds.Package, error) {
results := make(chan pollResult, len(fg.feeds))
for _, f := range fg.feeds {
go func(f *feedEntry) {
result := pollResult{
name: f.feed.GetName(),
feed: f.feed,
}
result.packages, f.lastPoll, result.errs = f.feed.Latest(f.lastPoll)
results <- result
}(f)
}
errs := []error{}
packages := []*feeds.Package{}
for i := 0; i < len(fg.feeds); i++ {
result := <-results
logger := log.WithField("feed", result.name)
for _, err := range result.errs {
logger.WithError(err).Error("Error fetching packages")
errs = append(errs, err)
}
for _, pkg := range result.packages {
log.WithFields(log.Fields{
"feed": result.name,
"name": pkg.Name,
"version": pkg.Version,
}).Print("Processing Package")
}
packages = append(packages, result.packages...)
logger.WithField("num_processed", len(result.packages)).Print("Packages successfully processed")
}
err := errPoll
if len(errs) == 0 {
err = nil
}
log.WithField("time", time.Now().UTC()).Printf("%d packages processed", len(packages))
return packages, err
}
func (fg *FeedGroup) publishPackages(pkgs []*feeds.Package) (int, error) {
processed := 0
errs := []error{}
for _, pkg := range pkgs {
log.WithFields(log.Fields{
"name": pkg.Name,
"feed": pkg.Type,
"created_date": pkg.CreatedDate,
}).Print("Sending package upstream")
b, err := json.Marshal(pkg)
if err != nil {
log.WithField("name", pkg.Name).WithError(err).Error("Error marshaling package")
errs = append(errs, err)
}
if err := (fg.publisher).Send(context.Background(), b); err != nil {
log.WithField("name", pkg.Name).WithError(err).Error("Error sending package to upstream publisher")
errs = append(errs, err)
}
processed++
}
err := errPub
if len(errs) == 0 {
err = nil
}
if len(pkgs)-processed != 0 {
log.Errorf("Failed to publish %v packages", len(pkgs)-processed)
}
return processed, err
}