github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/taurusdb/v3/instances/results_job.go (about)

     1  package instances
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/huaweicloud/golangsdk"
     8  )
     9  
    10  type JobResponse struct {
    11  	JobID   string `json:"job_id"`
    12  	OrderID string `json:"order_id"`
    13  }
    14  
    15  type JobStatus struct {
    16  	Job Job `json:"job"`
    17  }
    18  
    19  type Job struct {
    20  	Status     string `json:"status"`
    21  	JobID      string `json:"id"`
    22  	FailReason string `json:"fail_reason"`
    23  }
    24  
    25  type JobResult struct {
    26  	golangsdk.Result
    27  }
    28  
    29  func (r JobResult) ExtractJobResponse() (*JobResponse, error) {
    30  	job := new(JobResponse)
    31  	err := r.ExtractInto(job)
    32  	return job, err
    33  }
    34  
    35  func (r JobResult) ExtractJobStatus() (*JobStatus, error) {
    36  	job := new(JobStatus)
    37  	err := r.ExtractInto(job)
    38  	return job, err
    39  }
    40  
    41  func WaitForJobSuccess(client *golangsdk.ServiceClient, secs int, jobID string) error {
    42  	return golangsdk.WaitFor(secs, func() (bool, error) {
    43  		job_status := new(JobStatus)
    44  		_, err := client.Get(jobURL(client, jobID), &job_status, &golangsdk.RequestOpts{
    45  			MoreHeaders: requestOpts.MoreHeaders,
    46  		})
    47  		time.Sleep(15 * time.Second)
    48  		if err != nil {
    49  			return false, err
    50  		}
    51  		job := job_status.Job
    52  
    53  		if job.Status == "Completed" {
    54  			return true, nil
    55  		}
    56  		if job.Status == "Failed" {
    57  			err = fmt.Errorf("Job failed: %s.\n", job.FailReason)
    58  			return false, err
    59  		}
    60  
    61  		return false, nil
    62  	})
    63  }