guest_resume_task.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. 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 GuestResumeTask struct {
  24. SGuestBaseTask
  25. }
  26. func init() {
  27. taskman.RegisterTask(GuestResumeTask{})
  28. }
  29. func (self *GuestResumeTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  30. guest := obj.(*models.SGuest)
  31. db.OpsLog.LogEvent(guest, db.ACT_RESUMING, "", self.UserCred)
  32. self.SetStage("OnResumeComplete", nil)
  33. drv, err := guest.GetDriver()
  34. if err != nil {
  35. self.OnResumeGuestFail(guest, err.Error())
  36. return
  37. }
  38. err = drv.RequestResumeOnHost(ctx, guest, self)
  39. if err != nil {
  40. self.OnResumeGuestFail(guest, err.Error())
  41. }
  42. }
  43. func (self *GuestResumeTask) OnResumeComplete(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  44. guest := obj.(*models.SGuest)
  45. guest.SetStatus(ctx, self.UserCred, api.VM_RUNNING, "")
  46. db.OpsLog.LogEvent(guest, db.ACT_RESUME, "", self.UserCred)
  47. self.SetStageComplete(ctx, nil)
  48. }
  49. func (self *GuestResumeTask) OnResumeCompleteFailed(ctx context.Context, obj db.IStandaloneModel,
  50. err jsonutils.JSONObject) {
  51. guest := obj.(*models.SGuest)
  52. guest.SetStatus(ctx, self.UserCred, api.VM_SUSPEND, "")
  53. db.OpsLog.LogEvent(guest, db.ACT_RESUME_FAIL, err.String(), self.UserCred)
  54. self.SetStageFailed(ctx, err)
  55. }
  56. func (self *GuestResumeTask) OnResumeGuestFail(guest *models.SGuest, reason string) {
  57. guest.SetStatus(context.Background(), self.UserCred, api.VM_RESUME_FAILED, reason)
  58. }