task.go 4.0 KB

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