Bug #95787: warn and ignore tailing app logs if their base dir (container) goes away fast

.. but not for system logs as we expect /s/logs/ to exist.
This commit is contained in:
Sridhar Ratnakumar 2012-10-10 15:13:13 -07:00
parent 99fe83b742
commit 6a5092ff5c
1 changed files with 11 additions and 7 deletions

18
tail.go
View File

@ -32,20 +32,24 @@ type Tail struct {
// TailFile channels the lines of a logfile along with timestamp. If // TailFile channels the lines of a logfile along with timestamp. If
// end is true, channel only newly added lines. If retry is true, tail // end is true, channel only newly added lines. If retry is true, tail
// the file name (not descriptor) and retry on file open/read errors. // the file name (not descriptor) and retry on file open/read errors.
func TailFile(filename string, maxlinesize int, end bool, retry bool) *Tail { func TailFile(filename string, maxlinesize int, end bool, retry bool) (*Tail, error) {
watcher, err := fileCreateWatcher(filename)
if err != nil {
return nil, err
}
t := &Tail{ t := &Tail{
filename, filename,
make(chan *Line), make(chan *Line),
maxlinesize, maxlinesize,
nil, nil,
nil, nil,
fileCreateWatcher(filename), watcher,
make(chan bool), make(chan bool),
make(chan bool)} make(chan bool)}
go t.tailFileSync(end, retry) go t.tailFileSync(end, retry)
return t return t, nil
} }
func (tail *Tail) Stop() { func (tail *Tail) Stop() {
@ -166,19 +170,19 @@ func (tail *Tail) tailFileSync(end bool, retry bool) {
} }
// returns the watcher for file create events // returns the watcher for file create events
func fileCreateWatcher(filename string) *fsnotify.Watcher { func fileCreateWatcher(filename string) (*fsnotify.Watcher, error) {
watcher, err := fsnotify.NewWatcher() watcher, err := fsnotify.NewWatcher()
if err != nil { if err != nil {
panic(fmt.Sprintf("fsnotify error: %s", err)) return nil, err
} }
// watch on parent directory because the file may not exit. // watch on parent directory because the file may not exit.
err = watcher.WatchFlags(filepath.Dir(filename), fsnotify.FSN_CREATE) err = watcher.WatchFlags(filepath.Dir(filename), fsnotify.FSN_CREATE)
if err != nil { if err != nil {
panic(fmt.Sprintf("fsnotify error on file: %s", err)) return nil, err
} }
return watcher return watcher, nil
} }
// get current time in unix timestamp // get current time in unix timestamp