dbinstance_renew_task.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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/log"
  21. "yunion.io/x/pkg/errors"
  22. "yunion.io/x/pkg/util/billing"
  23. api "yunion.io/x/onecloud/pkg/apis/compute"
  24. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  25. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  26. "yunion.io/x/onecloud/pkg/compute/models"
  27. "yunion.io/x/onecloud/pkg/util/logclient"
  28. )
  29. type DBInstanceRenewTask struct {
  30. taskman.STask
  31. }
  32. func init() {
  33. taskman.RegisterTask(DBInstanceRenewTask{})
  34. }
  35. func (self *DBInstanceRenewTask) taskFailed(ctx context.Context, rds *models.SDBInstance, err error) {
  36. db.OpsLog.LogEvent(rds, db.ACT_REW_FAIL, err, self.UserCred)
  37. logclient.AddActionLogWithStartable(self, rds, logclient.ACT_RENEW, err, self.UserCred, false)
  38. rds.SetStatus(ctx, self.GetUserCred(), api.DBINSTANCE_RENEW_FAILED, err.Error())
  39. self.SetStageFailed(ctx, jsonutils.NewString(err.Error()))
  40. }
  41. func (self *DBInstanceRenewTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  42. rds := obj.(*models.SDBInstance)
  43. duration, _ := self.GetParams().GetString("duration")
  44. bc, err := billing.ParseBillingCycle(duration)
  45. if err != nil {
  46. self.taskFailed(ctx, rds, errors.Wrapf(err, "ParseBillingCycle(%s)", duration))
  47. return
  48. }
  49. iRds, err := rds.GetIDBInstance(ctx)
  50. if err != nil {
  51. self.taskFailed(ctx, rds, errors.Wrapf(err, "GetIDBInstance"))
  52. return
  53. }
  54. oldExpired := iRds.GetExpiredAt()
  55. err = iRds.Renew(bc)
  56. if err != nil {
  57. self.taskFailed(ctx, rds, errors.Wrapf(err, "iRds.Renew"))
  58. return
  59. }
  60. err = cloudprovider.WaitCreated(15*time.Second, 5*time.Minute, func() bool {
  61. err := iRds.Refresh()
  62. if err != nil {
  63. log.Errorf("failed refresh rds %s error: %v", rds.Name, err)
  64. }
  65. newExipred := iRds.GetExpiredAt()
  66. if newExipred.After(oldExpired) {
  67. return true
  68. }
  69. return false
  70. })
  71. if err != nil {
  72. self.taskFailed(ctx, rds, errors.Wrapf(err, "wait expired time refresh"))
  73. return
  74. }
  75. logclient.AddActionLogWithStartable(self, rds, logclient.ACT_RENEW, map[string]string{"duration": duration}, self.UserCred, true)
  76. self.SetStage("OnSyncstatusComplete", nil)
  77. rds.StartDBInstanceSyncTask(ctx, self.UserCred, self.GetTaskId())
  78. }
  79. func (self *DBInstanceRenewTask) OnSyncstatusComplete(ctx context.Context, rds *models.SDBInstance, data jsonutils.JSONObject) {
  80. self.SetStageComplete(ctx, nil)
  81. }
  82. func (self *DBInstanceRenewTask) OnSyncstatusCompleteFailed(ctx context.Context, rds *models.SDBInstance, reason jsonutils.JSONObject) {
  83. self.SetStageFailed(ctx, reason)
  84. }