task.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package job
  2. import (
  3. "GtDataStore/app/model"
  4. "context"
  5. "errors"
  6. "fmt"
  7. "github.com/zeromicro/go-zero/core/logx"
  8. "time"
  9. )
  10. // Task 提供项目的状态、信息,为子任务提供共享空间
  11. type Task struct {
  12. Id uint32 `json:"id"`
  13. Name string `json:"name"`
  14. Model *model.DcProjectConfig `json:"model"`
  15. handler func(ctx context.Context, task *Task) error
  16. Job *Job
  17. sigStart chan string
  18. status uint8 // 运行状态 0:未运行 1:运行中 2:待退出
  19. ctx context.Context
  20. cancelFunc context.CancelFunc
  21. }
  22. func NewProjectTask(id uint32, name string, job *Job, model model.DcProjectConfig) *Task {
  23. ctx, cancelFunc := context.WithCancel(context.Background())
  24. np := &Task{
  25. Id: id,
  26. Name: name,
  27. Model: &model,
  28. Job: job,
  29. handler: Handler,
  30. status: 0,
  31. sigStart: make(chan string),
  32. ctx: ctx,
  33. cancelFunc: cancelFunc,
  34. }
  35. // 等待开始信号
  36. go np.run()
  37. return np
  38. }
  39. func (p *Task) run() error {
  40. select {
  41. case <-p.sigStart:
  42. p.status = 1
  43. }
  44. err := p.handler(p.ctx, p)
  45. if err != nil {
  46. logx.Infof("project task simulations %d, %s, %s", p.Id, p.Name, err.Error())
  47. }
  48. return err
  49. }
  50. func (p *Task) Start(msg string) {
  51. p.sigStart <- msg
  52. }
  53. func (p *Task) Quit() {
  54. p.status = 2
  55. p.cancelFunc()
  56. }
  57. func (p *Task) IsRun() bool {
  58. return p.status == 1
  59. }
  60. func (p *Task) Test() error {
  61. if p.Job == nil {
  62. return errors.New("not found Job")
  63. }
  64. if p.Model == nil {
  65. return errors.New("not found project model")
  66. }
  67. if p.handler == nil {
  68. return errors.New("not set simulations")
  69. }
  70. if len(p.Model.Technologys) == 0 {
  71. return errors.New("not set technologys list, modify technologys field for mysql cd_project_config")
  72. }
  73. for _, technology := range p.Model.Technologys {
  74. if _, ok := p.Job.handlerTable[technology]; !ok {
  75. fmt.Printf("project: %d, %s no support", p.Id, technology)
  76. continue
  77. //return errors.New(fmt.Sprintf("project: %d, %s no support", p.Id, technology))
  78. }
  79. }
  80. return nil
  81. }
  82. func Handler(ctx context.Context, task *Task) error {
  83. for {
  84. select {
  85. case <-ctx.Done():
  86. return nil
  87. default:
  88. return distribute(ctx, task)
  89. }
  90. }
  91. }
  92. // 将一个项目需要处理的所有工艺进行分发
  93. func distribute(ctx context.Context, task *Task) error {
  94. fmt.Printf("distribute task for: %+v", task.Model.Technologys)
  95. for _, technology := range task.Model.Technologys {
  96. interval, ok := task.Job.handlerIntervalTable[technology]
  97. if !ok {
  98. fmt.Printf("project: %d, %s no support", task.Id, technology)
  99. continue
  100. //return errors.New(fmt.Sprintf("project: %d, %s no support", task.Id, technology))
  101. }
  102. f, ok := task.Job.handlerTable[technology]
  103. if !ok {
  104. return errors.New(fmt.Sprintf("project: %d, %s no bind simulations", task.Id, technology))
  105. }
  106. technologyName := technology
  107. go func(interval time.Duration, call func(ctx context.Context, task *Task, technologyName string) error) {
  108. tk := time.NewTicker(interval)
  109. for {
  110. select {
  111. case <-ctx.Done():
  112. return
  113. case <-tk.C:
  114. // 这里是同步执行的,避免并发问题
  115. err := call(ctx, task, technologyName)
  116. if err != nil {
  117. fmt.Printf("distribute.call error: %s\n", err.Error())
  118. }
  119. //} else {
  120. // fmt.Print("distribute.call finish\n")
  121. //}
  122. }
  123. }
  124. }(interval, f)
  125. }
  126. return nil
  127. }