pod_syncstatus_task.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package guest
  15. import (
  16. "context"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/pkg/errors"
  19. api "yunion.io/x/onecloud/pkg/apis/compute"
  20. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  22. "yunion.io/x/onecloud/pkg/compute/models"
  23. "yunion.io/x/onecloud/pkg/util/logclient"
  24. )
  25. type PodSyncstatusTask struct {
  26. SGuestBaseTask
  27. }
  28. func init() {
  29. taskman.RegisterTask(PodSyncstatusTask{})
  30. }
  31. func (t *PodSyncstatusTask) taskFailed(ctx context.Context, pod *models.SGuest, err string) {
  32. pod.SetStatus(ctx, t.GetUserCred(), api.POD_STATUS_UPLOADING_STATUS_FAILED, err)
  33. db.OpsLog.LogEvent(pod, db.ACT_SYNC_STATUS, err, t.GetUserCred())
  34. logclient.AddActionLogWithStartable(t, pod, logclient.ACT_SYNC_STATUS, err, t.GetUserCred(), false)
  35. t.SetStageFailed(ctx, jsonutils.NewString(err))
  36. }
  37. func (t *PodSyncstatusTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  38. pod := obj.(*models.SGuest)
  39. pod.SetStatus(ctx, t.GetUserCred(), api.POD_STATUS_UPLOADING_STATUS, "by PodSyncstatusTask")
  40. t.SetStage("OnUploadGuestStatus", nil)
  41. if err := t.requestUploadStatus(ctx, pod); err != nil {
  42. t.taskFailed(ctx, pod, err.Error())
  43. }
  44. }
  45. func (t *PodSyncstatusTask) requestUploadStatus(ctx context.Context, pod *models.SGuest) error {
  46. drv, err := pod.GetDriver()
  47. if err != nil {
  48. return errors.Wrap(err, "pod.GetDriver")
  49. }
  50. if err := drv.RequestUploadGuestStatus(ctx, pod, t); err != nil {
  51. return errors.Wrap(err, "drv.RequestUploadGuestStatus")
  52. }
  53. return nil
  54. }
  55. func (t *PodSyncstatusTask) OnUploadGuestStatus(ctx context.Context, pod *models.SGuest, data jsonutils.JSONObject) {
  56. t.SetStageComplete(ctx, nil)
  57. }
  58. func (t *PodSyncstatusTask) OnUploadGuestStatusFailed(ctx context.Context, pod *models.SGuest, reason jsonutils.JSONObject) {
  59. t.taskFailed(ctx, pod, reason.String())
  60. }
  61. /*func (t *PodSyncstatusTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  62. t.SetStage("OnWaitContainerSynced", nil)
  63. pod := obj.(*models.SGuest)
  64. pod.SetStatus(ctx, t.GetUserCred(), api.POD_STATUS_SYNCING_CONTAINER_STATUS, "")
  65. ctrs, err := models.GetContainerManager().GetContainersByPod(pod.GetId())
  66. if err != nil {
  67. t.OnWaitContainerSyncedFailed(ctx, pod, jsonutils.NewString(errors.Wrap(err, "GetContainersByPod").Error()))
  68. return
  69. }
  70. if len(ctrs) == 0 {
  71. t.OnWaitContainerSynced(ctx, pod, nil)
  72. } else {
  73. for i := range ctrs {
  74. curCtr := ctrs[i]
  75. curCtr.StartSyncStatusTask(ctx, t.GetUserCred(), t.GetTaskId())
  76. }
  77. }
  78. }
  79. func (t *PodSyncstatusTask) OnWaitContainerSynced(ctx context.Context, pod *models.SGuest, _ jsonutils.JSONObject) {
  80. isAllSynced := true
  81. ctrs, err := models.GetContainerManager().GetContainersByPod(pod.GetId())
  82. if err != nil {
  83. t.OnWaitContainerSyncedFailed(ctx, pod, jsonutils.NewString(errors.Wrap(err, "GetContainersByPod").Error()))
  84. return
  85. }
  86. for i := range ctrs {
  87. curCtr := ctrs[i]
  88. if curCtr.GetStatus() == api.CONTAINER_STATUS_SYNC_STATUS {
  89. isAllSynced = false
  90. }
  91. }
  92. if isAllSynced {
  93. t.OnContainerSynced(ctx, pod)
  94. }
  95. }
  96. func (t *PodSyncstatusTask) OnWaitContainerSyncedFailed(ctx context.Context, pod *models.SGuest, data jsonutils.JSONObject) {
  97. pod.SetStatus(ctx, t.GetUserCred(), api.POD_STATUS_SYNCING_CONTAINER_STATUS_FAILED, data.String())
  98. t.SetStageFailed(ctx, data)
  99. }
  100. func (t *PodSyncstatusTask) OnContainerSynced(ctx context.Context, pod *models.SGuest) {
  101. t.SetStage("OnPodSynced", nil)
  102. models.StartResourceSyncStatusTask(ctx, t.GetUserCred(), pod, "GuestSyncstatusTask", t.GetTaskId())
  103. }
  104. func (t *PodSyncstatusTask) OnPodSynced(ctx context.Context, pod *models.SGuest, data jsonutils.JSONObject) {
  105. t.SetStageComplete(ctx, nil)
  106. }
  107. func (t *PodSyncstatusTask) OnPodSyncedFailed(ctx context.Context, pod *models.SGuest, reason jsonutils.JSONObject) {
  108. t.SetStageFailed(ctx, reason)
  109. }*/