Fixed stop hang & stop err

This commit is contained in:
miraclesu 2013-09-23 15:03:51 +08:00
parent 45a47abe0e
commit a7f9b4b6fc
5 changed files with 29 additions and 14 deletions

10
tail.go
View File

@ -130,7 +130,10 @@ func (tail *Tail) reopen() error {
if err != nil { if err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
log.Printf("Waiting for %s to appear...", tail.Filename) log.Printf("Waiting for %s to appear...", tail.Filename)
if err := tail.watcher.BlockUntilExists(tail.Tomb); err != nil { if err := tail.watcher.BlockUntilExists(&tail.Tomb); err != nil {
if err == tomb.ErrDying {
return err
}
return fmt.Errorf("Failed to detect creation of %s: %s", tail.Filename, err) return fmt.Errorf("Failed to detect creation of %s: %s", tail.Filename, err)
} }
continue continue
@ -154,6 +157,9 @@ func (tail *Tail) tailFileSync() {
if !tail.MustExist { if !tail.MustExist {
// deferred first open. // deferred first open.
err := tail.reopen() err := tail.reopen()
if err == tomb.ErrDying {
return
}
if err != nil { if err != nil {
tail.Kill(err) tail.Kill(err)
return return
@ -236,7 +242,7 @@ func (tail *Tail) waitForChanges() error {
if err != nil { if err != nil {
return err return err
} }
tail.changes = tail.watcher.ChangeEvents(tail.Tomb, st) tail.changes = tail.watcher.ChangeEvents(&tail.Tomb, st)
} }
select { select {

View File

@ -40,6 +40,16 @@ func TestMustExist(t *testing.T) {
tail.Stop() tail.Stop()
} }
func TestStop(t *testing.T) {
tail, err := TailFile("_no_such_file", Config{Follow: true, MustExist: false})
if err != nil {
t.Error("MustExist:false is violated")
}
if tail.Stop() != nil {
t.Error("Should be stoped successfully")
}
}
func TestMaxLineSize(_t *testing.T) { func TestMaxLineSize(_t *testing.T) {
t := NewTailTest("maxlinesize", _t) t := NewTailTest("maxlinesize", _t)
t.CreateFile("test.txt", "hello\nworld\nfin\nhe") t.CreateFile("test.txt", "hello\nworld\nfin\nhe")

View File

@ -4,9 +4,9 @@ package watch
import ( import (
"github.com/howeyc/fsnotify" "github.com/howeyc/fsnotify"
"launchpad.net/tomb"
"os" "os"
"path/filepath" "path/filepath"
"launchpad.net/tomb"
) )
// InotifyFileWatcher uses inotify to monitor file changes. // InotifyFileWatcher uses inotify to monitor file changes.
@ -20,7 +20,7 @@ func NewInotifyFileWatcher(filename string) *InotifyFileWatcher {
return fw return fw
} }
func (fw *InotifyFileWatcher) BlockUntilExists(t tomb.Tomb) error { func (fw *InotifyFileWatcher) BlockUntilExists(t *tomb.Tomb) error {
w, err := fsnotify.NewWatcher() w, err := fsnotify.NewWatcher()
if err != nil { if err != nil {
return err return err
@ -56,7 +56,7 @@ func (fw *InotifyFileWatcher) BlockUntilExists(t tomb.Tomb) error {
panic("unreachable") panic("unreachable")
} }
func (fw *InotifyFileWatcher) ChangeEvents(t tomb.Tomb, fi os.FileInfo) *FileChanges { func (fw *InotifyFileWatcher) ChangeEvents(t *tomb.Tomb, fi os.FileInfo) *FileChanges {
changes := NewFileChanges() changes := NewFileChanges()
w, err := fsnotify.NewWatcher() w, err := fsnotify.NewWatcher()

View File

@ -21,7 +21,7 @@ func NewPollingFileWatcher(filename string) *PollingFileWatcher {
var POLL_DURATION time.Duration var POLL_DURATION time.Duration
func (fw *PollingFileWatcher) BlockUntilExists(t tomb.Tomb) error { func (fw *PollingFileWatcher) BlockUntilExists(t *tomb.Tomb) error {
for { for {
if _, err := os.Stat(fw.Filename); err == nil { if _, err := os.Stat(fw.Filename); err == nil {
return nil return nil
@ -38,7 +38,7 @@ func (fw *PollingFileWatcher) BlockUntilExists(t tomb.Tomb) error {
panic("unreachable") panic("unreachable")
} }
func (fw *PollingFileWatcher) ChangeEvents(t tomb.Tomb, origFi os.FileInfo) *FileChanges { func (fw *PollingFileWatcher) ChangeEvents(t *tomb.Tomb, origFi os.FileInfo) *FileChanges {
changes := NewFileChanges() changes := NewFileChanges()
var prevModTime time.Time var prevModTime time.Time

View File

@ -3,19 +3,18 @@
package watch package watch
import ( import (
"os"
"launchpad.net/tomb" "launchpad.net/tomb"
"os"
) )
// FileWatcher monitors file-level events. // FileWatcher monitors file-level events.
type FileWatcher interface { type FileWatcher interface {
// BlockUntilExists blocks until the file comes into existence. // BlockUntilExists blocks until the file comes into existence.
BlockUntilExists(tomb.Tomb) error BlockUntilExists(*tomb.Tomb) error
// ChangeEvents reports on changes to a file, be it modification, // ChangeEvents reports on changes to a file, be it modification,
// deletion, renames or truncations. Returned FileChanges group of // deletion, renames or truncations. Returned FileChanges group of
// channels will be closed, thus become unusable, after a deletion // channels will be closed, thus become unusable, after a deletion
// or truncation event. // or truncation event.
ChangeEvents(tomb.Tomb, os.FileInfo) *FileChanges ChangeEvents(*tomb.Tomb, os.FileInfo) *FileChanges
} }