蛋蛋星球 后台端
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.
 
 
 
 

106 rivejä
3.7 KiB

  1. package task
  2. import (
  3. "applet/app/db"
  4. taskMd "applet/app/task/md"
  5. "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
  6. "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
  7. "fmt"
  8. "time"
  9. "applet/app/utils/logx"
  10. "github.com/robfig/cron/v3"
  11. "xorm.io/xorm"
  12. )
  13. var (
  14. timer *cron.Cron
  15. jobs = map[string]func(*xorm.Engine){}
  16. baseEntryId cron.EntryID
  17. entryIds []cron.EntryID
  18. taskCfgList *[]model.CronTask
  19. ch = make(chan int, 30)
  20. workerNum = 15 // 并发数量
  21. )
  22. func Init() {
  23. // 初始化任务列表
  24. initTasks()
  25. var err error
  26. timer = cron.New()
  27. // reload为初始化数据库方法
  28. if baseEntryId, err = timer.AddFunc("@every 30m", reload); err != nil {
  29. _ = logx.Fatal(err)
  30. }
  31. }
  32. func Run() {
  33. reload()
  34. timer.Start()
  35. _ = logx.Info("auto tasks running...")
  36. }
  37. func reload() {
  38. cronTaskDb := implement.NewCronTaskDb(db.Db)
  39. taskCfgList = cronTaskDb.MapCrontabCfg()
  40. fmt.Println("taskCfgList>>>>>>>>>>>>>", taskCfgList)
  41. if len(*taskCfgList) > 0 {
  42. // 删除原有所有任务
  43. if len(entryIds) > 0 {
  44. for _, v := range entryIds {
  45. if v != baseEntryId {
  46. timer.Remove(v)
  47. }
  48. }
  49. entryIds = nil
  50. }
  51. var (
  52. entryId cron.EntryID
  53. err error
  54. )
  55. // 添加任务
  56. for _, v := range *taskCfgList {
  57. if _, ok := jobs[v.Key]; ok && v.Val != "" {
  58. fmt.Println(v.Val)
  59. if entryId, err = timer.AddFunc(v.Val, doTask(v.Key)); err == nil {
  60. entryIds = append(entryIds, entryId)
  61. }
  62. }
  63. }
  64. }
  65. }
  66. func doTask(fnName string) func() {
  67. return func() {
  68. begin := time.Now().Local()
  69. jobs[fnName](db.Db)
  70. end := time.Now().Local()
  71. logx.Infof(
  72. "[%s] AutoTask <%s> started at <%s>, ended at <%s> duration <%s>",
  73. fnName,
  74. begin.Format("2006-01-02 15:04:05.000"),
  75. end.Format("2006-01-02 15:04:05.000"),
  76. time.Duration(end.UnixNano()-begin.UnixNano()).String(),
  77. )
  78. }
  79. }
  80. // 增加自动任务队列
  81. func initTasks() {
  82. jobs[taskMd.CronEggEnergyAutoRecordPrices] = taskEggEnergyAutoRecordPrices
  83. jobs[taskMd.CronEggEnergyDealPlatformRevenueData] = taskEggEnergyDealPlatformRevenueData
  84. jobs[taskMd.CronEggEnergyDealFundData] = taskEggEnergyDealFundData
  85. jobs[taskMd.CronEggEnergyUserEggScore] = taskCronEggEnergyUserEggScore
  86. jobs[taskMd.CornEggEnergyAutoReleaseTeamEggEnergy] = taskEggEnergyAutoReleaseTeamEggEnergy // 蛋蛋能量-自动释放团队蛋蛋能量到个人能量
  87. jobs[taskMd.CornSettlementStarLevelDividends] = taskEggEnergySettlementStarLevelDividends // 蛋蛋能量-结算星级分红
  88. jobs[taskMd.CornEggEnergyDealUserPublicPlatoonPunish] = taskEggEnergyDealUserPublicPlatoonPunish // 蛋蛋能量-公排处罚
  89. jobs[taskMd.CornEggEnergyAutoAdjustPrice] = taskEggEnergyAutoAdjustPrice // 蛋蛋能量-自动调整价格
  90. jobs[taskMd.CornEggEnergyAutoCommunityDividends] = taskEggEnergyCommunityDividends // 蛋蛋能量-社区分红
  91. jobs[taskMd.CornEggEnergyAutomaticScoring] = taskEggEnergyAutomaticScoring // 蛋蛋能量-自动打分
  92. jobs[taskMd.CornEggEnergyAutoCachingEggPointStatistics] = taskAutoCachingEggPointStatistics // 蛋蛋分统计落地页-缓存
  93. jobs[taskMd.CornEggEnergyAutoUpdateUserAccess] = taskEggEnergyAutoUpdateUserAccess // es蛋蛋分记录-自动更新访问次数
  94. jobs[taskMd.CornEggEnergyAutoDeleteTableAdvertisingCallback] = taskAutoDeleteTableAdvertisingCallback // 定时清除七天以前的广告回调数据
  95. jobs[taskMd.CornEggEnergyCoinFlow] = taskEggEnergyCoinFlow // 定时重置用户流水聚合信息
  96. jobs[taskMd.CornEggEnergyAutoCachingTop100] = taskAutoCachingTop100 // 英雄榜缓存前一百数据
  97. }