task_starter.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package task
  2. import (
  3. "eta/eta_mini_ht_api/common/component/config"
  4. "eta/eta_mini_ht_api/common/contants"
  5. "eta/eta_mini_ht_api/task/base"
  6. _ "eta/eta_mini_ht_api/task/eta/author"
  7. _ "eta/eta_mini_ht_api/task/eta/permission"
  8. _ "eta/eta_mini_ht_api/task/message"
  9. _ "eta/eta_mini_ht_api/task/report"
  10. _ "eta/eta_mini_ht_api/task/sms"
  11. "github.com/beego/beego/v2/server/web"
  12. "github.com/beego/beego/v2/task"
  13. )
  14. func AddTasksToScheduler() {
  15. for _, t := range base.GetTasks() {
  16. if needAddTask(t.RunMode, web.BConfig.RunMode) {
  17. tk := task.NewTask(string(t.Name), t.Cron, t.Run)
  18. task.AddTask(string(t.Name), tk)
  19. }
  20. }
  21. }
  22. func needAddTask(taskRunMode string, runMode string) bool {
  23. if taskRunMode == base.PROD {
  24. return true
  25. }
  26. if taskRunMode == base.TEST {
  27. if runMode == base.PROD {
  28. return false
  29. }
  30. return true
  31. }
  32. if taskRunMode == base.DEV {
  33. if runMode == base.DEV {
  34. return true
  35. }
  36. return false
  37. }
  38. if taskRunMode == base.FORBIDDEN {
  39. return false
  40. }
  41. return false
  42. }
  43. // StartScheduler 启动任务调度器
  44. func StartScheduler() {
  45. htConfig := config.GetConfig(contants.HT).(*config.HTBizConfig)
  46. if htConfig.EnableTask() {
  47. task.StartTask()
  48. }
  49. }
  50. func init() {
  51. AddTasksToScheduler()
  52. StartScheduler()
  53. }