task.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package services
  2. import (
  3. ccfService "eta/eta_data_analysis/services/base_from_ccf"
  4. "eta/eta_data_analysis/utils"
  5. "fmt"
  6. "github.com/beego/beego/v2/task"
  7. "github.com/patrickmn/go-cache"
  8. "github.com/robfig/cron/v3"
  9. "io/fs"
  10. "os"
  11. "path/filepath"
  12. "strings"
  13. "syscall"
  14. "time"
  15. )
  16. func Task() {
  17. fmt.Println("task start")
  18. if utils.YongyiOpen == "1" {
  19. // 每隔两分钟检测文件是否变动,如果发生变动,则自动解析数据到库里
  20. readWatchYongyiFile := task.NewTask("ReadWatchYongyiFile", "0 */2 * * * *", ReadWatchYongyiFile)
  21. task.AddTask("监听涌溢咨询文件夹并解析Excel", readWatchYongyiFile)
  22. yongyiDownloadDaily := task.NewTask("YongyiDownloadDaily", "0 */25 13-23 * * *", YongyiDownloadDaily)
  23. task.AddTask("涌益咨询日度指标下载", yongyiDownloadDaily)
  24. yongyiDownloadWeekly := task.NewTask("YongyiDownloadWeekly", "0 */30 16-23 * * 4,5,6,0", YongyiDownloadWeekyly)
  25. task.AddTask("涌益咨询周度指标下载", yongyiDownloadWeekly)
  26. }
  27. if utils.CoalMineOpen == "1" {
  28. c := cron.New(cron.WithSeconds())
  29. //每2分钟检测一次指标文件是否更新
  30. _, err := c.AddFunc("0 */2 * * * *", ReadWatchIndexFile)
  31. if err != nil {
  32. fmt.Println("watch.ReadWatchIndexFile err" + err.Error())
  33. utils.FileLog.Info("watch.ReadWatchIndexFile err" + err.Error())
  34. }
  35. c.Start()
  36. }
  37. // 汾渭数据
  38. if utils.FenweiOpen == "1" {
  39. // 每5分钟检测一次目录是否有新文件
  40. fenWeiReadWatchIndexFile := task.NewTask("fenWeiReadWatchIndexFile", "0 */5 * * * *", FenweiReadWatchIndexFile)
  41. task.AddTask("汾渭数据指标文件检测", fenWeiReadWatchIndexFile)
  42. }
  43. if utils.MtjhOpen == "1" {
  44. c := cron.New(cron.WithSeconds())
  45. //每2分钟检测一次指标文件是否更新
  46. _, err := c.AddFunc("0 */2 * * * *", mtjhWatch)
  47. if err != nil {
  48. fmt.Println("watch.mtjhWatch err" + err.Error())
  49. utils.FileLog.Info("watch.mtjhWatch err" + err.Error())
  50. }
  51. c.Start()
  52. }
  53. if utils.CoalMailAttachmentOpen == "1" {
  54. mailAttachment := task.NewTask("MailAttachment", utils.CoalMailAttachmentTime, MailAttachment)
  55. task.AddTask("启动获取邮件附件脚本", mailAttachment)
  56. }
  57. // CCF化纤信息
  58. if utils.CCFOpen == "1" {
  59. // 原油石化早报-每日9:00
  60. taskCCFOilDailyEdb := task.NewTask("taskCCFOilDailyEdb", "0 30 17 * * *", ccfService.TaskOilDailyEdb)
  61. task.AddTask("CCF原油石化早报", taskCCFOilDailyEdb)
  62. // 各品种周报-每周五17-19点每隔半小时
  63. taskCCFWeeklyEdb := task.NewTask("taskCCFWeeklyEdb", "0 */30 17 * * 5", ccfService.TaskWeeklyEdb)
  64. task.AddTask("CCF周度指标", taskCCFWeeklyEdb)
  65. // 各品种装置-每周四15-18点每隔半小时
  66. taskCCFStockTable := task.NewTask("taskCCFStockTable", "0 */30 15-18 * * 4", ccfService.TaskStockTable)
  67. task.AddTask("CCF装置检修", taskCCFStockTable)
  68. }
  69. task.StartTask()
  70. fmt.Println("task end")
  71. }
  72. func ReadWatchIndexFile() {
  73. fmt.Println("ReadWatchIndexFile start")
  74. var err error
  75. defer func() {
  76. if err != nil {
  77. fmt.Println("ReadWatchIndexFile Err:" + err.Error())
  78. }
  79. }()
  80. var cacheClient *cache.Cache
  81. if cacheClient == nil {
  82. cacheClient = cache.New(365*24*time.Hour, 365*24*time.Hour)
  83. }
  84. err = filepath.Walk(utils.CoalMineFilePath, func(path string, info fs.FileInfo, err error) error {
  85. if err != nil {
  86. return err
  87. }
  88. if !info.IsDir() {
  89. fileInfo, err := os.Stat(path)
  90. if err != nil {
  91. fmt.Println("os.Stat:", err.Error())
  92. }
  93. winFileAttr := fileInfo.Sys().(*syscall.Win32FileAttributeData)
  94. modifyTimeStr := utils.SecondToTime(winFileAttr.LastWriteTime.Nanoseconds() / 1e9).Format(utils.FormatDateTime)
  95. existModifyTime, ok := cacheClient.Get(path)
  96. if ok {
  97. existModifyTimeStr := existModifyTime.(string)
  98. if existModifyTimeStr != modifyTimeStr {
  99. if strings.Contains(path, "442家晋陕蒙煤矿周度产量数据") {
  100. err = Jsm(path)
  101. } else if strings.Contains(path, "内陆17省动力煤终端用户供耗存") {
  102. err = Inland(path)
  103. } else if strings.Contains(path, "沿海八省动力煤终端用户供耗存数据更新") {
  104. err = Coastal(path)
  105. } else if strings.Contains(path, "442家晋陕蒙历史数据") {
  106. err = JsmHistory(path)
  107. } else if strings.Contains(path, "CⅢ-8-16 25省市库存和日耗情况") {
  108. err = CoastalHistory(path)
  109. time.Sleep(time.Second * 10)
  110. err = InlandHistory(path)
  111. } else if strings.Contains(path, "分企业煤炭产量旬度数据") {
  112. err = Firm(path)
  113. }
  114. }
  115. } else {
  116. if strings.Contains(path, "442家晋陕蒙煤矿周度产量数据") {
  117. err = Jsm(path)
  118. } else if strings.Contains(path, "内陆17省动力煤终端用户供耗存") {
  119. err = Inland(path)
  120. } else if strings.Contains(path, "沿海八省动力煤终端用户供耗存数据更新") {
  121. err = Coastal(path)
  122. } else if strings.Contains(path, "442家晋陕蒙历史数据") {
  123. err = JsmHistory(path)
  124. } else if strings.Contains(path, "CⅢ-8-16 25省市库存和日耗情况") {
  125. err = CoastalHistory(path)
  126. time.Sleep(time.Second * 10)
  127. err = InlandHistory(path)
  128. } else if strings.Contains(path, "分企业煤炭产量旬度数据") {
  129. err = Firm(path)
  130. }
  131. }
  132. cacheClient.Delete(path)
  133. cacheClient.Set(path, modifyTimeStr, 24*time.Hour)
  134. }
  135. return nil
  136. })
  137. }