job.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "net/http"
  6. "go-common/app/admin/ep/melloi/model"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. )
  10. //AddJob add perf job
  11. func (s *Service) AddJob(c context.Context, job model.Job) (firstRetMap *model.PaasJobResponse, err error) {
  12. var (
  13. token string
  14. clusterMap *model.PaasJobResponse
  15. JSON []byte
  16. )
  17. if JSON, err = json.Marshal(job); err != nil {
  18. log.Error("json.Marshal err: (%v)", err)
  19. return
  20. }
  21. log.Info("JOBINFO: (%s)", string(JSON))
  22. if token, err = s.RmToken(c); err != nil {
  23. log.Error("token err :(%v)", err)
  24. return
  25. }
  26. // 调度逻辑:先从ptest_job 表里面查看
  27. log.Info("创建job的 token:(%s) ", token)
  28. if clusterMap, err = s.dao.AddJob(c, token, &job); err != nil {
  29. log.Error("s.dao.AddJob err :(%v)", err)
  30. return
  31. }
  32. if clusterMap == nil {
  33. return
  34. }
  35. firstRetMap = clusterMap
  36. return
  37. }
  38. //DeleteJob force stop perf job
  39. func (s *Service) DeleteJob(c context.Context, name string) (firstRetMap *model.PaasJobResponse, err error) {
  40. var token string
  41. if token, err = s.RmToken(c); err != nil {
  42. return
  43. }
  44. log.Info("开始执行删除容器的 dao 层******* ")
  45. if firstRetMap, err = s.dao.DeleteJob(c, token, name); err != nil {
  46. err = ecode.MelloiPaasRequestErr
  47. return
  48. }
  49. return
  50. }
  51. // CleanJob clean None running job
  52. func (s *Service) CleanJob(c context.Context) (err error) {
  53. var dockers = []*model.CleanableDocker{}
  54. if dockers, err = s.dao.QueryCleanableDocker(c); err != nil {
  55. log.Error("s.job query docker error(%v)", err)
  56. return
  57. }
  58. for _, docker := range dockers {
  59. if _, err := s.DeleteJob(c, docker.Name); err != nil {
  60. continue
  61. }
  62. }
  63. return
  64. }
  65. // QueryCleanableDocker query clearable Docker
  66. func (s *Service) QueryCleanableDocker(c context.Context) (dockers []*model.CleanableDocker, err error) {
  67. return s.dao.QueryCleanableDocker(c)
  68. }
  69. //DeleteJobBatch force stop perf job batch
  70. func (s *Service) DeleteJobBatch(c context.Context, JobBatch model.JobBatch) (status string) {
  71. for _, reportSuID := range JobBatch.ReportSuIDs {
  72. go s.QueryOrStopAllPtestByJobName(context.TODO(), reportSuID, true, 3)
  73. }
  74. status = "success"
  75. return
  76. }
  77. //QueryJobCPU query job cpu
  78. func (s *Service) QueryJobCPU(c context.Context, jobNamed string) (responseBody *model.PaasQueryJobCPUResult, err error) {
  79. var token string
  80. if token, err = s.RmToken(c); err != nil {
  81. return
  82. }
  83. log.Info("执行查询容器实时cpu 接口 ")
  84. if responseBody, err = s.dao.QueryJobCPU(c, token, jobNamed); err != nil {
  85. log.Error("query job cpu err (%v)", err)
  86. err = ecode.MelloiPaasRequestErr
  87. return
  88. }
  89. return
  90. }
  91. //QueryJobCPUByEP query job cpu
  92. func (s *Service) QueryJobCPUByEP(c context.Context, id, hostIP string) (dockerStats *model.DockerStats, err error) {
  93. if dockerStats, err = s.dao.QueryJobCPUByEP(c, id, hostIP); err != nil {
  94. log.Error("query QueryJobCpuByEP err (%v)", err)
  95. return
  96. }
  97. return
  98. }
  99. //Job get job info
  100. func (s *Service) Job(c context.Context, name string) (firstRetMap *model.PaasJobQueryStatus, err error) {
  101. var token string
  102. if token, err = s.RmToken(c); err != nil {
  103. err = ecode.MelloiPaasRequestErr
  104. return
  105. }
  106. if firstRetMap, err = s.dao.Job(c, token, name); err != nil {
  107. err = ecode.MelloiPaasRequestErr
  108. return
  109. }
  110. return
  111. }
  112. //ForceDeleteJob Force Delete Job
  113. func (s *Service) ForceDeleteJob(c context.Context, reportSuId int) (jobList *model.JobInfoList, err error) {
  114. log.Info("开始执行强制删除容器******* ")
  115. if jobList, err = s.dao.ForceDeleteJob(reportSuId); err != nil {
  116. return
  117. }
  118. for _, job := range jobList.JobList {
  119. url := "http://" + job.HostIp + ":8999" + "/ep/docker/remove?job_name=" + job.JobName
  120. if _, delErr := http.Get(url); delErr != nil {
  121. //log.Error("强制删除失败,请重试!")
  122. s.QueryOrStopAllPtestByJobName(c, reportSuId, true, 3)
  123. return
  124. }
  125. }
  126. if upErr := s.dao.UpdateJobStatus(reportSuId); upErr != nil {
  127. return
  128. }
  129. return
  130. }