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.

pushtaskinfologic.go 8.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package core
  2. import (
  3. "context"
  4. "github.com/pkg/errors"
  5. "github.com/zeromicro/go-zero/core/logx"
  6. clientCore "gitlink.org.cn/JointCloud/pcm-coordinator/client"
  7. "gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc"
  8. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/constants"
  9. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models"
  10. "gorm.io/gorm"
  11. "strings"
  12. "time"
  13. )
  14. type PushTaskInfoLogic struct {
  15. logx.Logger
  16. ctx context.Context
  17. svcCtx *svc.ServiceContext
  18. }
  19. func NewPushTaskInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PushTaskInfoLogic {
  20. return &PushTaskInfoLogic{
  21. Logger: logx.WithContext(ctx),
  22. ctx: ctx,
  23. svcCtx: svcCtx,
  24. }
  25. }
  26. func (l *PushTaskInfoLogic) PushTaskInfo(req *clientCore.PushTaskInfoReq) (*clientCore.PushTaskInfoResp, error) {
  27. resp := clientCore.PushTaskInfoResp{}
  28. var kind int32
  29. l.svcCtx.DbEngin.Raw("select type as kind from t_adapter where id = ?", req.AdapterId).Scan(&kind)
  30. switch kind {
  31. case 0:
  32. var resourceType int32
  33. l.svcCtx.DbEngin.Raw("select resource_type as resourceType from `t_adapter` where id = ?", req.AdapterId).Scan(&resourceType)
  34. switch resourceType {
  35. case 01:
  36. for _, cloudInfo := range req.CloudInfoList {
  37. var taskId uint
  38. result := l.svcCtx.DbEngin.Table("task_cloud").Select("task_id").Where("task_id = ?", cloudInfo.TaskId).Find(&taskId)
  39. if errors.Is(result.Error, gorm.ErrRecordNotFound) {
  40. return nil, errors.New("Record does not exist")
  41. }
  42. l.svcCtx.DbEngin.Exec("update task_cloud set status = ?,start_time = ?,result = ? where task_id = ?",
  43. cloudInfo.Status, cloudInfo.StartTime, cloudInfo.Result, cloudInfo.TaskId)
  44. var taskName string
  45. l.svcCtx.DbEngin.Raw("select name as kind from task where id = ?", taskId).Scan(&taskName)
  46. noticeInfo := clientCore.NoticeInfo{
  47. TaskId: cloudInfo.TaskId,
  48. AdapterId: cloudInfo.AdapterId,
  49. AdapterName: cloudInfo.AdapterName,
  50. ClusterId: cloudInfo.ClusterId,
  51. ClusterName: cloudInfo.ClusterName,
  52. TaskName: taskName,
  53. }
  54. syncTask(l.svcCtx.DbEngin, noticeInfo)
  55. }
  56. case 02:
  57. for _, vmInfo := range req.VmInfoList {
  58. l.svcCtx.DbEngin.Exec("update task_vm set status = ?,start_time = ?,server_id = ? where cluster_id = ? and task_id = ? and name = ?",
  59. vmInfo.Status, vmInfo.StartTime, vmInfo.ServerId, vmInfo.ClusterId, vmInfo.TaskId, vmInfo.Name)
  60. noticeInfo := clientCore.NoticeInfo{
  61. TaskId: vmInfo.TaskId,
  62. AdapterId: vmInfo.AdapterId,
  63. AdapterName: vmInfo.AdapterName,
  64. ClusterId: vmInfo.ClusterId,
  65. ClusterName: vmInfo.ClusterName,
  66. TaskName: vmInfo.Name,
  67. }
  68. syncTask(l.svcCtx.DbEngin, noticeInfo)
  69. }
  70. }
  71. case 2:
  72. for _, hpcInfo := range req.HpcInfoList {
  73. l.svcCtx.DbEngin.Exec("update task_hpc set status = ?,start_time = ?,job_id = ? where cluster_id = ? and task_id = ? and name = ?",
  74. hpcInfo.Status, hpcInfo.StartTime, hpcInfo.JobId, hpcInfo.ClusterId, hpcInfo.TaskId, hpcInfo.Name)
  75. noticeInfo := clientCore.NoticeInfo{
  76. TaskId: hpcInfo.TaskId,
  77. AdapterId: hpcInfo.AdapterId,
  78. AdapterName: hpcInfo.AdapterName,
  79. ClusterId: hpcInfo.ClusterId,
  80. ClusterName: hpcInfo.ClusterName,
  81. TaskName: hpcInfo.Name,
  82. }
  83. syncTask(l.svcCtx.DbEngin, noticeInfo)
  84. }
  85. case 1:
  86. for _, aiInfo := range req.AiInfoList {
  87. l.svcCtx.DbEngin.Exec("update task_ai_asynchronous set status = ?,start_time = ?,job_id = ? where cluster_id = ? and task_id = ? and name = ?",
  88. aiInfo.Status, aiInfo.StartTime, aiInfo.JobId, aiInfo.ClusterId, aiInfo.TaskId, aiInfo.Name)
  89. noticeInfo := clientCore.NoticeInfo{
  90. TaskId: aiInfo.TaskId,
  91. AdapterId: aiInfo.AdapterId,
  92. AdapterName: aiInfo.AdapterName,
  93. ClusterId: aiInfo.ClusterId,
  94. ClusterName: aiInfo.ClusterName,
  95. TaskName: aiInfo.Name,
  96. }
  97. syncTask(l.svcCtx.DbEngin, noticeInfo)
  98. }
  99. }
  100. return &resp, nil
  101. }
  102. func syncTask(gorm *gorm.DB, noticeInfo clientCore.NoticeInfo) {
  103. var allStatus string
  104. tx := gorm.Raw("SELECT CONCAT_WS(',',GROUP_CONCAT(DISTINCT h.status) ,GROUP_CONCAT(DISTINCT a.status) ,GROUP_CONCAT(DISTINCT c.status),GROUP_CONCAT(DISTINCT v.status),GROUP_CONCAT(DISTINCT as.status))as status from task t left join task_hpc h on t.id = h.task_id left join task_cloud c on t.id = c.task_id left join task_vm v on t.id = v.task_id left join task_ai a on t.id = a.task_id where t.id left join task_ai_asynchronous as on t.id = as.task_id = ?", noticeInfo.TaskId).Scan(&allStatus)
  105. if tx.Error != nil {
  106. logx.Error(tx.Error)
  107. }
  108. allStatus = strings.ToUpper(allStatus)
  109. for pcmStatus, ProviderStatus := range clientCore.StatusMapping {
  110. for _, originalStatus := range ProviderStatus {
  111. // if Failed type status appears in subTask then update mainTask to Failed
  112. if pcmStatus == "Failed" && strings.Contains(allStatus, originalStatus) {
  113. updateTask(gorm, noticeInfo.TaskId, constants.Failed)
  114. noticeInfo := clientCore.NoticeInfo{
  115. AdapterId: noticeInfo.AdapterId,
  116. AdapterName: noticeInfo.AdapterName,
  117. ClusterId: noticeInfo.ClusterId,
  118. ClusterName: noticeInfo.ClusterName,
  119. NoticeType: "failed",
  120. TaskName: noticeInfo.TaskName,
  121. Incident: "任务执行失败,请查看日志!",
  122. CreatedTime: time.Now(),
  123. }
  124. gorm.Table("t_notice").Create(&noticeInfo)
  125. return
  126. // no Failed type status in subTask,if Saved type status appears in subTask then update mainTask to Saved
  127. } else if pcmStatus == "Saved" && strings.Contains(allStatus, originalStatus) {
  128. if getTaskStatus(gorm, noticeInfo.TaskId) != "Saved" {
  129. updateTask(gorm, noticeInfo.TaskId, constants.Saved)
  130. noticeInfo := clientCore.NoticeInfo{
  131. AdapterId: noticeInfo.AdapterId,
  132. AdapterName: noticeInfo.AdapterName,
  133. ClusterId: noticeInfo.ClusterId,
  134. ClusterName: noticeInfo.ClusterName,
  135. NoticeType: "saved",
  136. TaskName: noticeInfo.TaskName,
  137. Incident: "任务已处于队列中!",
  138. CreatedTime: time.Now(),
  139. }
  140. gorm.Table("t_notice").Create(&noticeInfo)
  141. return
  142. } else {
  143. return
  144. }
  145. // no Failed and Saved type status in subTask,if Running type status appears in subTask then update mainTask to Running
  146. } else if pcmStatus == "Running" && strings.Contains(allStatus, originalStatus) {
  147. if getTaskStatus(gorm, noticeInfo.TaskId) != "Running" {
  148. updateTask(gorm, noticeInfo.TaskId, constants.Running)
  149. noticeInfo := clientCore.NoticeInfo{
  150. AdapterId: noticeInfo.AdapterId,
  151. AdapterName: noticeInfo.AdapterName,
  152. ClusterId: noticeInfo.ClusterId,
  153. ClusterName: noticeInfo.ClusterName,
  154. NoticeType: "running",
  155. TaskName: noticeInfo.TaskName,
  156. Incident: "任务状态切换为运行中!",
  157. CreatedTime: time.Now(),
  158. }
  159. gorm.Table("t_notice").Create(&noticeInfo)
  160. return
  161. } else {
  162. return
  163. }
  164. // at last, mainTask should be succeeded
  165. } else {
  166. if strings.Contains(allStatus, originalStatus) {
  167. updateTask(gorm, noticeInfo.TaskId, constants.Succeeded)
  168. noticeInfo := clientCore.NoticeInfo{
  169. AdapterId: noticeInfo.AdapterId,
  170. AdapterName: noticeInfo.AdapterName,
  171. ClusterId: noticeInfo.ClusterId,
  172. ClusterName: noticeInfo.ClusterName,
  173. NoticeType: "succeeded",
  174. TaskName: noticeInfo.TaskName,
  175. Incident: "任务执行完成!",
  176. CreatedTime: time.Now(),
  177. }
  178. gorm.Table("t_notice").Create(&noticeInfo)
  179. return
  180. }
  181. }
  182. }
  183. }
  184. }
  185. func updateTask(gorm *gorm.DB, taskId int64, status string) {
  186. now := time.Now()
  187. var task models.Task
  188. gorm.Where("id = ? ", taskId).Find(&task)
  189. if task.Status != status {
  190. task.Status = status
  191. if status == constants.Running {
  192. task.StartTime = &now
  193. }
  194. if task.Status == constants.Failed || task.Status == constants.Succeeded {
  195. task.EndTime = &now
  196. }
  197. gorm.Updates(&task)
  198. }
  199. }
  200. func getTaskStatus(gorm *gorm.DB, taskId int64) (status string) {
  201. var task models.Task
  202. gorm.Where("id = ? ", taskId).Find(&task)
  203. return task.Status
  204. }

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.