-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtail.go
196 lines (177 loc) · 4.38 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package webtail
// This file holds directory file tail methods
import (
"io"
"os"
"path"
"path/filepath"
"sync"
"github.com/go-logr/logr"
"github.com/nxadm/tail"
)
// TailAttr holds tail worker attributes
type TailAttr struct {
// Store for last Config.Lines lines
Buffer [][]byte
// Quit worker process
Quit chan struct{}
// Skip 1st line when read file not from start
IsHeadTrimmed bool
}
// TailService holds Worker hub operations
type TailService struct {
log logr.Logger
Config *Config
workers map[string]*TailAttr
index IndexItemAttrStore
}
// tailWorker holds tailer run arguments
type tailWorker struct {
out chan *TailMessage
quit chan struct{}
log logr.Logger
tf *tail.Tail
channel string
}
// NewTailService creates tailer service
func NewTailService(logger logr.Logger, cfg *Config) (*TailService, error) {
_, err := os.Stat(cfg.Root)
if err != nil {
return nil, err
}
aPath, err := filepath.Abs(cfg.Root)
if err != nil {
return nil, err
}
if aPath != cfg.Root {
cfg.Root = aPath
}
return &TailService{
Config: cfg,
log: logger,
workers: make(map[string]*TailAttr),
index: make(IndexItemAttrStore),
}, nil
}
// WorkerExists checks if worker already registered
func (ts *TailService) WorkerExists(channel string) bool {
_, ok := ts.workers[channel]
return ok
}
// ChannelExists checks if channel allowed to attach
func (ts *TailService) ChannelExists(channel string) bool {
if channel == "" {
return true
}
_, ok := ts.index[channel]
return ok
}
// SetTrace turns on/off logging of incoming workers messages
func (ts *TailService) SetTrace(mode string) {
if mode == "on" {
ts.Config.Trace = true
} else if mode == "off" {
ts.Config.Trace = false
}
ts.log.Info("Tracing", "trace", ts.Config.Trace)
}
// TraceEnabled returns trace state
func (ts *TailService) TraceEnabled() bool {
return ts.Config.Trace
}
// WorkerStop stops worker (tailer or indexer)
func (ts *TailService) WorkerStop(channel string) {
w := ts.workers[channel]
w.Quit <- struct{}{}
delete(ts.workers, channel)
}
// TailerBuffer returns worker buffer
func (ts *TailService) TailerBuffer(channel string) [][]byte {
return ts.workers[channel].Buffer
}
// TailerAppend adds a line into worker buffer
func (ts *TailService) TailerAppend(channel string, data []byte) bool {
if ts.workers[channel].IsHeadTrimmed {
// Skip first trimmed (partial) line
ts.workers[channel].IsHeadTrimmed = false
return false
}
buf := ts.workers[channel].Buffer
if len(buf) == ts.Config.Lines {
// drop oldest line if buffer is full
buf = buf[1:]
}
buf = append(buf, data)
ts.workers[channel].Buffer = buf
return true
}
// TailerRun creates and runs tail worker
func (ts *TailService) TailerRun(channel string, out chan *TailMessage, readyChan chan struct{}, wg *sync.WaitGroup) error {
cfg := ts.Config
config := tail.Config{
Follow: true,
ReOpen: true,
MustExist: true,
MaxLineSize: cfg.MaxLineSize,
Poll: cfg.Poll,
}
filename := path.Join(cfg.Root, channel)
headTrimmed := false
if cfg.Bytes != 0 {
fi, err := os.Stat(filename)
if err != nil {
return err
}
// get the file size
size := fi.Size()
if size > cfg.Bytes {
config.Location = &tail.SeekInfo{Offset: -cfg.Bytes, Whence: io.SeekEnd}
headTrimmed = true
}
}
t, err := tail.TailFile(filename, config)
if err != nil {
return err
}
quit := make(chan struct{})
ts.workers[channel] = &TailAttr{Buffer: [][]byte{}, Quit: quit, IsHeadTrimmed: headTrimmed}
go tailWorker{
tf: t,
channel: channel,
out: out,
quit: quit,
log: ts.log,
}.run(readyChan, wg)
return nil
}
// run runs tail worker
func (tw tailWorker) run(readyChan chan struct{}, wg *sync.WaitGroup) {
wg.Add(1)
defer func() {
tw.log.Info("tailworker close")
wg.Done()
}()
log := tw.log.WithValues("channel", tw.channel)
log.Info("Tailer started")
readyChan <- struct{}{}
for {
select {
case line, ok := <-tw.tf.Lines:
if !ok {
log.Error(tw.tf.Err(), "Tailer channel is unavailable")
tw.out <- &TailMessage{Channel: tw.channel, Data: tw.tf.Err().Error(), Type: "error"}
<-tw.quit
return
}
tw.out <- &TailMessage{Channel: tw.channel, Data: line.Text, Type: "log"}
case <-tw.quit:
err := tw.tf.Stop() // Cleanup()
if err != nil {
log.Error(err, "Tailer stopped with error")
} else {
log.Info("Tailer stopped")
}
return
}
}
}