task.go 6.7 KB

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