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
|
package cache
import (
"sync"
"github.com/Necoro/feed2imap-go/internal/feed"
"github.com/Necoro/feed2imap-go/pkg/config"
"github.com/Necoro/feed2imap-go/pkg/log"
)
type State struct {
feeds map[string]*feed.Feed
cachedFeeds map[string]CachedFeed
cache Cache
cfg *config.Config
}
func (state *State) Foreach(f func(CachedFeed)) {
for _, feed := range state.cachedFeeds {
f(feed)
}
}
func (state *State) ForeachGo(goFunc func(CachedFeed)) {
var wg sync.WaitGroup
wg.Add(len(state.feeds))
f := func(feed CachedFeed, wg *sync.WaitGroup) {
goFunc(feed)
wg.Done()
}
for _, feed := range state.cachedFeeds {
go f(feed, &wg)
}
wg.Wait()
}
func (state *State) LoadCache(fileName string, forceNew bool) error {
var (
cache Cache
err error
)
if forceNew {
cache, err = newCache()
} else {
cache, err = LoadCache(fileName)
}
if err != nil {
return err
}
state.cache = cache
for name, feed := range state.feeds {
state.cachedFeeds[name] = cache.cachedFeed(feed)
}
// state.feeds should not be used after loading the cache --> enforce a panic
state.feeds = nil
return nil
}
func (state *State) StoreCache(fileName string) error {
return state.cache.store(fileName)
}
func (state *State) UnlockCache() {
_ = state.cache.Unlock()
}
func (state *State) Fetch() int {
state.ForeachGo(handleFeed)
ctr := 0
for _, cf := range state.cachedFeeds {
success := cf.Feed().FetchSuccessful()
cf.Checked(!success)
if success {
ctr++
}
}
return ctr
}
func handleFeed(cf CachedFeed) {
feed := cf.Feed()
log.Printf("Fetching %s from %s", feed.Name, feed.Url)
err := feed.Parse()
if err != nil {
if feed.Url == "" || cf.Failures() >= feed.Global.MaxFailures {
log.Error(err)
} else {
log.Print(err)
}
}
}
func filterFeed(cf CachedFeed) {
cf.Feed().Filter(cf.Filter)
}
func (state *State) Filter() {
if log.IsDebug() {
// single threaded for better output
state.Foreach(filterFeed)
} else {
state.ForeachGo(filterFeed)
}
}
func NewState(cfg *config.Config) (*State, error) {
state := State{
feeds: map[string]*feed.Feed{},
cachedFeeds: map[string]CachedFeed{},
cache: Cache{}, // loaded later on
cfg: cfg,
}
for name, parsedFeed := range cfg.Feeds {
feed, err := feed.Create(parsedFeed, cfg.GlobalOptions)
if err != nil {
return nil, err
}
state.feeds[name] = feed
}
return &state, nil
}
func (state *State) RemoveUndue() {
for name, feed := range state.cachedFeeds {
if feed.Feed().Disable || !feed.Feed().NeedsUpdate(feed.Last()) {
delete(state.cachedFeeds, name)
}
}
}
func (state *State) NumFeeds() int {
return len(state.cachedFeeds)
}
|