task.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package services
  2. import (
  3. "eta/eta_data_analysis/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/task"
  6. "github.com/patrickmn/go-cache"
  7. "github.com/robfig/cron/v3"
  8. "io/fs"
  9. "os"
  10. "path/filepath"
  11. "strings"
  12. "syscall"
  13. "time"
  14. )
  15. func Task() {
  16. fmt.Println("task start")
  17. if utils.YongyiOpen == "1" {
  18. // 每隔两分钟检测文件是否变动,如果发生变动,则自动解析数据到库里
  19. readWatchYongyiFile := task.NewTask("ReadWatchYongyiFile", "0 */2 * * * *", ReadWatchYongyiFile)
  20. task.AddTask("监听涌溢咨询文件夹并解析Excel", readWatchYongyiFile)
  21. /*handleYongyiExcelDaily := task.NewTask("handleYongyiExcelDaily", "0 35 13,23 * * *", HandleYongyiExcelDaily)
  22. task.AddTask("涌益咨询日度指标处理", handleYongyiExcelDaily)
  23. handleYongyiExcelWeekly := task.NewTask("handleYongyiExcelWeekly", "0 5 17,23 * * *", HandleYongyiExcelWeekly)
  24. task.AddTask("涌益咨询周度指标处理", handleYongyiExcelWeekly)*/
  25. //HandleYongyiExcelDaily("/Users/xiexiaoyuan/Downloads/2023年11月21日涌益咨询日度数据 (1).xlsx")
  26. //HandleYongyiExcelWeekly()
  27. yongyiDownloadDaily := task.NewTask("YongyiDownloadDaily", "0 */30 13-23 * * *", YongyiDownloadDaily)
  28. task.AddTask("涌益咨询日度指标下载", yongyiDownloadDaily)
  29. yongyiDownloadWeekly := task.NewTask("YongyiDownloadWeekly", "0 30 17-23 * * *", YongyiDownloadWeekyly)
  30. task.AddTask("涌益咨询周度指标下载", yongyiDownloadWeekly)
  31. task.StartTask()
  32. }
  33. if utils.CoalMineOpen == "1" {
  34. c := cron.New(cron.WithSeconds())
  35. //每2分钟检测一次指标文件是否更新
  36. _, err := c.AddFunc("0 */2 * * * *", ReadWatchIndexFile)
  37. if err != nil {
  38. fmt.Println("watch.ReadWatchIndexFile err" + err.Error())
  39. utils.FileLog.Info("watch.ReadWatchIndexFile err" + err.Error())
  40. }
  41. c.Start()
  42. }
  43. fmt.Println("task end")
  44. }
  45. func ReadWatchIndexFile() {
  46. fmt.Println("ReadWatchIndexFile start")
  47. var err error
  48. defer func() {
  49. if err != nil {
  50. fmt.Println("ReadWatchIndexFile Err:" + err.Error())
  51. }
  52. }()
  53. var cacheClient *cache.Cache
  54. if cacheClient == nil {
  55. cacheClient = cache.New(365*24*time.Hour, 365*24*time.Hour)
  56. }
  57. err = filepath.Walk(utils.CoalMineFilePath, func(path string, info fs.FileInfo, err error) error {
  58. if err != nil {
  59. return err
  60. }
  61. if !info.IsDir() {
  62. fileInfo, err := os.Stat(path)
  63. if err != nil {
  64. fmt.Println("os.Stat:", err.Error())
  65. }
  66. winFileAttr := fileInfo.Sys().(*syscall.Win32FileAttributeData)
  67. modifyTimeStr := utils.SecondToTime(winFileAttr.LastWriteTime.Nanoseconds() / 1e9).Format(utils.FormatDateTime)
  68. existModifyTime, ok := cacheClient.Get(path)
  69. if ok {
  70. existModifyTimeStr := existModifyTime.(string)
  71. if existModifyTimeStr != modifyTimeStr {
  72. if strings.Contains(path, "442家晋陕蒙煤矿周度产量数据") {
  73. err = Jsm(path)
  74. } else if strings.Contains(path, "内陆17省动力煤终端用户供耗存") {
  75. err = Inland(path)
  76. } else if strings.Contains(path, "沿海八省动力煤终端用户供耗存数据更新") {
  77. err = Coastal(path)
  78. } else if strings.Contains(path, "442家晋陕蒙历史数据") {
  79. err = JsmHistory(path)
  80. } else if strings.Contains(path, "CⅢ-8-16 25省市库存和日耗情况") {
  81. err = CoastalHistory(path)
  82. time.Sleep(time.Second * 10)
  83. err = InlandHistory(path)
  84. }
  85. }
  86. } else {
  87. if strings.Contains(path, "442家晋陕蒙煤矿周度产量数据") {
  88. err = Jsm(path)
  89. } else if strings.Contains(path, "内陆17省动力煤终端用户供耗存") {
  90. err = Inland(path)
  91. } else if strings.Contains(path, "沿海八省动力煤终端用户供耗存数据更新") {
  92. err = Coastal(path)
  93. } else if strings.Contains(path, "442家晋陕蒙历史数据") {
  94. err = JsmHistory(path)
  95. } else if strings.Contains(path, "CⅢ-8-16 25省市库存和日耗情况") {
  96. err = CoastalHistory(path)
  97. time.Sleep(time.Second * 10)
  98. err = InlandHistory(path)
  99. }
  100. }
  101. cacheClient.Delete(path)
  102. cacheClient.Set(path, modifyTimeStr, 24*time.Hour)
  103. }
  104. return nil
  105. })
  106. }