A helper function that you can use to get the result of a running async job. If the job is not finished within the configured timeout, the async job returns a AsyncTimeoutErr.
(jobid string, timeout int64)
| 485 | // A helper function that you can use to get the result of a running async job. If the job is not finished within the configured |
| 486 | // timeout, the async job returns a AsyncTimeoutErr. |
| 487 | func (cs *CloudStackClient) GetAsyncJobResult(jobid string, timeout int64) (json.RawMessage, error) { |
| 488 | var timer time.Duration |
| 489 | currentTime := time.Now().Unix() |
| 490 | |
| 491 | for { |
| 492 | p := cs.Asyncjob.NewQueryAsyncJobResultParams(jobid) |
| 493 | r, err := cs.Asyncjob.QueryAsyncJobResult(p) |
| 494 | if err != nil { |
| 495 | return nil, err |
| 496 | } |
| 497 | |
| 498 | // Status 1 means the job is finished successfully |
| 499 | if r.Jobstatus == 1 { |
| 500 | return r.Jobresult, nil |
| 501 | } |
| 502 | |
| 503 | // When the status is 2, the job has failed |
| 504 | if r.Jobstatus == 2 { |
| 505 | if r.Jobresulttype == "text" { |
| 506 | return nil, fmt.Errorf(string(r.Jobresult)) |
| 507 | } else { |
| 508 | return nil, fmt.Errorf("Undefined error: %s", string(r.Jobresult)) |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | if time.Now().Unix()-currentTime > timeout { |
| 513 | return nil, AsyncTimeoutErr |
| 514 | } |
| 515 | |
| 516 | // Add an (extremely simple) exponential backoff like feature to prevent |
| 517 | // flooding the CloudStack API |
| 518 | if timer < 15 { |
| 519 | timer++ |
| 520 | } |
| 521 | |
| 522 | time.Sleep(timer * time.Second) |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | // Execute the request against a CS API. Will return the raw JSON data returned by the API and nil if |
| 527 | // no error occurred. If the API returns an error the result will be nil and the HTTP error code and CS |
no test coverage detected