task.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package service
  2. import (
  3. "context"
  4. "go-common/app/admin/ep/merlin/model"
  5. "go-common/library/log"
  6. )
  7. func (s *Service) taskGetExpiredMachinesIntoTask() {
  8. machines, err := s.dao.FindExpiredMachine()
  9. if err != nil {
  10. log.Error("Task get expired machines into task (%v)", err)
  11. return
  12. }
  13. if machines != nil {
  14. log.Info("machines will be expired on tomorrow and add into task")
  15. s.dao.InsertDeleteMachinesTasks(machines)
  16. }
  17. }
  18. // 定时发邮件通知将要过期机器
  19. func (s *Service) taskSendTaskMailMachinesWillExpired() {
  20. var (
  21. machines []*model.Machine
  22. err error
  23. )
  24. if machines, err = s.dao.FindExpiredMachineByDay(s.c.Scheduler.ExpiredDate); err != nil {
  25. log.Error("Task send task mail machines will expired (%v)", err)
  26. return
  27. }
  28. for _, machine := range machines {
  29. log.Info("Machine named [%s] will be expired on next week and send a mail", machine.Name)
  30. if machine.DelayStatus == model.DelayStatusInit {
  31. s.dao.UpdateMachineDelayStatus(machine.ID, model.DelayStatusAuto)
  32. }
  33. if err = s.SendMail(model.MailTypeMachineWillExpired, machine); err != nil {
  34. log.Error("Send mail failed (%v)", err)
  35. }
  36. }
  37. }
  38. // 定时删除过期机器
  39. func (s *Service) taskDeleteExpiredMachines() {
  40. var (
  41. tasks []*model.Task
  42. instance *model.ReleaseInstance
  43. err error
  44. machine *model.Machine
  45. )
  46. if tasks, err = s.dao.FindDeleteMachineTasks(); err != nil {
  47. log.Error("Task delete expired machines error (%v)", err)
  48. return
  49. }
  50. for _, taskEle := range tasks {
  51. if instance, err = s.DelMachine(context.TODO(), taskEle.MachineID, "机器删除"); err != nil {
  52. log.Error("Task delete expired machines error (%v)", err)
  53. continue
  54. }
  55. if instance.InstanceReleaseStatus != model.SuccessDeletePaasMachines {
  56. if machine, err = s.dao.QueryMachine(taskEle.MachineID); err != nil {
  57. log.Error("Task delete expired machines error (%v)", err)
  58. continue
  59. }
  60. if err = s.SendMail(model.MailTypeTaskDeleteMachineFailed, machine); err != nil {
  61. log.Error("Send mail failed (%v)", err)
  62. }
  63. s.dao.UpdateTaskStatusByTaskID(taskEle.ID, model.TaskFailed)
  64. }
  65. }
  66. }
  67. func (s *Service) taskMachineStatus() {
  68. var (
  69. pathAndPodNames map[string][]string
  70. machineStatuses map[string]bool
  71. createdPodNames []string
  72. creatingPodNames []string
  73. creatingMachines []*model.Machine
  74. err error
  75. c = context.TODO()
  76. )
  77. if pathAndPodNames, err = s.dao.QueryPathAndPodNamesMapping(); err != nil {
  78. log.Error("Query creating machines in db err(%v)", err)
  79. return
  80. }
  81. if len(pathAndPodNames) == 0 {
  82. return
  83. }
  84. log.Info("Get pathAndPodNames(%v) from Service Tree", pathAndPodNames)
  85. if machineStatuses, err = s.TreeMachinesIsExist(c, pathAndPodNames); err != nil {
  86. log.Error("Query service tree machine status err(%v)", err)
  87. return
  88. }
  89. for k, v := range machineStatuses {
  90. if v {
  91. createdPodNames = append(createdPodNames, k)
  92. } else {
  93. creatingPodNames = append(creatingPodNames, k)
  94. }
  95. }
  96. if len(createdPodNames) > 0 {
  97. if s.dao.UpdateMachineStatusByPodNames(createdPodNames, model.BootMachineInMerlin); err != nil {
  98. log.Error("update creating machines to boot in db err(%v)", err)
  99. return
  100. }
  101. }
  102. if len(creatingPodNames) <= 0 {
  103. return
  104. }
  105. if creatingMachines, err = s.dao.QueryMachinesByPodNames(creatingPodNames); err != nil {
  106. log.Error("Query creating machines in db err(%v)", err)
  107. return
  108. }
  109. for _, m := range creatingMachines {
  110. log.Info("Create machine(%v) deadline exceeded", m)
  111. if _, err = s.verifyPassStatus(c, m, false); err != nil {
  112. log.Error("Del verify machine(%v) in db err(%v)", m, err)
  113. continue
  114. }
  115. if _, err = s.dao.DelPaasMachine(c, m.ToPaasQueryAndDelMachineRequest()); err != nil {
  116. log.Error("Del creating machine(%v) in db err(%v)", m, err)
  117. continue
  118. }
  119. }
  120. }
  121. // 同步ios移动设备状态
  122. func (s *Service) taskSyncMobileDeviceList() {
  123. s.SyncMobileDeviceList(context.Background())
  124. }
  125. // 定时清理由于回调失败,而在进行中的快照
  126. func (s *Service) taskUpdateSnapshotStatusInDoing() {
  127. var (
  128. err error
  129. snapshotRecords []*model.SnapshotRecord
  130. )
  131. if snapshotRecords, err = s.dao.FindSnapshotStatusInDoingOver2Hours(); err != nil {
  132. return
  133. }
  134. for _, snapshotRecord := range snapshotRecords {
  135. if err = s.dao.UpdateSnapshotRecordStatus(snapshotRecord.MachineID, model.SnapShotFailed); err != nil {
  136. continue
  137. }
  138. }
  139. }