disk_base_task.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 disk
  15. import (
  16. "context"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/onecloud/pkg/cloudcommon/db/quotas"
  19. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  20. "yunion.io/x/onecloud/pkg/compute/models"
  21. )
  22. type SDiskBaseTask struct {
  23. taskman.STask
  24. }
  25. func (self *SDiskBaseTask) getDisk() *models.SDisk {
  26. obj := self.GetObject()
  27. return obj.(*models.SDisk)
  28. }
  29. func (self *SDiskBaseTask) SetStageFailed(ctx context.Context, reason jsonutils.JSONObject) {
  30. self.finalReleasePendingUsage(ctx)
  31. self.STask.SetStageFailed(ctx, reason)
  32. }
  33. func (self *SDiskBaseTask) finalReleasePendingUsage(ctx context.Context) {
  34. pendingUsage := models.SQuota{}
  35. err := self.GetPendingUsage(&pendingUsage, 0)
  36. if err == nil && !pendingUsage.IsEmpty() {
  37. quotas.CancelPendingUsage(ctx, self.UserCred, &pendingUsage, &pendingUsage, false)
  38. }
  39. }
  40. func (self *SDiskBaseTask) CleanHostSchedCache(disk *models.SDisk) {
  41. storage, _ := disk.GetStorage()
  42. if storage == nil {
  43. return
  44. }
  45. storage.ClearSchedDescCache()
  46. if len(disk.BackupStorageId) > 0 {
  47. bkStorage := models.StorageManager.FetchStorageById(disk.BackupStorageId)
  48. if bkStorage != nil {
  49. bkStorage.ClearSchedDescCache()
  50. }
  51. }
  52. }