task.go 6.5 KB

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