智盟项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
4.9 KiB

  1. package task
  2. import (
  3. "applet/app/db"
  4. taskMd "applet/app/task/md"
  5. "applet/app/utils/logx"
  6. "github.com/robfig/cron/v3"
  7. "time"
  8. )
  9. var (
  10. timer *cron.Cron
  11. jobs = map[string]func(){}
  12. baseEntryId cron.EntryID
  13. entryIds []cron.EntryID
  14. ch = make(chan int, 30)
  15. workerNum = 15 // 智盟跟单并发数量
  16. )
  17. func Init() {
  18. // 初始化任务列表
  19. initTasks()
  20. var err error
  21. timer = cron.New()
  22. // reload为初始化数据库方法
  23. if baseEntryId, err = timer.AddFunc("@every 15m", reload); err != nil {
  24. _ = logx.Fatal(err)
  25. }
  26. }
  27. func Run() {
  28. reload()
  29. timer.Start()
  30. _ = logx.Info("auto tasks running...")
  31. }
  32. func reload() {
  33. // 重新初始化数据库
  34. sysCfgDb := db.SysCfgDb{}
  35. sysCfgDb.Set()
  36. cronList, _ := sysCfgDb.SysCfgGetCron()
  37. if cronList != nil {
  38. // 删除原有所有任务
  39. if len(entryIds) > 0 {
  40. for _, v := range entryIds {
  41. if v != baseEntryId {
  42. timer.Remove(v)
  43. }
  44. }
  45. entryIds = nil
  46. }
  47. var (
  48. entryId cron.EntryID
  49. err error
  50. )
  51. for _, vv := range *cronList {
  52. if _, ok := jobs[vv.K]; ok && vv.V != "" {
  53. // fmt.Println(vv.Val)
  54. if entryId, err = timer.AddFunc(vv.V, doTask(vv.K)); err == nil {
  55. entryIds = append(entryIds, entryId)
  56. }
  57. }
  58. }
  59. }
  60. }
  61. func doTask(fnName string) func() {
  62. return func() {
  63. begin := time.Now().Local()
  64. jobs[fnName]()
  65. end := time.Now().Local()
  66. logx.Infof(
  67. " AutoTask <%s> started at <%s>, ended at <%s> duration <%s>",
  68. fnName,
  69. begin.Format("2006-01-02 15:04:05.000"),
  70. end.Format("2006-01-02 15:04:05.000"),
  71. time.Duration(end.UnixNano()-begin.UnixNano()).String(),
  72. )
  73. }
  74. }
  75. // 增加自动任务队列
  76. func initTasks() {
  77. jobs[taskMd.ZhimengCronTikTokLocalLifeOrderSettle] = taskTikTokLocalLifeOrderSettle //c
  78. jobs[taskMd.ZhimengCronChanxuanOrderSettle] = taskChanxuanOrderSettle //c
  79. jobs[taskMd.ZhimengCronPLayletBihaiOrder] = taskPlayletBihaiOrder //
  80. jobs[taskMd.ZhimengCronCinema5bmsOrder] = taskCinema5bmsOrder //
  81. jobs[taskMd.ZhimengCronCinema5bmsYesterOrder] = taskCinema5bmsYesterOrder //
  82. jobs[taskMd.ZhimengCronTikTokCsjp] = taskTikTokCsjp //
  83. jobs[taskMd.ZhimengCronTikTokCsjpActivity] = taskTikTokCsjpActivity //
  84. jobs[taskMd.ZhimengCronTikTokCsjpLive] = taskTikTokCsjpLive //
  85. jobs[taskMd.ZhimengCronTikTokChanxuan] = taskTikTokCsjpChanxuan //
  86. jobs[taskMd.ZhimengCronTikTokChanxuanPay] = taskTikTokCsjpChanxuanPay //
  87. jobs[taskMd.ZhimengCronTikTokTask] = taskTikTokTask //
  88. jobs[taskMd.ZhimengCronTikTokTaskRelate] = taskTikTokTaskRelate //
  89. jobs[taskMd.ZhimengCronTikTokTaskMoney] = taskTikTokTaskMoney //
  90. jobs[taskMd.ZhimengCronTikTokTaskMoneyByYestday] = taskTikTokTaskMoneyByYestday //
  91. jobs[taskMd.ZhimengCronDianzhongKuaishouMoney] = taskDianzhongKuaishouMoney //
  92. jobs[taskMd.ZhimengCronPlayletOrderSettle] = taskPlayletOrderSettle //
  93. jobs[taskMd.ZhimengCronHwOrderSettle] = taskHwOrderSettle //
  94. jobs[taskMd.ZhimengCronMasterMonthSettle] = taskMasterMonthSettle //
  95. jobs[taskMd.ZhimengCronHwStarbucksOrder] = taskHwStarbucksOrder //
  96. jobs[taskMd.ZhimengCronHwMovieOrder] = taskHwMovieOrder //
  97. jobs[taskMd.ZhimengCronHwMcdonaldOrder] = taskHwMcdonaldOrder //
  98. jobs[taskMd.ZhimengCronHwNayukiOrder] = taskHwNayukiOrder //
  99. jobs[taskMd.ZhimengCronHwLuckinOrder] = taskHwLuckinOrder //
  100. jobs[taskMd.ZhimengCronHwPizzaOrder] = taskHwPizzaOrder //
  101. jobs[taskMd.ZhimengCronHwPagodaOrder] = taskHwPagodaOrder //
  102. jobs[taskMd.ZhimengCronHwBurgerKingOrder] = taskHwBurgerKingOrder //
  103. jobs[taskMd.ZhimengCronHwHeyteaOrder] = taskHwHeyteaOrder //
  104. jobs[taskMd.ZhimengCronHwToKfcOrder] = taskHwToKfcOrder //
  105. jobs[taskMd.ZhimengCronHwWallaceOrder] = taskHwWallaceOrder //
  106. jobs[taskMd.ZhimengCronHwFlowerCakeOrder] = taskHwFlowerCakeOrder //
  107. jobs[taskMd.ZhimengCronHwDeliveryOrder] = taskHwDeliveryOrder //
  108. jobs[taskMd.ZhimengCronHwTourismOrder] = taskHwTourismOrder //
  109. jobs[taskMd.ZhimengCronTikTokLocalLifeOrder] = taskTikTokLocalLifeOrder //
  110. jobs[taskMd.ZhimengCronCsjpLocalLifeOrder] = taskCsjpLocalLifeOrder //
  111. jobs[taskMd.ZhimengCronKuaishouOrder] = taskKuaishouOrder //
  112. jobs[taskMd.ZhimengCronKuaishouAuth] = taskKuaishouAuth //
  113. jobs[taskMd.ZhimengCronMeituanOrder] = taskMeituanOrder //
  114. jobs[taskMd.ZhimengCronMeituanStoreOrder] = taskMeituanStoreOrder //
  115. jobs[taskMd.ZhimengCronLifeOrderSettle] = taskLifeOrderSettle //
  116. jobs[taskMd.ZhimengCronElmOrder] = taskElmOrder //
  117. jobs[taskMd.ZhimengCronDidiOrder] = taskDidiOrder //
  118. jobs[taskMd.ZhimengCronT3Order] = taskT3Order //
  119. }