task_starter.go 1.3 KB

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