yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/cloudprovider/waitstatus.go (about)

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