task.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. //var ctx context.Context
  69. //go ccfService.ReadEdbHistoryFiles(ctx)
  70. }
  71. task.StartTask()
  72. fmt.Println("task end")
  73. }
  74. func ReadWatchIndexFile() {
  75. fmt.Println("ReadWatchIndexFile start")
  76. var err error
  77. defer func() {
  78. if err != nil {
  79. fmt.Println("ReadWatchIndexFile Err:" + err.Error())
  80. }
  81. }()
  82. var cacheClient *cache.Cache
  83. if cacheClient == nil {
  84. cacheClient = cache.New(365*24*time.Hour, 365*24*time.Hour)
  85. }
  86. err = filepath.Walk(utils.CoalMineFilePath, func(path string, info fs.FileInfo, err error) error {
  87. if err != nil {
  88. return err
  89. }
  90. if !info.IsDir() {
  91. fileInfo, err := os.Stat(path)
  92. if err != nil {
  93. fmt.Println("os.Stat:", err.Error())
  94. }
  95. winFileAttr := fileInfo.Sys().(*syscall.Win32FileAttributeData)
  96. modifyTimeStr := utils.SecondToTime(winFileAttr.LastWriteTime.Nanoseconds() / 1e9).Format(utils.FormatDateTime)
  97. existModifyTime, ok := cacheClient.Get(path)
  98. if ok {
  99. existModifyTimeStr := existModifyTime.(string)
  100. if existModifyTimeStr != modifyTimeStr {
  101. if strings.Contains(path, "442家晋陕蒙煤矿周度产量数据") {
  102. err = Jsm(path)
  103. } else if strings.Contains(path, "内陆17省动力煤终端用户供耗存") {
  104. err = Inland(path)
  105. } else if strings.Contains(path, "沿海八省动力煤终端用户供耗存数据更新") {
  106. err = Coastal(path)
  107. } else if strings.Contains(path, "442家晋陕蒙历史数据") {
  108. err = JsmHistory(path)
  109. } else if strings.Contains(path, "CⅢ-8-16 25省市库存和日耗情况") {
  110. err = CoastalHistory(path)
  111. time.Sleep(time.Second * 10)
  112. err = InlandHistory(path)
  113. } else if strings.Contains(path, "分企业煤炭产量旬度数据") {
  114. err = Firm(path)
  115. }
  116. }
  117. } else {
  118. if strings.Contains(path, "442家晋陕蒙煤矿周度产量数据") {
  119. err = Jsm(path)
  120. } else if strings.Contains(path, "内陆17省动力煤终端用户供耗存") {
  121. err = Inland(path)
  122. } else if strings.Contains(path, "沿海八省动力煤终端用户供耗存数据更新") {
  123. err = Coastal(path)
  124. } else if strings.Contains(path, "442家晋陕蒙历史数据") {
  125. err = JsmHistory(path)
  126. } else if strings.Contains(path, "CⅢ-8-16 25省市库存和日耗情况") {
  127. err = CoastalHistory(path)
  128. time.Sleep(time.Second * 10)
  129. err = InlandHistory(path)
  130. } else if strings.Contains(path, "分企业煤炭产量旬度数据") {
  131. err = Firm(path)
  132. }
  133. }
  134. cacheClient.Delete(path)
  135. cacheClient.Set(path, modifyTimeStr, 24*time.Hour)
  136. }
  137. return nil
  138. })
  139. }