task_view.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. Copyright (c) 2017 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 view
  14. import (
  15. "context"
  16. "github.com/vmware/govmomi/property"
  17. "github.com/vmware/govmomi/vim25/types"
  18. )
  19. // TaskView extends ListView such that it can follow a ManagedEntity's recentTask updates.
  20. type TaskView struct {
  21. *ListView
  22. Follow bool
  23. Watch *types.ManagedObjectReference
  24. }
  25. // CreateTaskView creates a new ListView that optionally watches for a ManagedEntity's recentTask updates.
  26. func (m Manager) CreateTaskView(ctx context.Context, watch *types.ManagedObjectReference) (*TaskView, error) {
  27. l, err := m.CreateListView(ctx, nil)
  28. if err != nil {
  29. return nil, err
  30. }
  31. tv := &TaskView{
  32. ListView: l,
  33. Watch: watch,
  34. }
  35. return tv, nil
  36. }
  37. // Collect calls function f for each Task update.
  38. func (v TaskView) Collect(ctx context.Context, f func([]types.TaskInfo)) error {
  39. // Using TaskHistoryCollector would be less clunky, but it isn't supported on ESX at all.
  40. ref := v.Reference()
  41. filter := new(property.WaitFilter).Add(ref, "Task", []string{"info"}, v.TraversalSpec())
  42. if v.Watch != nil {
  43. filter.Add(*v.Watch, v.Watch.Type, []string{"recentTask"})
  44. }
  45. pc := property.DefaultCollector(v.Client())
  46. completed := make(map[string]bool)
  47. return property.WaitForUpdates(ctx, pc, filter, func(updates []types.ObjectUpdate) bool {
  48. var infos []types.TaskInfo
  49. var prune []types.ManagedObjectReference
  50. var tasks []types.ManagedObjectReference
  51. var reset func()
  52. for _, update := range updates {
  53. for _, change := range update.ChangeSet {
  54. if change.Name == "recentTask" {
  55. tasks = change.Val.(types.ArrayOfManagedObjectReference).ManagedObjectReference
  56. if len(tasks) != 0 {
  57. reset = func() {
  58. _, _ = v.Reset(ctx, tasks)
  59. // Remember any tasks we've reported as complete already,
  60. // to avoid reporting multiple times when Reset is triggered.
  61. rtasks := make(map[string]bool)
  62. for i := range tasks {
  63. if _, ok := completed[tasks[i].Value]; ok {
  64. rtasks[tasks[i].Value] = true
  65. }
  66. }
  67. completed = rtasks
  68. }
  69. }
  70. continue
  71. }
  72. info, ok := change.Val.(types.TaskInfo)
  73. if !ok {
  74. continue
  75. }
  76. if !completed[info.Task.Value] {
  77. infos = append(infos, info)
  78. }
  79. if v.Follow && info.CompleteTime != nil {
  80. prune = append(prune, info.Task)
  81. completed[info.Task.Value] = true
  82. }
  83. }
  84. }
  85. if len(infos) != 0 {
  86. f(infos)
  87. }
  88. if reset != nil {
  89. reset()
  90. } else if len(prune) != 0 {
  91. _, _ = v.Remove(ctx, prune)
  92. }
  93. if len(tasks) != 0 && len(infos) == 0 {
  94. return false
  95. }
  96. return !v.Follow
  97. })
  98. }