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

cronlogic.go 2.0 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package cron
  2. import (
  3. "github.com/zeromicro/go-zero/core/logx"
  4. "gitlink.org.cn/jcce-pcm/pcm-coordinator/pkg/constants"
  5. "gitlink.org.cn/jcce-pcm/pcm-coordinator/pkg/models"
  6. "gitlink.org.cn/jcce-pcm/pcm-coordinator/rpc/internal/svc"
  7. "gorm.io/gorm"
  8. "strings"
  9. )
  10. func InitCron(svc *svc.ServiceContext) {
  11. svc.Cron.Start()
  12. svc.Cron.AddFunc("*/5 * * * * ?", func() {
  13. var tasks []models.Task
  14. svc.DbEngin.Where("status not in ?", []string{constants.Deleted, constants.Succeeded, constants.Completed, constants.Failed}).Find(&tasks)
  15. for _, task := range tasks {
  16. var allStatus string
  17. tx := svc.DbEngin.Raw("SELECT CONCAT_WS(',',GROUP_CONCAT(DISTINCT h.status) ,GROUP_CONCAT(DISTINCT a.status) ,GROUP_CONCAT(DISTINCT c.status))as status from task t left join hpc h on t.id = h.task_id left join cloud c on t.id = c.task_id left join ai a on t.id = a.task_id where t.id = ?", task.Id).Scan(&allStatus)
  18. if tx.Error != nil {
  19. logx.Error(tx.Error)
  20. }
  21. // 子状态统一则修改主任务状态
  22. statusArray := strings.Split(allStatus, ",")
  23. if len(removeRepeatedElement(statusArray)) == 1 {
  24. updateTask(svc.DbEngin, &task, statusArray[0])
  25. continue
  26. }
  27. // 子任务包含失败状态 主任务则失败
  28. if strings.Contains(allStatus, constants.Failed) {
  29. updateTask(svc.DbEngin, &task, constants.Failed)
  30. continue
  31. }
  32. if strings.Contains(allStatus, constants.Running) {
  33. updateTask(svc.DbEngin, &task, constants.Running)
  34. }
  35. }
  36. })
  37. }
  38. func updateTask(dbEngin *gorm.DB, task *models.Task, status string) {
  39. if task.Status != status {
  40. task.Status = status
  41. dbEngin.Updates(&task)
  42. }
  43. }
  44. func removeRepeatedElement(arr []string) (newArr []string) {
  45. newArr = make([]string, 0)
  46. for i := 0; i < len(arr); i++ {
  47. repeat := false
  48. for j := i + 1; j < len(arr); j++ {
  49. if arr[i] == arr[j] {
  50. repeat = true
  51. break
  52. }
  53. }
  54. if !repeat {
  55. newArr = append(newArr, arr[i])
  56. }
  57. }
  58. return
  59. }

PCM is positioned as Software stack over Cloud, aiming to build the standards and ecology of heterogeneous cloud collaboration for JCC in a non intrusive and autonomous peer-to-peer manner.