pending_usage.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 utils
  15. import (
  16. "context"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/log"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/onecloud/pkg/cloudcommon/db/quotas"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  22. "yunion.io/x/onecloud/pkg/compute/models"
  23. )
  24. func ClearTaskPendingUsage(ctx context.Context, task taskman.ITask) error {
  25. index := 0
  26. pendingUsage := models.SQuota{}
  27. err := task.GetPendingUsage(&pendingUsage, index)
  28. if err != nil {
  29. log.Errorf("GetPendingUsage fail %s", err)
  30. // ignore error
  31. // return errors.Wrap(err, "task.GetPendingUsage")
  32. return nil
  33. }
  34. log.Debugf("ClearTaskPendingUsage %s", jsonutils.Marshal(pendingUsage))
  35. if pendingUsage.IsEmpty() {
  36. return nil
  37. }
  38. err = quotas.CancelPendingUsage(ctx, task.GetUserCred(), &pendingUsage, &pendingUsage, false)
  39. if err != nil {
  40. return errors.Wrap(err, "models.QuotaManager.CancelPendingUsage")
  41. }
  42. if pendingUsage.IsEmpty() {
  43. err = task.ClearPendingUsage(index)
  44. if err != nil {
  45. return errors.Wrap(err, "task.ClearPendingUsage")
  46. }
  47. } else {
  48. log.Warningf("pendingUsage %s is not empty after cancel????", jsonutils.Marshal(pendingUsage).String())
  49. }
  50. return nil
  51. }
  52. func ClearTaskPendingRegionUsage(ctx context.Context, task taskman.ITask) error {
  53. index := 1
  54. pendingUsage := models.SRegionQuota{}
  55. err := task.GetPendingUsage(&pendingUsage, index)
  56. if err != nil {
  57. log.Errorf("GetPendingUsage fail %s", err)
  58. // ignore error
  59. // return errors.Wrap(err, "task.GetPendingUsage")
  60. return nil
  61. }
  62. if pendingUsage.IsEmpty() {
  63. return nil
  64. }
  65. err = quotas.CancelPendingUsage(ctx, task.GetUserCred(), &pendingUsage, &pendingUsage, false)
  66. if err != nil {
  67. return errors.Wrap(err, "models.QuotaManager.CancelPendingUsage")
  68. }
  69. if pendingUsage.IsEmpty() {
  70. err = task.ClearPendingUsage(index)
  71. if err != nil {
  72. return errors.Wrap(err, "task.ClearPendingUsage")
  73. }
  74. } else {
  75. log.Warningf("pendingUsage %s is not empty after cancel????", jsonutils.Marshal(pendingUsage).String())
  76. }
  77. return nil
  78. }