wait.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. Copyright (c) 2015 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 task
  14. import (
  15. "context"
  16. "github.com/vmware/govmomi/property"
  17. "github.com/vmware/govmomi/vim25/progress"
  18. "github.com/vmware/govmomi/vim25/types"
  19. )
  20. type taskProgress struct {
  21. info *types.TaskInfo
  22. }
  23. func (t taskProgress) Percentage() float32 {
  24. return float32(t.info.Progress)
  25. }
  26. func (t taskProgress) Detail() string {
  27. return ""
  28. }
  29. func (t taskProgress) Error() error {
  30. if t.info.Error != nil {
  31. return Error{t.info.Error}
  32. }
  33. return nil
  34. }
  35. type taskCallback struct {
  36. ch chan<- progress.Report
  37. info *types.TaskInfo
  38. err error
  39. }
  40. func (t *taskCallback) fn(pc []types.PropertyChange) bool {
  41. for _, c := range pc {
  42. if c.Name != "info" {
  43. continue
  44. }
  45. if c.Op != types.PropertyChangeOpAssign {
  46. continue
  47. }
  48. if c.Val == nil {
  49. continue
  50. }
  51. ti := c.Val.(types.TaskInfo)
  52. t.info = &ti
  53. }
  54. // t.info could be nil if pc can't satify the rules above
  55. if t.info == nil {
  56. return false
  57. }
  58. pr := taskProgress{t.info}
  59. // Store copy of error, so Wait() can return it as well.
  60. t.err = pr.Error()
  61. switch t.info.State {
  62. case types.TaskInfoStateQueued, types.TaskInfoStateRunning:
  63. if t.ch != nil {
  64. // Don't care if this is dropped
  65. select {
  66. case t.ch <- pr:
  67. default:
  68. }
  69. }
  70. return false
  71. case types.TaskInfoStateSuccess, types.TaskInfoStateError:
  72. if t.ch != nil {
  73. // Last one must always be delivered
  74. t.ch <- pr
  75. }
  76. return true
  77. default:
  78. panic("unknown state: " + t.info.State)
  79. }
  80. }
  81. // Wait waits for a task to finish with either success or failure. It does so
  82. // by waiting for the "info" property of task managed object to change. The
  83. // function returns when it finds the task in the "success" or "error" state.
  84. // In the former case, the return value is nil. In the latter case the return
  85. // value is an instance of this package's Error struct.
  86. //
  87. // Any error returned while waiting for property changes causes the function to
  88. // return immediately and propagate the error.
  89. //
  90. // If the progress.Sinker argument is specified, any progress updates for the
  91. // task are sent here. The completion percentage is passed through directly.
  92. // The detail for the progress update is set to an empty string. If the task
  93. // finishes in the error state, the error instance is passed through as well.
  94. // Note that this error is the same error that is returned by this function.
  95. //
  96. func Wait(ctx context.Context, ref types.ManagedObjectReference, pc *property.Collector, s progress.Sinker) (*types.TaskInfo, error) {
  97. cb := &taskCallback{}
  98. // Include progress sink if specified
  99. if s != nil {
  100. cb.ch = s.Sink()
  101. defer close(cb.ch)
  102. }
  103. err := property.Wait(ctx, pc, ref, []string{"info"}, cb.fn)
  104. if err != nil {
  105. return nil, err
  106. }
  107. return cb.info, cb.err
  108. }