waitstatus.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 cloudprovider
  15. import (
  16. "time"
  17. "yunion.io/x/log"
  18. "yunion.io/x/pkg/errors"
  19. )
  20. func WaitStatusWithSync(res ICloudResource, expect string, sync func(status string), interval time.Duration, timeout time.Duration) error {
  21. startTime, status := time.Now(), res.GetStatus()
  22. for time.Since(startTime) < timeout {
  23. err := res.Refresh()
  24. if err != nil {
  25. return errors.Wrapf(err, "Refresh")
  26. }
  27. status = res.GetStatus()
  28. log.Infof("%s status %s expect %s", res.GetName(), status, expect)
  29. if sync != nil {
  30. sync(status)
  31. }
  32. if res.GetStatus() == expect {
  33. return nil
  34. }
  35. time.Sleep(interval)
  36. }
  37. return errors.Wrapf(errors.ErrTimeout, "status %s expect %s", status, expect)
  38. }
  39. func WaitStatus(res ICloudResource, expect string, interval time.Duration, timeout time.Duration) error {
  40. return WaitStatusWithSync(res, expect, nil, interval, timeout)
  41. }
  42. func WaitMultiStatusWithSync(res ICloudResource, expects []string, sync func(string), interval time.Duration, timeout time.Duration) error {
  43. startTime, status := time.Now(), res.GetStatus()
  44. for time.Since(startTime) < timeout {
  45. err := res.Refresh()
  46. if err != nil {
  47. return errors.Wrapf(err, "Refresh")
  48. }
  49. status = res.GetStatus()
  50. log.Infof("%s status %s expect %v", res.GetName(), status, expects)
  51. if sync != nil {
  52. sync(status)
  53. }
  54. for _, expect := range expects {
  55. if status == expect {
  56. return nil
  57. }
  58. }
  59. time.Sleep(interval)
  60. }
  61. return errors.Wrapf(errors.ErrTimeout, "status %s expect %v", status, expects)
  62. }
  63. func WaitMultiStatus(res ICloudResource, expects []string, interval time.Duration, timeout time.Duration) error {
  64. return WaitMultiStatusWithSync(res, expects, nil, interval, timeout)
  65. }
  66. func WaitStatusWithDelay(res ICloudResource, expect string, delay time.Duration, interval time.Duration, timeout time.Duration) error {
  67. time.Sleep(delay)
  68. return WaitStatus(res, expect, interval, timeout)
  69. }
  70. func WaitStatusWithInstanceErrorCheck(res ICloudResource, expect string, interval time.Duration, timeout time.Duration, errCheck func() error) error {
  71. startTime, status := time.Now(), res.GetStatus()
  72. for time.Since(startTime) < timeout {
  73. err := res.Refresh()
  74. if err != nil {
  75. return errors.Wrapf(err, "Refresh")
  76. }
  77. status = res.GetStatus()
  78. log.Infof("%s status %s expect %s", res.GetName(), status, expect)
  79. if status == expect {
  80. return nil
  81. }
  82. err = errCheck()
  83. if err != nil {
  84. return err
  85. }
  86. time.Sleep(interval)
  87. }
  88. return errors.Wrapf(errors.ErrTimeout, "status %s expect %s", status, expect)
  89. }
  90. func WaitDeletedWithDelay(res ICloudResource, delay time.Duration, interval time.Duration, timeout time.Duration) error {
  91. time.Sleep(delay)
  92. return WaitDeleted(res, interval, timeout)
  93. }
  94. func WaitDeleted(res ICloudResource, interval time.Duration, timeout time.Duration) error {
  95. startTime := time.Now()
  96. for time.Since(startTime) < timeout {
  97. err := res.Refresh()
  98. if err != nil {
  99. if errors.Cause(err) == ErrNotFound {
  100. return nil
  101. } else {
  102. return errors.Wrapf(err, "Refresh")
  103. }
  104. }
  105. time.Sleep(interval)
  106. }
  107. return ErrTimeout
  108. }
  109. func Wait(interval time.Duration, timeout time.Duration, callback func() (bool, error)) error {
  110. startTime := time.Now()
  111. for time.Since(startTime) < timeout {
  112. ok, err := callback()
  113. if err != nil {
  114. return err
  115. }
  116. if ok {
  117. return nil
  118. }
  119. time.Sleep(interval)
  120. }
  121. return ErrTimeout
  122. }
  123. func WaitCreated(interval time.Duration, timeout time.Duration, callback func() bool) error {
  124. startTime := time.Now()
  125. for time.Since(startTime) < timeout {
  126. ok := callback()
  127. if ok {
  128. return nil
  129. }
  130. time.Sleep(interval)
  131. }
  132. return ErrTimeout
  133. }