Skip to content

Commit

Permalink
Merge pull request #43 from svanharmelen/dev
Browse files Browse the repository at this point in the history
Tweak the async logic
  • Loading branch information
Sander van Harmelen committed Dec 3, 2015
2 parents 104168f + 60afae5 commit eaf4e42
Show file tree
Hide file tree
Showing 135 changed files with 2,846 additions and 4,107 deletions.
50 changes: 20 additions & 30 deletions cloudstack/AccountService.go
Expand Up @@ -333,15 +333,13 @@ func (s *AccountService) DeleteAccount(p *DeleteAccountParams) (*DeleteAccountRe

// If we have a async client, we need to wait for the async result
if s.cs.async {
b, warn, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
b, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
if err != nil {
if err == AsyncTimeoutErr {
return &r, err
}
return nil, err
}
// If 'warn' has a value it means the job is running longer than the configured
// timeout, the resonse will contain the jobid of the running async job
if warn != nil {
return &r, warn
}

if err := json.Unmarshal(b, &r); err != nil {
return nil, err
Expand Down Expand Up @@ -616,15 +614,13 @@ func (s *AccountService) DisableAccount(p *DisableAccountParams) (*DisableAccoun

// If we have a async client, we need to wait for the async result
if s.cs.async {
b, warn, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
b, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
if err != nil {
if err == AsyncTimeoutErr {
return &r, err
}
return nil, err
}
// If 'warn' has a value it means the job is running longer than the configured
// timeout, the resonse will contain the jobid of the running async job
if warn != nil {
return &r, warn
}

b, err = getRawValue(b)
if err != nil {
Expand Down Expand Up @@ -1367,15 +1363,13 @@ func (s *AccountService) MarkDefaultZoneForAccount(p *MarkDefaultZoneForAccountP

// If we have a async client, we need to wait for the async result
if s.cs.async {
b, warn, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
b, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
if err != nil {
if err == AsyncTimeoutErr {
return &r, err
}
return nil, err
}
// If 'warn' has a value it means the job is running longer than the configured
// timeout, the resonse will contain the jobid of the running async job
if warn != nil {
return &r, warn
}

b, err = getRawValue(b)
if err != nil {
Expand Down Expand Up @@ -1532,15 +1526,13 @@ func (s *AccountService) AddAccountToProject(p *AddAccountToProjectParams) (*Add

// If we have a async client, we need to wait for the async result
if s.cs.async {
b, warn, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
b, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
if err != nil {
if err == AsyncTimeoutErr {
return &r, err
}
return nil, err
}
// If 'warn' has a value it means the job is running longer than the configured
// timeout, the resonse will contain the jobid of the running async job
if warn != nil {
return &r, warn
}

if err := json.Unmarshal(b, &r); err != nil {
return nil, err
Expand Down Expand Up @@ -1613,15 +1605,13 @@ func (s *AccountService) DeleteAccountFromProject(p *DeleteAccountFromProjectPar

// If we have a async client, we need to wait for the async result
if s.cs.async {
b, warn, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
b, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
if err != nil {
if err == AsyncTimeoutErr {
return &r, err
}
return nil, err
}
// If 'warn' has a value it means the job is running longer than the configured
// timeout, the resonse will contain the jobid of the running async job
if warn != nil {
return &r, warn
}

if err := json.Unmarshal(b, &r); err != nil {
return nil, err
Expand Down
30 changes: 12 additions & 18 deletions cloudstack/AddressService.go
Expand Up @@ -160,15 +160,13 @@ func (s *AddressService) AssociateIpAddress(p *AssociateIpAddressParams) (*Assoc

// If we have a async client, we need to wait for the async result
if s.cs.async {
b, warn, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
b, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
if err != nil {
if err == AsyncTimeoutErr {
return &r, err
}
return nil, err
}
// If 'warn' has a value it means the job is running longer than the configured
// timeout, the resonse will contain the jobid of the running async job
if warn != nil {
return &r, warn
}

b, err = getRawValue(b)
if err != nil {
Expand Down Expand Up @@ -273,15 +271,13 @@ func (s *AddressService) DisassociateIpAddress(p *DisassociateIpAddressParams) (

// If we have a async client, we need to wait for the async result
if s.cs.async {
b, warn, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
b, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
if err != nil {
if err == AsyncTimeoutErr {
return &r, err
}
return nil, err
}
// If 'warn' has a value it means the job is running longer than the configured
// timeout, the resonse will contain the jobid of the running async job
if warn != nil {
return &r, warn
}

if err := json.Unmarshal(b, &r); err != nil {
return nil, err
Expand Down Expand Up @@ -747,15 +743,13 @@ func (s *AddressService) UpdateIpAddress(p *UpdateIpAddressParams) (*UpdateIpAdd

// If we have a async client, we need to wait for the async result
if s.cs.async {
b, warn, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
b, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
if err != nil {
if err == AsyncTimeoutErr {
return &r, err
}
return nil, err
}
// If 'warn' has a value it means the job is running longer than the configured
// timeout, the resonse will contain the jobid of the running async job
if warn != nil {
return &r, warn
}

b, err = getRawValue(b)
if err != nil {
Expand Down
30 changes: 12 additions & 18 deletions cloudstack/AffinityGroupService.go
Expand Up @@ -115,15 +115,13 @@ func (s *AffinityGroupService) CreateAffinityGroup(p *CreateAffinityGroupParams)

// If we have a async client, we need to wait for the async result
if s.cs.async {
b, warn, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
b, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
if err != nil {
if err == AsyncTimeoutErr {
return &r, err
}
return nil, err
}
// If 'warn' has a value it means the job is running longer than the configured
// timeout, the resonse will contain the jobid of the running async job
if warn != nil {
return &r, warn
}

b, err = getRawValue(b)
if err != nil {
Expand Down Expand Up @@ -227,15 +225,13 @@ func (s *AffinityGroupService) DeleteAffinityGroup(p *DeleteAffinityGroupParams)

// If we have a async client, we need to wait for the async result
if s.cs.async {
b, warn, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
b, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
if err != nil {
if err == AsyncTimeoutErr {
return &r, err
}
return nil, err
}
// If 'warn' has a value it means the job is running longer than the configured
// timeout, the resonse will contain the jobid of the running async job
if warn != nil {
return &r, warn
}

if err := json.Unmarshal(b, &r); err != nil {
return nil, err
Expand Down Expand Up @@ -566,15 +562,13 @@ func (s *AffinityGroupService) UpdateVMAffinityGroup(p *UpdateVMAffinityGroupPar

// If we have a async client, we need to wait for the async result
if s.cs.async {
b, warn, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
b, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
if err != nil {
if err == AsyncTimeoutErr {
return &r, err
}
return nil, err
}
// If 'warn' has a value it means the job is running longer than the configured
// timeout, the resonse will contain the jobid of the running async job
if warn != nil {
return &r, warn
}

b, err = getRawValue(b)
if err != nil {
Expand Down
10 changes: 4 additions & 6 deletions cloudstack/AlertService.go
Expand Up @@ -471,15 +471,13 @@ func (s *AlertService) GenerateAlert(p *GenerateAlertParams) (*GenerateAlertResp

// If we have a async client, we need to wait for the async result
if s.cs.async {
b, warn, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
b, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
if err != nil {
if err == AsyncTimeoutErr {
return &r, err
}
return nil, err
}
// If 'warn' has a value it means the job is running longer than the configured
// timeout, the resonse will contain the jobid of the running async job
if warn != nil {
return &r, warn
}

if err := json.Unmarshal(b, &r); err != nil {
return nil, err
Expand Down
6 changes: 4 additions & 2 deletions cloudstack/AsyncjobService.go
Expand Up @@ -20,6 +20,7 @@ import (
"encoding/json"
"net/url"
"strconv"
"time"
)

type QueryAsyncJobResultParams struct {
Expand Down Expand Up @@ -62,9 +63,10 @@ func (s *AsyncjobService) QueryAsyncJobResult(p *QueryAsyncJobResultParams) (*Qu
// We should be able to retry on failure as this call is idempotent
for i := 0; i < 3; i++ {
resp, err = s.cs.newRequest("queryAsyncJobResult", p.toURLValues())
if err != nil {
continue
if err == nil {
break
}
time.Sleep(500 * time.Millisecond)
}
if err != nil {
return nil, err
Expand Down

0 comments on commit eaf4e42

Please sign in to comment.