container_stop_task.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 container
  15. import (
  16. "context"
  17. "yunion.io/x/jsonutils"
  18. api "yunion.io/x/onecloud/pkg/apis/compute"
  19. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  20. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  21. "yunion.io/x/onecloud/pkg/compute/models"
  22. )
  23. type ContainerStopTask struct {
  24. ContainerBaseTask
  25. }
  26. func init() {
  27. taskman.RegisterTask(ContainerStopTask{})
  28. }
  29. func (t *ContainerStopTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  30. t.requestStop(ctx, obj.(*models.SContainer))
  31. }
  32. func (t *ContainerStopTask) requestStop(ctx context.Context, container *models.SContainer) {
  33. t.SetStage("OnStopped", nil)
  34. if err := t.GetPodDriver().RequestStopContainer(ctx, t.GetUserCred(), t); err != nil {
  35. t.OnStoppedFailed(ctx, container, jsonutils.NewString(err.Error()))
  36. return
  37. }
  38. }
  39. func (t *ContainerStopTask) OnStoppedFailed(ctx context.Context, container *models.SContainer, reason jsonutils.JSONObject) {
  40. container.SetStatus(ctx, t.GetUserCred(), api.CONTAINER_STATUS_STOP_FAILED, reason.String())
  41. t.SetStageFailed(ctx, reason)
  42. }
  43. func (t *ContainerStopTask) OnStopped(ctx context.Context, container *models.SContainer, data jsonutils.JSONObject) {
  44. t.SetStage("OnSyncStatus", nil)
  45. container.StartSyncStatusTask(ctx, t.GetUserCred(), t.GetTaskId())
  46. }
  47. func (t *ContainerStopTask) OnSyncStatus(ctx context.Context, container *models.SContainer, data jsonutils.JSONObject) {
  48. t.SetStageComplete(ctx, nil)
  49. }
  50. func (t *ContainerStopTask) OnSyncStatusFailed(ctx context.Context, container *models.SContainer, reason jsonutils.JSONObject) {
  51. t.SetStageFailed(ctx, reason)
  52. }