pod_stop_task.go 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. )
  24. type PodStopTask struct {
  25. SGuestBaseTask
  26. }
  27. func init() {
  28. taskman.RegisterTask(PodStopTask{})
  29. }
  30. func (t *PodStopTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  31. t.OnWaitContainerStopped(ctx, obj.(*models.SGuest), nil)
  32. }
  33. type ServerStopTaskParams struct {
  34. IsForce bool `json:"is_force"`
  35. Timeout int64 `json:"timeout"`
  36. }
  37. func (t *PodStopTask) OnWaitContainerStopped(ctx context.Context, pod *models.SGuest, _ jsonutils.JSONObject) {
  38. pod.SetStatus(ctx, t.GetUserCred(), api.POD_STATUS_STOPPING_CONTAINER, "")
  39. ctrs, err := models.GetContainerManager().GetContainersByPod(pod.GetId())
  40. if err != nil {
  41. t.OnWaitContainerStoppedFailed(ctx, pod, jsonutils.NewString(errors.Wrap(err, "GetContainersByPod").Error()))
  42. return
  43. }
  44. if len(ctrs) == 0 {
  45. t.OnContainerStopped(ctx, pod, nil)
  46. } else {
  47. t.SetStage("OnContainerStopped", nil)
  48. input := new(ServerStopTaskParams)
  49. t.GetParams().Unmarshal(input)
  50. if input.Timeout == 0 {
  51. input.Timeout = 1
  52. }
  53. if err := models.GetContainerManager().StartBatchStopTask(ctx, t.GetUserCred(), ctrs, int(input.Timeout), input.IsForce, t.GetId()); err != nil {
  54. t.OnWaitContainerStoppedFailed(ctx, pod, jsonutils.NewString(err.Error()))
  55. return
  56. }
  57. }
  58. }
  59. func (t *PodStopTask) OnWaitContainerStoppedFailed(ctx context.Context, pod *models.SGuest, data jsonutils.JSONObject) {
  60. pod.SetStatus(ctx, t.GetUserCred(), api.POD_STATUS_STOP_CONTAINER_FAILED, data.String())
  61. t.SetStageFailed(ctx, data)
  62. }
  63. func (t *PodStopTask) OnContainerStopped(ctx context.Context, pod *models.SGuest, _ jsonutils.JSONObject) {
  64. t.SetStage("OnPodStopped", nil)
  65. task, err := taskman.TaskManager.NewTask(ctx, "GuestStopTask", pod, t.GetUserCred(), nil, t.GetTaskId(), "", nil)
  66. if err != nil {
  67. t.OnPodStoppedFailed(ctx, pod, jsonutils.NewString(err.Error()))
  68. return
  69. }
  70. task.ScheduleRun(nil)
  71. }
  72. func (t *PodStopTask) OnContainerStoppedFailed(ctx context.Context, pod *models.SGuest, data jsonutils.JSONObject) {
  73. pod.SetStatus(ctx, t.GetUserCred(), api.POD_STATUS_STOP_CONTAINER_FAILED, data.String())
  74. t.SetStageFailed(ctx, data)
  75. }
  76. func (t *PodStopTask) OnPodStopped(ctx context.Context, pod *models.SGuest, data jsonutils.JSONObject) {
  77. pod.SetStatus(ctx, t.GetUserCred(), api.VM_READY, "")
  78. t.SetStageComplete(ctx, nil)
  79. }
  80. func (t *PodStopTask) OnPodStoppedFailed(ctx context.Context, pod *models.SGuest, reason jsonutils.JSONObject) {
  81. pod.SetStatus(ctx, t.GetUserCred(), api.VM_STOP_FAILED, reason.String())
  82. t.SetStageFailed(ctx, reason)
  83. }