|
| 1 | +// Copyright 2018 The grok_exporter Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package tailer |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "github.com/fstab/grok_exporter/tailer/fswatcher" |
| 20 | +) |
| 21 | + |
| 22 | +type tailerWrapper struct { |
| 23 | + lines chan string |
| 24 | + errors chan Error |
| 25 | + done chan struct{} |
| 26 | +} |
| 27 | + |
| 28 | +func (t *tailerWrapper) Close() { |
| 29 | + close(t.done) |
| 30 | + close(t.lines) |
| 31 | + close(t.errors) |
| 32 | +} |
| 33 | + |
| 34 | +func (t *tailerWrapper) Lines() chan string { |
| 35 | + return t.lines |
| 36 | +} |
| 37 | + |
| 38 | +func (t *tailerWrapper) Errors() chan Error { |
| 39 | + return t.errors |
| 40 | +} |
| 41 | + |
| 42 | +// Switch to the new file tailer implementation which supports watching multiple files. |
| 43 | +// Once we switched for all supported operating systems, we can remove the old implementation and the wrapper. |
| 44 | +func RunFseventFileTailer(path string, readall bool, failOnMissingFile bool, _ interface{}) Tailer { |
| 45 | + result := &tailerWrapper{ |
| 46 | + lines: make(chan string), |
| 47 | + errors: make(chan Error), |
| 48 | + done: make(chan struct{}), |
| 49 | + } |
| 50 | + |
| 51 | + newTailer, err := fswatcher.Run([]string{path}, readall, failOnMissingFile) |
| 52 | + if err != nil { |
| 53 | + go func() { |
| 54 | + result.errors <- newError("failed to initialize file system watcher", err) |
| 55 | + }() |
| 56 | + return result |
| 57 | + } |
| 58 | + |
| 59 | + go func() { |
| 60 | + for { |
| 61 | + select { |
| 62 | + case l := <-newTailer.Lines(): |
| 63 | + fmt.Printf("*** forwarding line %q to wrapped tailer\n", l.Line) |
| 64 | + result.lines <- l.Line |
| 65 | + case e := <-newTailer.Errors(): |
| 66 | + result.errors <- newError(e.Error(), e.Cause()) |
| 67 | + result.Close() |
| 68 | + return |
| 69 | + case <-result.done: |
| 70 | + newTailer.Close() |
| 71 | + return |
| 72 | + } |
| 73 | + } |
| 74 | + }() |
| 75 | + return result |
| 76 | +} |
0 commit comments