-
Notifications
You must be signed in to change notification settings - Fork 2
/
processor.go
59 lines (53 loc) · 1.09 KB
/
processor.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
package snitch
import (
"github.com/xjewer/snitch/lib/simplelog"
"gopkg.in/tomb.v1"
)
// Processor is a structure which gets the Reader, reads line by line from that and sends them to the Parser
type Processor struct {
tomb.Tomb
p Parser
r LogReader
l simplelog.Logger
}
// NewProcessor returns the New Processor
func NewProcessor(p Parser, r LogReader, l simplelog.Logger) *Processor {
return &Processor{
p: p,
r: r,
l: l,
}
}
// Close processor and reader
func (p *Processor) Close() error {
err := p.r.Close()
if err != ErrReaderIsFinished {
p.l.Println(err)
}
p.Kill(ErrProcessorIsFinished)
return p.Wait()
}
// Run runs handler getting readers's simplelog lines and parse them
func (p *Processor) Run() {
defer p.Done()
lines := make(chan *Line, 0)
defer close(lines)
go p.r.GetLines(lines)
for {
select {
case l := <-lines:
if l.err != nil {
p.l.Println("got line error", l.err)
continue
}
err := p.p.HandleLine(l)
if err != nil {
p.l.Println(err)
continue
}
case <-p.Dying():
p.l.Printf("Closing %q ...\n", p.r.GetName())
return
}
}
}