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.5 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. Copyright (c) [2023] [pcm]
  3. [pcm-coordinator] is licensed under Mulan PSL v2.
  4. You can use this software according to the terms and conditions of the Mulan PSL v2.
  5. You may obtain a copy of Mulan PSL v2 at:
  6. http://license.coscl.org.cn/MulanPSL2
  7. THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
  8. EITHER EXPaRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
  9. MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
  10. See the Mulan PSL v2 for more details.
  11. */
  12. package cron
  13. import (
  14. "github.com/zeromicro/go-zero/core/logx"
  15. "gitlink.org.cn/jcce-pcm/pcm-coordinator/pkg/constants"
  16. "gitlink.org.cn/jcce-pcm/pcm-coordinator/pkg/models"
  17. "gitlink.org.cn/jcce-pcm/pcm-coordinator/rpc/internal/svc"
  18. "gorm.io/gorm"
  19. "strings"
  20. )
  21. func InitCron(svc *svc.ServiceContext) {
  22. svc.Cron.Start()
  23. svc.Cron.AddFunc("*/5 * * * * ?", func() {
  24. var tasks []models.Task
  25. svc.DbEngin.Where("status not in ?", []string{constants.Deleted, constants.Succeeded, constants.Completed, constants.Failed}).Find(&tasks)
  26. for _, task := range tasks {
  27. var allStatus string
  28. 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)
  29. if tx.Error != nil {
  30. logx.Error(tx.Error)
  31. }
  32. // 子状态统一则修改主任务状态
  33. statusArray := strings.Split(allStatus, ",")
  34. if len(removeRepeatedElement(statusArray)) == 1 {
  35. updateTask(svc.DbEngin, &task, statusArray[0])
  36. continue
  37. }
  38. // 子任务包含失败状态 主任务则失败
  39. if strings.Contains(allStatus, constants.Failed) {
  40. updateTask(svc.DbEngin, &task, constants.Failed)
  41. continue
  42. }
  43. if strings.Contains(allStatus, constants.Running) {
  44. updateTask(svc.DbEngin, &task, constants.Running)
  45. }
  46. }
  47. })
  48. }
  49. func updateTask(dbEngin *gorm.DB, task *models.Task, status string) {
  50. if task.Status != status {
  51. task.Status = status
  52. dbEngin.Updates(&task)
  53. }
  54. }
  55. func removeRepeatedElement(arr []string) (newArr []string) {
  56. newArr = make([]string, 0)
  57. for i := 0; i < len(arr); i++ {
  58. repeat := false
  59. for j := i + 1; j < len(arr); j++ {
  60. if arr[i] == arr[j] {
  61. repeat = true
  62. break
  63. }
  64. }
  65. if !repeat {
  66. newArr = append(newArr, arr[i])
  67. }
  68. }
  69. return
  70. }

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.