task.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. }
  32. if utils.CoalMineOpen == "1" {
  33. c := cron.New(cron.WithSeconds())
  34. //每2分钟检测一次指标文件是否更新
  35. _, err := c.AddFunc("0 */2 * * * *", ReadWatchIndexFile)
  36. if err != nil {
  37. fmt.Println("watch.ReadWatchIndexFile err" + err.Error())
  38. utils.FileLog.Info("watch.ReadWatchIndexFile err" + err.Error())
  39. }
  40. c.Start()
  41. }
  42. // 汾渭数据
  43. if utils.FenweiOpen == "1" {
  44. // 每5分钟检测一次目录是否有新文件
  45. fenWeiReadWatchIndexFile := task.NewTask("fenWeiReadWatchIndexFile", "0 */5 * * * *", FenweiReadWatchIndexFile)
  46. task.AddTask("汾渭数据指标文件检测", fenWeiReadWatchIndexFile)
  47. }
  48. if utils.MtjhOpen == "1" {
  49. c := cron.New(cron.WithSeconds())
  50. //每2分钟检测一次指标文件是否更新
  51. _, err := c.AddFunc("0 */2 * * * *", mtjhWatch)
  52. if err != nil {
  53. fmt.Println("watch.mtjhWatch err" + err.Error())
  54. utils.FileLog.Info("watch.mtjhWatch err" + err.Error())
  55. }
  56. c.Start()
  57. }
  58. if utils.CoalMailAttachmentOpen == "1" {
  59. mailAttachment := task.NewTask("MailAttachment", utils.CoalMailAttachmentTime, MailAttachment)
  60. task.AddTask("启动获取邮件附件脚本", mailAttachment)
  61. }
  62. task.StartTask()
  63. fmt.Println("task end")
  64. }
  65. func ReadWatchIndexFile() {
  66. fmt.Println("ReadWatchIndexFile start")
  67. var err error
  68. defer func() {
  69. if err != nil {
  70. fmt.Println("ReadWatchIndexFile Err:" + err.Error())
  71. }
  72. }()
  73. var cacheClient *cache.Cache
  74. if cacheClient == nil {
  75. cacheClient = cache.New(365*24*time.Hour, 365*24*time.Hour)
  76. }
  77. err = filepath.Walk(utils.CoalMineFilePath, func(path string, info fs.FileInfo, err error) error {
  78. if err != nil {
  79. return err
  80. }
  81. if !info.IsDir() {
  82. fileInfo, err := os.Stat(path)
  83. if err != nil {
  84. fmt.Println("os.Stat:", err.Error())
  85. }
  86. winFileAttr := fileInfo.Sys().(*syscall.Win32FileAttributeData)
  87. modifyTimeStr := utils.SecondToTime(winFileAttr.LastWriteTime.Nanoseconds() / 1e9).Format(utils.FormatDateTime)
  88. existModifyTime, ok := cacheClient.Get(path)
  89. if ok {
  90. existModifyTimeStr := existModifyTime.(string)
  91. if existModifyTimeStr != modifyTimeStr {
  92. if strings.Contains(path, "442家晋陕蒙煤矿周度产量数据") {
  93. err = Jsm(path)
  94. } else if strings.Contains(path, "内陆17省动力煤终端用户供耗存") {
  95. err = Inland(path)
  96. } else if strings.Contains(path, "沿海八省动力煤终端用户供耗存数据更新") {
  97. err = Coastal(path)
  98. } else if strings.Contains(path, "442家晋陕蒙历史数据") {
  99. err = JsmHistory(path)
  100. } else if strings.Contains(path, "CⅢ-8-16 25省市库存和日耗情况") {
  101. err = CoastalHistory(path)
  102. time.Sleep(time.Second * 10)
  103. err = InlandHistory(path)
  104. } else if strings.Contains(path, "分企业煤炭产量旬度数据") {
  105. err = Firm(path)
  106. }
  107. }
  108. } else {
  109. if strings.Contains(path, "442家晋陕蒙煤矿周度产量数据") {
  110. err = Jsm(path)
  111. } else if strings.Contains(path, "内陆17省动力煤终端用户供耗存") {
  112. err = Inland(path)
  113. } else if strings.Contains(path, "沿海八省动力煤终端用户供耗存数据更新") {
  114. err = Coastal(path)
  115. } else if strings.Contains(path, "442家晋陕蒙历史数据") {
  116. err = JsmHistory(path)
  117. } else if strings.Contains(path, "CⅢ-8-16 25省市库存和日耗情况") {
  118. err = CoastalHistory(path)
  119. time.Sleep(time.Second * 10)
  120. err = InlandHistory(path)
  121. } else if strings.Contains(path, "分企业煤炭产量旬度数据") {
  122. err = Firm(path)
  123. }
  124. }
  125. cacheClient.Delete(path)
  126. cacheClient.Set(path, modifyTimeStr, 24*time.Hour)
  127. }
  128. return nil
  129. })
  130. }