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.

template.go 4.1 kB

3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package wechat
  2. import (
  3. "code.gitea.io/gitea/models"
  4. "code.gitea.io/gitea/modules/log"
  5. "code.gitea.io/gitea/modules/setting"
  6. "errors"
  7. "fmt"
  8. "time"
  9. )
  10. type JobOperateType string
  11. const (
  12. JobOperateTypeStart JobOperateType = "start"
  13. JobOperateTypeStop JobOperateType = "stop"
  14. )
  15. func GetJobOperateTypeFromCloudbrainStatus(cloudbrain *models.Cloudbrain) JobOperateType {
  16. if cloudbrain.IsTerminal() {
  17. return JobOperateTypeStop
  18. }
  19. if cloudbrain.IsRunning() {
  20. return JobOperateTypeStart
  21. }
  22. return ""
  23. }
  24. func SendCloudbrainStartedMsg(operateType JobOperateType, cloudbrain models.Cloudbrain) error {
  25. defer func() {
  26. if err := recover(); err != nil {
  27. combinedErr := fmt.Errorf("%s\n%s", err, log.Stack(2))
  28. log.Error("PANIC:", combinedErr)
  29. }
  30. }()
  31. repo, err := models.GetRepositoryByID(cloudbrain.RepoID)
  32. if err != nil {
  33. log.Error("SendCloudbrainStartedMsg GetRepositoryByID error,%v", err)
  34. }
  35. if setting.CloudbrainStartedTemplateId == "" {
  36. return nil
  37. }
  38. openId := models.GetUserWechatOpenId(cloudbrain.UserID)
  39. if openId == "" {
  40. return errors.New("Wechat openId not exist")
  41. }
  42. data := CloudbrainTaskData{
  43. First: TemplateValue{Value: getCloudbrainTemplateTitle(operateType)},
  44. Keyword1: TemplateValue{Value: cloudbrain.DisplayJobName},
  45. Keyword2: TemplateValue{Value: getJobTypeDisplayName(cloudbrain.JobType)},
  46. Keyword3: TemplateValue{Value: time.Unix(int64(cloudbrain.CreatedUnix), 0).Format("2006-01-02 15:04:05")},
  47. Remark: TemplateValue{Value: getCloudbrainTemplateRemark(operateType)},
  48. }
  49. req := TemplateMsgRequest{
  50. ToUser: openId,
  51. TemplateId: setting.CloudbrainStartedTemplateId,
  52. Url: getCloudbrainTemplateUrl(cloudbrain, repo),
  53. ClientMsgId: string(operateType) + "_" + fmt.Sprint(cloudbrain.ID),
  54. Data: data,
  55. }
  56. err, retryFlag := sendTemplateMsg(req)
  57. if retryFlag {
  58. log.Info("retrySendCloudbrainTemplateMsg calling")
  59. refreshAccessToken()
  60. err, _ = sendTemplateMsg(req)
  61. if err != nil {
  62. log.Error("SendCloudbrainStartedMsg err. %v", err)
  63. return err
  64. }
  65. return nil
  66. }
  67. if err != nil {
  68. log.Error("SendCloudbrainStartedMsg err. %v", err)
  69. return err
  70. }
  71. return nil
  72. }
  73. func getCloudbrainTemplateUrl(cloudbrain models.Cloudbrain, repo *models.Repository) string {
  74. url := setting.AppURL + repo.FullName()
  75. switch cloudbrain.JobType {
  76. case string(models.JobTypeDebug):
  77. if cloudbrain.ComputeResource == "CPU/GPU" {
  78. url += "/cloudbrain/" + fmt.Sprint(cloudbrain.ID)
  79. } else {
  80. url += "/modelarts/notebook/" + fmt.Sprint(cloudbrain.ID)
  81. }
  82. case string(models.JobTypeBenchmark):
  83. url += "/cloudbrain/benchmark/" + fmt.Sprint(cloudbrain.ID)
  84. case string(models.JobTypeTrain):
  85. if cloudbrain.Type == models.TypeCloudBrainOne {
  86. url += "/cloudbrain/train-job/" + fmt.Sprint(cloudbrain.JobID)
  87. } else if cloudbrain.Type == models.TypeCloudBrainTwo {
  88. url += "/modelarts/train-job/" + fmt.Sprint(cloudbrain.JobID)
  89. } else if cloudbrain.Type == models.TypeC2Net {
  90. url += "/grampus/train-job/" + fmt.Sprint(cloudbrain.JobID)
  91. }
  92. case string(models.JobTypeInference):
  93. url += "/modelarts/inference-job/" + fmt.Sprint(cloudbrain.JobID)
  94. }
  95. return url
  96. }
  97. func getCloudbrainTemplateTitle(operateType JobOperateType) string {
  98. var title string
  99. switch operateType {
  100. case JobOperateTypeStart:
  101. title = "您好,您提交的算力资源申请已通过,任务已启动,请您关注运行情况。"
  102. case JobOperateTypeStop:
  103. title = "您好,您提交的任务已运行结束。"
  104. }
  105. return title
  106. }
  107. func getCloudbrainTemplateRemark(operateType JobOperateType) string {
  108. var remark string
  109. switch operateType {
  110. case JobOperateTypeStart:
  111. remark = "感谢您的耐心等待。"
  112. case JobOperateTypeStop:
  113. remark = "点击可查看运行结果"
  114. }
  115. return remark
  116. }
  117. func getJobTypeDisplayName(jobType string) string {
  118. switch jobType {
  119. case string(models.JobTypeDebug):
  120. return "调试任务"
  121. case string(models.JobTypeBenchmark):
  122. return "评测任务"
  123. case string(models.JobTypeTrain):
  124. return "训练任务"
  125. case string(models.JobTypeInference):
  126. return "推理任务"
  127. }
  128. return ""
  129. }