task.go 4.5 KB

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