mtjh_watch.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package services
  2. import (
  3. "eta/eta_data_analysis/utils"
  4. "fmt"
  5. "github.com/patrickmn/go-cache"
  6. "io/fs"
  7. "os"
  8. "path/filepath"
  9. "strings"
  10. "syscall"
  11. "time"
  12. )
  13. func mtjhWatch() {
  14. fmt.Println("mtjhWatch start")
  15. var err error
  16. defer func() {
  17. if err != nil {
  18. fmt.Println("mtjhWatch Err:" + err.Error())
  19. }
  20. }()
  21. var cacheClient *cache.Cache
  22. if cacheClient == nil {
  23. cacheClient = cache.New(365*24*time.Hour, 365*24*time.Hour)
  24. }
  25. err = filepath.Walk(utils.MtjhFilePath, func(path string, info fs.FileInfo, err error) error {
  26. if err != nil {
  27. return err
  28. }
  29. if !info.IsDir() {
  30. fileInfo, err := os.Stat(path)
  31. if err != nil {
  32. fmt.Println("os.Stat:", err.Error())
  33. }
  34. winFileAttr := fileInfo.Sys().(*syscall.Win32FileAttributeData)
  35. modifyTimeStr := utils.SecondToTime(winFileAttr.LastWriteTime.Nanoseconds() / 1e9).Format(utils.FormatDateTime)
  36. existModifyTime, ok := cacheClient.Get(path)
  37. if ok {
  38. existModifyTimeStr := existModifyTime.(string)
  39. if existModifyTimeStr != modifyTimeStr {
  40. if strings.Contains(path, "煤炭江湖") {
  41. err = Mtjh(path)
  42. }
  43. }
  44. } else {
  45. if strings.Contains(path, "煤炭江湖") {
  46. err = Mtjh(path)
  47. }
  48. }
  49. cacheClient.Delete(path)
  50. cacheClient.Set(path, modifyTimeStr, 24*time.Hour)
  51. }
  52. return nil
  53. })
  54. }