This repository has been archived by the owner on Mar 30, 2020. It is now read-only.
/
tail.go
172 lines (140 loc) · 3.19 KB
/
tail.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
package input
import (
"fmt"
"io/ioutil"
"os"
"path"
"strconv"
"time"
"github.com/mcuadros/harvester/src/intf"
. "github.com/mcuadros/harvester/src/logger"
"github.com/hpcloud/tail"
"github.com/hpcloud/tail/ratelimiter"
)
type TailConfig struct {
Format string `description:"A valid format name"`
File string `description:"File to be read"`
MustExist bool `description:"Fail early if the file does not exist"`
Poll bool `description:"Poll for file changes instead of using inotify"`
LimitRate int64 `description:"Maximum read rate (lines per second)"`
}
type Tail struct {
tail *tail.Tail
format intf.Format
file string
posFile string
eof bool
needSavePos bool
stopChannel chan struct{}
savePosTicker *time.Ticker
}
func NewTail(config *TailConfig, format intf.Format) *Tail {
input := new(Tail)
input.SetConfig(config)
input.SetFormat(format)
input.Boot()
return input
}
func (i *Tail) Boot() {
i.goKeepPosition()
}
func (i *Tail) SetFormat(format intf.Format) {
i.format = format
}
func (i *Tail) SetConfig(config *TailConfig) {
i.file = config.File
Info(i.file)
i.posFile = fmt.Sprintf("%s/.%s.pos", path.Dir(i.file), path.Base(i.file))
i.createTailReader(i.translateConfig(config))
}
func (i *Tail) createTailReader(config tail.Config) {
tail, err := tail.TailFile(i.file, config)
if err != nil {
Critical("tail %s: %v", i.file, err)
}
i.tail = tail
}
func (i *Tail) translateConfig(original *TailConfig) tail.Config {
config := tail.Config{Follow: true, ReOpen: true}
if original.MustExist {
config.MustExist = true
}
if original.Poll {
config.Poll = true
}
if original.LimitRate > 0 {
config.RateLimiter = ratelimiter.NewLeakyBucket(
uint16(original.LimitRate),
time.Second,
)
}
position := i.readPosition()
if position > 0 {
config.Location = &tail.SeekInfo{Offset: position, Whence: 0}
}
return config
}
func (i *Tail) readPosition() int64 {
_, err := os.Stat(i.posFile)
if os.IsNotExist(err) {
return 0
}
positionRaw, err := ioutil.ReadFile(i.posFile)
if err != nil {
Critical("read %s: %v", i.posFile, err)
}
position, err := strconv.ParseInt(string(positionRaw), 10, 0)
if err != nil {
Critical("malformed content %s: %v", i.posFile, err)
}
return position
}
func (i *Tail) GetLine() string {
line, ok := (<-i.tail.Lines)
if ok {
i.needSavePos = true
return line.Text
} else {
i.eof = true
return ""
}
}
func (i *Tail) GetRecord() intf.Record {
line := i.GetLine()
if line != "" {
return i.format.Parse(line)
}
return nil
}
func (i *Tail) goKeepPosition() {
i.savePosTicker = time.NewTicker(1 * time.Second)
i.stopChannel = make(chan struct{})
go func() {
for {
select {
case <-i.savePosTicker.C:
if i.needSavePos {
i.keepPosition()
i.needSavePos = false
}
case <-i.stopChannel:
i.savePosTicker.Stop()
return
}
}
}()
}
func (i *Tail) keepPosition() {
position, _ := i.tail.Tell()
ioutil.WriteFile(i.posFile, []byte(strconv.FormatInt(position, 10)), 0755)
}
func (i *Tail) IsEOF() bool {
return i.eof
}
func (i *Tail) Stop() {
i.keepPosition()
i.tail.Stop()
}
func (i *Tail) Teardown() {
close(i.stopChannel)
}