chg: [filewatcher] daily rotation of watched folder - fixed

master v0.1.14
Jean-Louis Huynen 2021-03-03 12:09:13 +01:00
parent 1e9e4a28d0
commit 9bea72ce04
No known key found for this signature in database
GPG Key ID: 64799157F4BD6B93
1 changed files with 42 additions and 19 deletions

View File

@ -22,7 +22,8 @@ type FileWatcherReader struct {
// TearDown channel // TearDown channel
exit chan string exit chan string
// Chan used to restart the watching channel on a new folder // Chan used to restart the watching channel on a new folder
dailyswitch chan string dailySwitch chan bool
dailySwitchExit chan bool
// Current buffer // Current buffer
json bool json bool
// Current file // Current file
@ -31,17 +32,22 @@ type FileWatcherReader struct {
watching bool watching bool
// Insert Separator // Insert Separator
insertsep bool insertsep bool
// logging
log * log.Logger
} }
// NewFileWatcherReader creates a new FileWatcherReader // NewFileWatcherReader creates a new FileWatcherReader
// json specifies whether we now we handle json files // json specifies whether we now we handle json files
func NewFileWatcherReader(f string, j bool, daily bool) (*FileWatcherReader, error) { func NewFileWatcherReader(f string, j bool, daily bool, logger *log.Logger) (*FileWatcherReader, error) {
r := &FileWatcherReader{ r := &FileWatcherReader{
folderstr: f, folderstr: f,
eic: make(chan notify.EventInfo, 4096), eic: make(chan notify.EventInfo, 4096),
json: j, dailySwitch: make(chan bool),
watching: true, dailySwitchExit: make(chan bool),
insertsep: false, json: j,
watching: true,
insertsep: false,
log: logger,
} }
// go routine holding the watcher // go routine holding the watcher
go setUpWatcher(r, daily) go setUpWatcher(r, daily)
@ -50,8 +56,12 @@ func NewFileWatcherReader(f string, j bool, daily bool) (*FileWatcherReader, err
if daily { if daily {
c := cron.New() c := cron.New()
c.AddFunc("@midnight", func() { c.AddFunc("@midnight", func() {
//c.AddFunc("@every 1m", func() { //c.AddFunc("@every 10s", func() {
r.dailyswitch <- "switch" // Sending exit signal to setUpWatcher
r.dailySwitch <- true
// Waiting for exit signal from setUpWatcher
<-r.dailySwitchExit
go setUpWatcher(r, daily)
}) })
c.Start() c.Start()
} }
@ -61,21 +71,34 @@ func NewFileWatcherReader(f string, j bool, daily bool) (*FileWatcherReader, err
// setUpWatcher holds the watcher // setUpWatcher holds the watcher
func setUpWatcher(r *FileWatcherReader, daily bool) { func setUpWatcher(r *FileWatcherReader, daily bool) {
if daily { if daily {
dt := time.Now()
//Format YYYYMMDD
// TODO make it customizable // TODO make it customizable
currentFolder := dt.Format("20060102") t, _ := time.ParseDuration("1s")
log.Println(fmt.Sprintf("Watching : %s/%s/...", r.folderstr, currentFolder)) retryWatch(r, t)
if err := notify.Watch(fmt.Sprintf("%s/%s/...", r.folderstr, currentFolder), r.eic, notify.InCloseWrite); err != nil {
log.Fatal(err)
}
} else { } else {
if err := notify.Watch(fmt.Sprintf("%s/...", r.folderstr), r.eic, notify.InCloseWrite); err != nil { if err := notify.Watch(fmt.Sprintf("%s/...", r.folderstr), r.eic, notify.InCloseWrite); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }
defer notify.Stop(r.eic) defer notify.Stop(r.eic)
<-r.dailyswitch <-r.dailySwitch
r.dailySwitchExit <- true
}
// retryWatch tries to set up the watcher until it works every t
func retryWatch(r *FileWatcherReader, t time.Duration) {
dt := time.Now()
//Format YYYYMMDD
// TODO make it customizable
currentFolder := dt.Format("20060102")
r.log.Println(fmt.Sprintf("Watching: %s/%s/...", r.folderstr, currentFolder))
for {
if err := notify.Watch(fmt.Sprintf("%s/%s/...", r.folderstr, currentFolder), r.eic, notify.InCloseWrite); err != nil {
r.log.Println(fmt.Sprintf("Waiting for: %s/%s/... to exist", r.folderstr, currentFolder))
time.Sleep(t)
}else{
return
}
}
} }
// Read waits for InCloseWrite file event uses a bytes reader to copy // Read waits for InCloseWrite file event uses a bytes reader to copy
@ -88,12 +111,12 @@ func (fw *FileWatcherReader) Read(p []byte) (n int, err error) {
for { for {
select { select {
case ei := <-fw.eic: case ei := <-fw.eic:
//log.Println("Got event:", ei) //r.log.Println("Got event:", ei)
// New File, let's read its content // New File, let's read its content
var err error var err error
fw.curfile, err = os.Open(ei.Path()) fw.curfile, err = os.Open(ei.Path())
if err != nil { if err != nil {
log.Fatal(err) fw.log.Fatal(err)
} }
fw.watching = false fw.watching = false
break watchloop break watchloop