dbinstance_reboot_task.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 dbinstance
  15. import (
  16. "context"
  17. "time"
  18. "yunion.io/x/cloudmux/pkg/cloudprovider"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/pkg/errors"
  21. api "yunion.io/x/onecloud/pkg/apis/compute"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  23. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  24. "yunion.io/x/onecloud/pkg/compute/models"
  25. "yunion.io/x/onecloud/pkg/util/logclient"
  26. )
  27. type DBInstanceRebootTask struct {
  28. taskman.STask
  29. }
  30. func init() {
  31. taskman.RegisterTask(DBInstanceRebootTask{})
  32. }
  33. func (self *DBInstanceRebootTask) taskFailed(ctx context.Context, dbinstance *models.SDBInstance, err error) {
  34. dbinstance.SetStatus(ctx, self.UserCred, api.DBINSTANCE_REBOOT_FAILED, err.Error())
  35. db.OpsLog.LogEvent(dbinstance, db.ACT_REBOOT, err, self.GetUserCred())
  36. logclient.AddActionLogWithStartable(self, dbinstance, logclient.ACT_REBOOT, err, self.UserCred, false)
  37. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  38. }
  39. func (self *DBInstanceRebootTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  40. dbinstance := obj.(*models.SDBInstance)
  41. self.RebootDBInstance(ctx, dbinstance)
  42. }
  43. func (self *DBInstanceRebootTask) RebootDBInstance(ctx context.Context, dbinstance *models.SDBInstance) {
  44. idbinstance, err := dbinstance.GetIDBInstance(ctx)
  45. if err != nil {
  46. self.taskFailed(ctx, dbinstance, errors.Wrap(err, "dbinstance.GetIDBInstance"))
  47. return
  48. }
  49. err = idbinstance.Reboot()
  50. if err != nil {
  51. self.taskFailed(ctx, dbinstance, errors.Wrap(err, "idbinstance.Reboot"))
  52. return
  53. }
  54. err = cloudprovider.WaitStatus(idbinstance, api.DBINSTANCE_RUNNING, 10*time.Second, time.Minute*30)
  55. if err != nil {
  56. self.taskFailed(ctx, dbinstance, errors.Wrap(err, "cloudprovider.WaitStatus"))
  57. return
  58. }
  59. self.taskComplete(ctx, dbinstance)
  60. }
  61. func (self *DBInstanceRebootTask) taskComplete(ctx context.Context, dbinstance *models.SDBInstance) {
  62. dbinstance.SetStatus(ctx, self.UserCred, api.DBINSTANCE_RUNNING, "")
  63. logclient.AddActionLogWithStartable(self, dbinstance, logclient.ACT_REBOOT, nil, self.UserCred, true)
  64. self.SetStageComplete(ctx, nil)
  65. }