-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpecker.go
323 lines (289 loc) · 7.1 KB
/
pecker.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package logpeck
import (
"errors"
"fmt"
"sync"
"time"
log "github.com/Sirupsen/logrus"
"github.com/hpcloud/tail"
)
// Pecker .
type Pecker struct {
logTasks map[string]*LogTask
nameToPath map[string]string
db *DB
mu sync.Mutex
stop bool
}
// NewPecker .
func NewPecker(db *DB) (*Pecker, error) {
pecker := &Pecker{
logTasks: make(map[string]*LogTask),
nameToPath: make(map[string]string),
db: db,
stop: true,
}
err := pecker.restorePeckTasks(db)
if err != nil {
return nil, err
}
return pecker, nil
}
func (p *Pecker) restorePeckTasks(db *DB) error {
defer LogExecTime(time.Now(), "Restore PeckTaskConfig")
configs, err := p.db.GetAllConfigs()
if err != nil {
return err
}
for i, config := range configs {
stat, _ := p.db.GetStat(config.Name)
p.AddPeckTask(&config, stat)
log.Infof("[Pecker] Restore PeckTask[%d] : %v", i, config)
}
return nil
}
// allow only modification of db/logTasks/nameToPath in this function
func (p *Pecker) record(config *PeckTaskConfig, stat *PeckTaskStat) {
if _, ok := p.nameToPath[config.Name]; !ok {
if _, ok2 := p.logTasks[config.LogPath]; !ok2 {
p.logTasks[config.LogPath] = NewLogTask(config.LogPath)
}
p.nameToPath[config.Name] = config.LogPath
}
db.SaveConfig(config)
if stat != nil {
db.SaveStat(stat)
}
}
// AddPeckTask .
func (p *Pecker) AddPeckTask(config *PeckTaskConfig, stat *PeckTaskStat) error {
p.mu.Lock()
defer p.mu.Unlock()
log.Info("[Pecker] AddPeckTask", *config)
if _, ok := p.nameToPath[config.Name]; ok {
return errors.New("Peck task already exist")
}
task, err := NewPeckTask(config, stat)
if err != nil {
return err
}
p.record(config, &task.Stat)
// AddPeckTask must be successful
p.logTasks[p.nameToPath[config.Name]].AddPeckTask(task)
log.Info("[Pecker] Add PeckTask nameToPath", p.nameToPath)
log.Info("[Pecker] Add PeckTask logTasks", p.logTasks)
return nil
}
// UpdatePeckTask .
func (p *Pecker) UpdatePeckTask(config *PeckTaskConfig) error {
p.mu.Lock()
defer p.mu.Unlock()
log.Info("[Pecker] UpdatePeckTask", *config)
if _, ok := p.nameToPath[config.Name]; !ok {
return errors.New("Peck task name not exist")
}
stat, err := db.GetStat(config.Name)
task, err := NewPeckTask(config, stat)
if err != nil {
return err
}
p.record(config, &task.Stat)
// UpdatePeckTask must be successful
if err := p.logTasks[p.nameToPath[config.Name]].UpdatePeckTask(task); err != nil {
return err
}
log.Info("[Pecker] Update PeckTask nameToPath", p.nameToPath)
log.Info("[Pecker] Update PeckTask logTasks", p.logTasks)
return nil
}
// RemovePeckTask .
func (p *Pecker) RemovePeckTask(config *PeckTaskConfig) error {
p.mu.Lock()
defer p.mu.Unlock()
if _, ok := p.nameToPath[config.Name]; !ok {
return errors.New("Peck task name not exist")
}
logPath, ok1 := p.nameToPath[config.Name]
logTask, ok2 := p.logTasks[logPath]
if !ok1 || !ok2 {
log.Panicf("%v\n%v\n%v", config.Name, p.nameToPath, p.logTasks)
}
log.Info("[Pecker] Remove PeckTask try clean db", config)
err1 := db.RemoveConfig(config.Name)
err2 := db.RemoveStat(config.Name)
if err1 != nil || err2 != nil {
panic(err1.Error() + " " + err2.Error())
}
if err := logTask.RemovePeckTask(config); err != nil {
return err
}
delete(p.nameToPath, config.Name)
if logTask.Empty() {
logTask.Close()
delete(p.logTasks, logPath)
}
log.Info("[Pecker] Remove PeckTask nameToPath", p.nameToPath)
log.Info("[Pecker] Remove PeckTask logTasks", p.logTasks)
return nil
}
// ListPeckTask .
func (p *Pecker) ListPeckTask() ([]PeckTaskConfig, error) {
p.mu.Lock()
defer p.mu.Unlock()
configs, err := p.db.GetAllConfigs()
if err != nil {
return nil, err
}
return configs, nil
}
// ListTaskStats .
func (p *Pecker) ListTaskStats() ([]PeckTaskStat, error) {
p.mu.Lock()
defer p.mu.Unlock()
stats, err := p.db.GetAllStats()
if err != nil {
return nil, err
}
return stats, nil
}
// StartPeckTask .
func (p *Pecker) StartPeckTask(config *PeckTaskConfig) error {
p.mu.Lock()
defer p.mu.Unlock()
logPath, ok := p.nameToPath[config.Name]
if !ok {
log.Infof("Task not exist, Name: %s, Exist: %v", config.Name, p.nameToPath)
return fmt.Errorf("Task not exist, Name: %s, Exist: %v", config.Name, p.nameToPath)
}
logTask := p.logTasks[logPath]
if err := logTask.StartPeckTask(config); err != nil {
return err
}
{
// Try update peck task stat in boltdb
// stat, err := db.GetStat(config.LogPath, config.Name)
stat, err := db.GetStat(config.Name)
if err != nil {
return err
}
if !stat.Stop {
return errors.New("Task already started")
}
stat.Stop = false
err = db.SaveStat(stat)
}
if logTask.IsStop() {
logTask.Start()
}
return nil
}
// StopPeckTask .
func (p *Pecker) StopPeckTask(config *PeckTaskConfig) error {
p.mu.Lock()
defer p.mu.Unlock()
log.Infof("[Pecker]Try stop task, Name: %s, Exist: %v", config.Name, p.nameToPath)
logPath, ok := p.nameToPath[config.Name]
if !ok {
log.Infof("Task not exist, Name: %s, Exist: %v", config.Name, p.nameToPath)
return fmt.Errorf("Task not exist, Name: %s, Exist: %v", config.Name, p.nameToPath)
}
logTask := p.logTasks[logPath]
if err := logTask.StopPeckTask(config); err != nil {
return err
}
{
// Try update peck task stat in boltdb
stat, err := db.GetStat(config.Name)
if err != nil {
return err
}
if stat.Stop {
return errors.New("Task already stopped")
}
stat.Stop = true
err = db.SaveStat(stat)
}
return nil
}
// TestPeckTask .
func TestPeckTask(config *PeckTaskConfig) ([]map[string]interface{}, error) {
task, err := NewPeckTask(config, nil)
if err != nil {
return []map[string]interface{}{}, err
}
tailConf := tail.Config{
MustExist: true,
ReOpen: true,
Poll: true,
Follow: true,
Location: &tail.SeekInfo{
Offset: 0,
Whence: 2,
},
}
ch := make(chan bool, 1)
resultsCh := make(chan map[string]interface{}, config.Test.TestNum)
id := 0
close := false
tail, err := tail.TailFile(config.LogPath, tailConf)
if err != nil {
return []map[string]interface{}{}, err
}
go func() {
for content := range tail.Lines {
if close == true {
break
}
fields, err := task.ProcessTest(content.Text)
Log := make(map[string]interface{})
if err != nil {
if err.Error() == "Discarded" {
continue
}
Log["_Error"] = err.Error()
Log["_Log"] = content.Text
} else if _, ok := fields["_Log"]; !ok {
Log["_Log"] = content.Text
Log["_Fields"] = fields
} else {
Log = fields
}
resultsCh <- Log
id++
if id >= config.Test.TestNum {
break
}
}
ch <- true
}()
var res []map[string]interface{}
select {
case <-ch:
case <-time.After(time.Second * time.Duration(config.Test.Timeout)):
close = true
}
l := len(resultsCh)
for i := 0; i < l; i++ {
res = append(res, <-resultsCh)
}
return res, nil
}
// Start .
func (p *Pecker) Start() error {
p.mu.Lock()
defer p.mu.Unlock()
if !p.stop {
return errors.New("Pecker already started")
}
for path, logTask := range p.logTasks {
log.Info("[Pecker] Start LogTask", path)
logTask.Start()
}
return nil
}
// GetStat .
func (p *Pecker) GetStat() *PeckerStat {
p.mu.Lock()
defer p.mu.Unlock()
return nil
}