2
0
mirror of https://github.com/carlostrub/sisyphus synced 2024-10-31 09:20:15 +00:00

re-enable watcher

This commit is contained in:
Carlo Strub 2017-05-23 21:30:24 +00:00
parent 1840dff22a
commit 8ce5e2dada
2 changed files with 49 additions and 53 deletions

View File

@ -79,7 +79,7 @@ func CloseDatabases(databases map[Maildir]*bolt.DB) {
if err != nil {
log.WithFields(log.Fields{
"db": string(key) + "/sisyphus.db",
}).Warn("Unable to close database")
}).Error("Unable to close database")
}
log.WithFields(log.Fields{
"db": string(key) + "/sisyphus.db",

View File

@ -4,8 +4,10 @@ import (
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"github.com/fsnotify/fsnotify"
log "github.com/sirupsen/logrus"
"github.com/carlostrub/sisyphus"
@ -151,58 +153,52 @@ func main() {
}
}
// // Classify on arrival
// watcher, err := fsnotify.NewWatcher()
// if err != nil {
//log.WithFields(log.Fields{
// "err": err,
//}).Warning("Cannot create directory watcher")
// }
// defer watcher.Close()
//
// done := make(chan bool)
// go func() {
// for {
// select {
// case event := <-watcher.Events:
// if event.Op&fsnotify.Create == fsnotify.Create {
// mailName := strings.Split(event.Name, "/")
// m := sisyphus.Mail{
// Key: mailName[len(mailName)-1],
// }
//
// if mailName[len(mailName)-2] == "new" {
// err = m.Classify(db)
// if err != nil {
// log.Print(err)
// }
// } else {
// err = m.Learn(db)
// if err != nil {
// log.Print(err)
// }
// }
//
// }
// case err := <-watcher.Errors:
// log.Println("error:", err)
// }
// }
// }()
//
// err = watcher.Add(maildirPaths[0] + "/cur")
// if err != nil {
// log.Fatal(err)
// }
// err = watcher.Add(maildirPaths[0] + "/new")
// if err != nil {
// log.Fatal(err)
// }
// err = watcher.Add(maildirPaths[0] + "/.Junk/cur")
// if err != nil {
// log.Fatal(err)
// }
// <-done
// Classify whenever a mail arrives in "new"
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Fatal("Cannot setup directory watcher")
}
defer watcher.Close()
done := make(chan bool)
go func() {
for {
select {
case event := <-watcher.Events:
if event.Op&fsnotify.Create == fsnotify.Create {
mailName := strings.Split(event.Name, "/")
mailDir := strings.TrimRight(event.Name, "/new/"+mailName[len(mailName)-1])
m := sisyphus.Mail{
Key: mailName[len(mailName)-1],
}
err = m.Classify(dbs[sisyphus.Maildir(mailDir)])
if err != nil {
log.Print(err)
}
}
case err := <-watcher.Errors:
log.WithFields(log.Fields{
"err": err,
}).Warning("Problem with directory watcher")
}
}
}()
for _, val := range maildirPaths {
err = watcher.Add(val + "/new")
if err != nil {
log.WithFields(log.Fields{
"err": err,
"dir": val + "/new",
}).Error("Cannot watch directory")
}
}
<-done
},
},
{