75 lines
2.0 KiB
Go
75 lines
2.0 KiB
Go
// Package comtrade define related functions for comtrade data processing
|
|
package comtrade
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"sync"
|
|
|
|
"github.com/fsnotify/fsnotify"
|
|
"github.com/panjf2000/ants/v2"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
// DirWatch define function for monitoring directories and passing add file name to channels
|
|
func DirWatch(monitorDir string, addChan chan string) {
|
|
logger := zap.L()
|
|
|
|
watcher, err := fsnotify.NewWatcher()
|
|
if err != nil {
|
|
logger.Error("create dir watcher failed:", zap.Error(err))
|
|
}
|
|
defer watcher.Close()
|
|
|
|
err = watcher.Add(monitorDir)
|
|
if err != nil {
|
|
logger.Error("add monitor dir failed:", zap.Error(err))
|
|
}
|
|
|
|
for {
|
|
select {
|
|
case event, ok := <-watcher.Events:
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
if event.Op&fsnotify.Create == fsnotify.Create {
|
|
logger.Info("file add in watcher dir", zap.String("file_name", event.Name))
|
|
addChan <- event.Name
|
|
}
|
|
|
|
if event.Op&fsnotify.Remove == fsnotify.Remove {
|
|
logger.Info("file remove in watcher dir", zap.String("file_name", event.Name))
|
|
}
|
|
case err, ok := <-watcher.Errors:
|
|
if !ok {
|
|
logger.Error("monitor watcher error channel closed", zap.Bool("channel_status", ok))
|
|
return
|
|
}
|
|
|
|
logger.Error("monitor watcher error occurred", zap.Error(err))
|
|
}
|
|
}
|
|
}
|
|
|
|
// TraverseMonitorDir define function for traverse the files existing in the directory
|
|
func TraverseMonitorDir(ctx context.Context, monitorDir string, dbName string, comtradeMap *sync.Map, addChan chan string, delChan chan string, pool *ants.PoolWithFunc) {
|
|
logger := zap.L()
|
|
err := filepath.Walk(monitorDir, func(path string, info os.FileInfo, err error) error {
|
|
if err != nil {
|
|
logger.Error("accessing a path failed", zap.Error(err))
|
|
// TODO 嵌套error
|
|
return err
|
|
}
|
|
|
|
if !info.IsDir() {
|
|
processComtradeFile(ctx, path, monitorDir, dbName, comtradeMap, addChan, delChan, pool, logger)
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
logger.Error("traversing files that already exist in the monitor directory failed", zap.Error(err))
|
|
}
|
|
}
|