package services import ( "context" "eta/eta_data_analysis/utils" "fmt" "io/fs" "os" "path/filepath" "sync" ) // HandleExcelFilePathMutex 创建一个互斥锁 var HandleExcelFilePathMutex sync.Mutex func FenweiReadWatchIndexFile(context.Context) (err error) { defer func() { if err != nil { fmt.Println("FenweiReadWatchIndexFile Err:" + err.Error()) utils.FileLog.Info(fmt.Sprintf("FenweiReadWatchIndexFile Err: %s", err.Error())) } }() if utils.FenweiFileDir == "" { utils.FileLog.Info("文件目录未配置") return } // 上一个监听未结束时不执行 HandleExcelFilePathMutex.Lock() defer HandleExcelFilePathMutex.Unlock() //fmt.Println("FenweiFileDir: ", utils.FenweiFileDir) err = filepath.Walk(utils.FenweiFileDir, func(path string, info fs.FileInfo, e error) error { if e != nil { err = fmt.Errorf("walk err: %s", e.Error()) return err } if info.IsDir() { return nil } if path == "" { return nil } //fmt.Println("path", path) e = AnalysisFenweiNewExcel(path) if e != nil { err = fmt.Errorf("AnalysisFenweiNewExcel err: %s", e.Error()) return err } // 移动文件至已处理目录 e = utils.CreateDirIfNotExists(utils.FenweiOldFileDir) if e != nil { err = fmt.Errorf("CreateDirIfNotExists err: %s", e.Error()) return err } destPath := filepath.Join(utils.FenweiOldFileDir, info.Name()) e = os.Rename(path, destPath) if e != nil { err = fmt.Errorf("file move err: %s", e.Error()) return err } return nil }) return }