task.go 7.6 KB

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