refactors psscanner

This commit is contained in:
Dominic Breuker
2018-03-02 13:59:41 +01:00
parent 9670b85f43
commit 644d65be7b
8 changed files with 150 additions and 116 deletions

View File

@@ -1,13 +1,18 @@
package pspy
import (
"errors"
"os"
"time"
"github.com/dominicbreuker/pspy/internal/config"
)
type Bindings struct {
Logger Logger
FSW FSWatcher
PSS PSScanner
}
type Logger interface {
Infof(format string, v ...interface{})
Errorf(format string, v ...interface{})
@@ -19,56 +24,73 @@ type FSWatcher interface {
Run() (chan struct{}, chan string, chan error)
}
type ProcfsScanner interface {
Setup(triggerCh chan struct{}, interval time.Duration) (chan string, error)
type PSScanner interface {
Run(triggerCh chan struct{}) (chan string, chan error)
}
func Start(cfg config.Config, logger Logger, inotify FSWatcher, pscan ProcfsScanner, sigCh chan os.Signal) (chan struct{}, error) {
logger.Infof("Config: %+v\n", cfg)
func Start(cfg *config.Config, b *Bindings, sigCh chan os.Signal) chan struct{} {
b.Logger.Infof("Config: %+v\n", cfg)
errCh, doneCh := inotify.Init(cfg.RDirs, cfg.Dirs)
initloop:
for {
select {
case <-doneCh:
break initloop
case err := <-errCh:
logger.Errorf("initializing fs watcher: %v", err)
initFSW(b.FSW, cfg.RDirs, cfg.Dirs, b.Logger)
triggerCh, fsEventCh := startFSW(b.FSW, b.Logger)
psEventCh := startPSS(b.PSS, b.Logger, triggerCh)
go func() {
for {
<-time.After(100 * time.Millisecond)
triggerCh <- struct{}{}
}
}
triggerCh, fsEventCh, errCh := inotify.Run()
go logErrors(errCh, logger)
psEventCh, err := pscan.Setup(triggerCh, 100*time.Millisecond)
if err != nil {
logger.Errorf("Can't set up procfs scanner: %+v\n", err)
return nil, errors.New("procfs scanner error")
}
// ignore all file system events created on startup
logger.Infof("Draining file system events due to startup...")
drainChanFor(fsEventCh, 1*time.Second)
logger.Infof("done")
}()
exit := make(chan struct{})
go func() {
for {
select {
case se := <-sigCh:
logger.Infof("Exiting program... (%s)\n", se)
b.Logger.Infof("Exiting program... (%s)\n", se)
exit <- struct{}{}
case fe := <-fsEventCh:
if cfg.LogFS {
logger.Eventf("FS: %+v\n", fe)
b.Logger.Eventf("FS: %+v\n", fe)
}
case pe := <-psEventCh:
if cfg.LogPS {
logger.Eventf("CMD: %+v\n", pe)
b.Logger.Eventf("CMD: %+v\n", pe)
}
}
}
}()
return exit, nil
return exit
}
func initFSW(fsw FSWatcher, rdirs, dirs []string, logger Logger) {
errCh, doneCh := fsw.Init(rdirs, dirs)
for {
select {
case <-doneCh:
return
case err := <-errCh:
logger.Errorf("initializing fs watcher: %v", err)
}
}
}
func startFSW(fsw FSWatcher, logger Logger) (triggerCh chan struct{}, fsEventCh chan string) {
triggerCh, fsEventCh, errCh := fsw.Run()
go logErrors(errCh, logger)
// ignore all file system events created on startup
logger.Infof("Draining file system events due to startup...")
drainEventsFor(triggerCh, fsEventCh, 1*time.Second)
logger.Infof("done")
return triggerCh, fsEventCh
}
func startPSS(pss PSScanner, logger Logger, triggerCh chan struct{}) (psEventCh chan string) {
psEventCh, errCh := pss.Run(triggerCh)
go logErrors(errCh, logger)
return psEventCh
}
func logErrors(errCh chan error, logger Logger) {
@@ -78,10 +100,11 @@ func logErrors(errCh chan error, logger Logger) {
}
}
func drainChanFor(c chan string, d time.Duration) {
func drainEventsFor(triggerCh chan struct{}, eventCh chan string, d time.Duration) {
for {
select {
case <-c:
case <-triggerCh:
case <-eventCh:
case <-time.After(d):
return
}