task.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. Copyright (c) 2015-2024 VMware, Inc. All Rights Reserved.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package object
  14. import (
  15. "context"
  16. "fmt"
  17. "github.com/vmware/govmomi/property"
  18. "github.com/vmware/govmomi/task"
  19. "github.com/vmware/govmomi/vim25"
  20. "github.com/vmware/govmomi/vim25/methods"
  21. "github.com/vmware/govmomi/vim25/progress"
  22. "github.com/vmware/govmomi/vim25/types"
  23. )
  24. // Task is a convenience wrapper around task.Task that keeps a reference to
  25. // the client that was used to create it. This allows users to call the Wait()
  26. // function with only a context parameter, instead of a context parameter, a
  27. // soap.RoundTripper, and reference to the root property collector.
  28. type Task struct {
  29. Common
  30. }
  31. func NewTask(c *vim25.Client, ref types.ManagedObjectReference) *Task {
  32. t := Task{
  33. Common: NewCommon(c, ref),
  34. }
  35. return &t
  36. }
  37. // Wait waits for a task to complete.
  38. // NOTE: This method create a thread-safe PropertyCollector instance per-call, so it is thread safe.
  39. // The downside of this approach is the additional resource usage on the vCenter side for each call.
  40. func (t *Task) Wait(ctx context.Context) error {
  41. _, err := t.WaitForResult(ctx, nil)
  42. return err
  43. }
  44. // WaitForResult wait for a task to complete.
  45. // NOTE: This method create a thread-safe PropertyCollector instance per-call, so it is thread safe.
  46. // The downside of this approach is the additional resource usage on the vCenter side for each call.
  47. func (t *Task) WaitForResult(ctx context.Context, s ...progress.Sinker) (taskInfo *types.TaskInfo, result error) {
  48. var pr progress.Sinker
  49. if len(s) == 1 {
  50. pr = s[0]
  51. }
  52. p, err := property.DefaultCollector(t.c).Create(ctx)
  53. if err != nil {
  54. return nil, err
  55. }
  56. // Attempt to destroy the collector using the background context, as the
  57. // specified context may have timed out or have been canceled.
  58. defer func() {
  59. if err := p.Destroy(context.Background()); err != nil {
  60. if result == nil {
  61. result = err
  62. } else {
  63. result = fmt.Errorf(
  64. "destroy property collector failed with %s after failing to wait for updates: %w",
  65. err,
  66. result)
  67. }
  68. }
  69. }()
  70. return task.WaitEx(ctx, t.Reference(), p, pr)
  71. }
  72. // WaitEx waits for a task to complete.
  73. // NOTE: This method use the same PropertyCollector instance in each call, thus reducing resource usage on the vCenter side.
  74. // The downside of this approach is that this method is not thread safe.
  75. func (t *Task) WaitEx(ctx context.Context) error {
  76. _, err := t.WaitForResultEx(ctx, nil)
  77. return err
  78. }
  79. // WaitForResultEx waits for a task to complete.
  80. // NOTE: This method use the same PropertyCollector instance in each call, thus reducing resource usage on the vCenter side.
  81. // The downside of this approach is that this method is not thread safe.
  82. func (t *Task) WaitForResultEx(ctx context.Context, s ...progress.Sinker) (*types.TaskInfo, error) {
  83. var pr progress.Sinker
  84. if len(s) == 1 {
  85. pr = s[0]
  86. }
  87. p := property.DefaultCollector(t.c)
  88. return task.WaitEx(ctx, t.Reference(), p, pr)
  89. }
  90. func (t *Task) Cancel(ctx context.Context) error {
  91. _, err := methods.CancelTask(ctx, t.Client(), &types.CancelTask{
  92. This: t.Reference(),
  93. })
  94. return err
  95. }
  96. // SetState sets task state and optionally sets results or fault, as appropriate for state.
  97. func (t *Task) SetState(ctx context.Context, state types.TaskInfoState, result types.AnyType, fault *types.LocalizedMethodFault) error {
  98. req := types.SetTaskState{
  99. This: t.Reference(),
  100. State: state,
  101. Result: result,
  102. Fault: fault,
  103. }
  104. _, err := methods.SetTaskState(ctx, t.Common.Client(), &req)
  105. return err
  106. }
  107. // SetDescription updates task description to describe the current phase of the task.
  108. func (t *Task) SetDescription(ctx context.Context, description types.LocalizableMessage) error {
  109. req := types.SetTaskDescription{
  110. This: t.Reference(),
  111. Description: description,
  112. }
  113. _, err := methods.SetTaskDescription(ctx, t.Common.Client(), &req)
  114. return err
  115. }
  116. // UpdateProgress Sets percentage done for this task and recalculates overall percentage done.
  117. // If a percentDone value of less than zero or greater than 100 is specified,
  118. // a value of zero or 100 respectively is used.
  119. func (t *Task) UpdateProgress(ctx context.Context, percentDone int) error {
  120. req := types.UpdateProgress{
  121. This: t.Reference(),
  122. PercentDone: int32(percentDone),
  123. }
  124. _, err := methods.UpdateProgress(ctx, t.Common.Client(), &req)
  125. return err
  126. }