-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathfeed_manager.go
248 lines (195 loc) · 5.63 KB
/
feed_manager.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
package readeef
import (
"context"
"fmt"
"regexp"
"time"
"net/url"
"github.com/pkg/errors"
"github.com/urandom/readeef/config"
"github.com/urandom/readeef/content"
"github.com/urandom/readeef/content/processor"
"github.com/urandom/readeef/content/repo"
"github.com/urandom/readeef/feed"
"github.com/urandom/readeef/log"
"github.com/urandom/readeef/parser"
)
// TODO: split up this struct and modify the api
type FeedManager struct {
config config.Config
repo repo.Feed
ops chan func(context.Context, *FeedManager)
log log.Log
hubbub *Hubbub
scheduler feed.Scheduler
parserProcessors []processor.Feed
}
var (
commentPattern = regexp.MustCompile("<!--.*?-->")
linkPattern = regexp.MustCompile(`<link ([^>]+)>`)
ErrNoFeed = errors.New("Feed not found")
httpStatusPrefix = "HTTP Status: "
)
func NewFeedManager(repo repo.Feed, c config.Config, l log.Log) *FeedManager {
return &FeedManager{
repo: repo, config: c, log: l,
ops: make(chan func(context.Context, *FeedManager)),
scheduler: feed.NewScheduler(l),
}
}
func (fm *FeedManager) SetHubbub(hubbub *Hubbub) {
fm.hubbub = hubbub
}
func (fm *FeedManager) AddFeedProcessor(p processor.Feed) {
fm.parserProcessors = append(fm.parserProcessors, p)
}
func (fm *FeedManager) Start(ctx context.Context) error {
fm.log.Infoln("Starting the feed manager")
go fm.loop(ctx)
go fm.scheduler.Start(ctx)
feeds, err := fm.repo.Unsubscribed()
if err != nil {
return err
}
for _, f := range feeds {
fm.log.Infoln("Scheduling feed " + f.String())
fm.AddFeed(f)
}
return nil
}
func (fm *FeedManager) AddFeed(feed content.Feed) {
fm.ops <- func(ctx context.Context, fm *FeedManager) {
fm.startUpdatingFeed(ctx, feed)
}
}
func (fm *FeedManager) RemoveFeed(feed content.Feed) {
fm.ops <- func(ctx context.Context, fm *FeedManager) {
fm.stopUpdatingFeed(feed)
}
}
func (fm *FeedManager) AddFeedByLink(link string) (content.Feed, error) {
u, err := url.Parse(link)
if err == nil {
if !u.IsAbs() {
return content.Feed{}, errors.New("link not absolute")
}
u.Fragment = ""
link = u.String()
} else {
return content.Feed{}, err
}
f, err := fm.repo.FindByLink(link)
if err != nil && !content.IsNoContent(err) {
return f, err
}
if err != nil {
fm.log.Infoln("Discovering feeds in " + link)
parsedFeeds, err := feed.Search(link, fm.log)
if err != nil {
return content.Feed{}, errors.WithMessage(err, "searching for feeds")
}
for link, parserFeed := range parsedFeeds {
f.Link = link
f.Refresh(fm.processParserFeed(parserFeed))
break
}
if _, err = fm.repo.Update(&f); err != nil {
return content.Feed{}, errors.WithMessage(err, "updating feed with parsed data")
}
}
fm.log.Infoln("Adding feed " + f.String() + " to manager")
fm.AddFeed(f)
return f, nil
}
func (fm *FeedManager) RemoveFeedByLink(link string) (content.Feed, error) {
f, err := fm.repo.FindByLink(link)
if err != nil && !content.IsNoContent(err) {
return f, err
}
if f.Validate() != nil {
return f, nil
}
fm.log.Infoln("Removing feed " + f.String() + " from manager")
fm.RemoveFeed(f)
return f, nil
}
func (fm *FeedManager) DiscoverFeeds(link string) ([]content.Feed, error) {
parsedFeeds, err := feed.Search(link, fm.log)
if err != nil {
return []content.Feed{}, errors.WithMessage(err, "discovering feeds")
}
feeds := make([]content.Feed, 0, len(parsedFeeds))
for link, parserFeed := range parsedFeeds {
feed := content.Feed{Link: link}
feed.Refresh(parserFeed)
feeds = append(feeds, feed)
}
return feeds, nil
}
func (fm *FeedManager) loop(ctx context.Context) {
for {
select {
case op := <-fm.ops:
op(ctx, fm)
case <-ctx.Done():
return
}
}
}
func (fm *FeedManager) startUpdatingFeed(ctx context.Context, feed content.Feed) {
if feed.HubLink != "" && fm.hubbub != nil {
err := fm.hubbub.Subscribe(feed)
if err != nil && err != ErrSubscribed {
fm.log.Printf("Error subscribing to feed hublink: %+v\n", err)
}
}
d := 30 * time.Minute
if fm.config.FeedManager.Converted.UpdateInterval != 0 {
if feed.TTL != 0 && feed.TTL > fm.config.FeedManager.Converted.UpdateInterval {
d = feed.TTL
} else {
d = fm.config.FeedManager.Converted.UpdateInterval
}
}
go fm.scheduleFeed(ctx, feed, d)
}
func (fm *FeedManager) scheduleFeed(ctx context.Context, feed content.Feed, update time.Duration) {
fm.log.Infof("Scheduling update of feed %s", feed)
for update := range fm.scheduler.ScheduleFeed(ctx, feed, update) {
fm.log.Infof("Update for feed %s", feed)
if update.IsErr() {
feed.AddUpdateError(fmt.Sprintf("%s: %s", time.Now().Format(time.UnixDate), update.Error()))
} else {
feed.Refresh(fm.processParserFeed(update.Feed))
}
fm.updateFeed(feed)
}
}
func (fm *FeedManager) stopUpdatingFeed(feed content.Feed) {
if feed.HubLink != "" && fm.hubbub != nil {
fm.hubbub.Unsubscribe(feed)
}
fm.log.Infoln("Stopping feed update for " + feed.Link)
users, err := fm.repo.Users(feed)
if err != nil {
fm.log.Printf("Error getting users for feed '%s': %v\n", feed, err)
} else {
if len(users) == 0 {
fm.log.Infoln("Removing orphan feed " + feed.String() + " from the database")
if err = fm.repo.Delete(feed); err != nil {
fm.log.Printf("Error deleting feed '%s' from the repository: %v\n", feed, err)
}
}
}
}
func (fm FeedManager) updateFeed(feed content.Feed) {
if _, err := fm.repo.Update(&feed); err != nil {
fm.log.Printf("Error updating feed '%s' database record: %+v", feed, err)
}
}
func (fm FeedManager) processParserFeed(pf parser.Feed) parser.Feed {
for _, p := range fm.parserProcessors {
pf = p.ProcessFeed(pf)
}
return pf
}