-
Notifications
You must be signed in to change notification settings - Fork 0
/
farmer.go
50 lines (40 loc) · 811 Bytes
/
farmer.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
package harvester
import (
log "github.com/inconshreveable/log15"
)
type Farmer struct {
pitchforks []Pitchfork
maxLoops int
}
func NewFarmer(pitchforks []Pitchfork, maxLoops int) *Farmer {
if maxLoops == 0 {
maxLoops = 50
}
return &Farmer{
pitchforks: pitchforks,
maxLoops: maxLoops,
}
}
func (f *Farmer) Farm(s Seeds) (Data, error) {
d := s.ToData()
loops := 0
for {
c := d.Count()
if f.maxLoops == loops {
break
}
for _, p := range f.pitchforks {
log.Info("harvest", "pitchfork", p.Name())
if err := p.Harvest(d); err != nil {
log.Warn("error executing pitchfork", "name", p.Name(), "error", err)
}
}
finalCount := d.Count()
if c == finalCount {
log.Info("process finalized", "finalCount", finalCount)
break
}
loops++
}
return d, nil
}