Skip to content

Commit be806dd

Browse files
authored
Merge pull request #14 from Yuliia7-1/feat(connection)-add-request_timeout-and-poll_interval-for-send_async_request
feat(connection): Add per-request async_timeout and async_poll_interval for async API calls
2 parents 97b4df5 + 21aa3a0 commit be806dd

2 files changed

Lines changed: 21 additions & 6 deletions

File tree

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,13 @@ options = {
133133
cs = CloudstackClient::Client.new(config[:url], config[:api_key], config[:secret_key], options)
134134
```
135135

136+
For a single call you can override defaults on the **second** hash (client options), without changing the client instance:
137+
138+
```ruby
139+
cs.deploy_virtual_machine({ zoneid: "...", serviceofferingid: "...", templateid: "..." },
140+
async_timeout: 600, async_poll_interval: 5)
141+
```
142+
136143
### Interactive Console
137144

138145
cloudstack_client comes with an interactive console.

lib/cloudstack_client/connection.rb

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,18 @@ def send_request(params, opts = {})
9797
# The contents of the 'jobresult' element are returned upon completion of the command.
9898

9999
def send_async_request(params, opts = {})
100+
request_timeout = opts[:async_timeout] || @async_timeout
101+
poll_interval = opts[:async_poll_interval] || @async_poll_interval
102+
validate_async_timeouts!(request_timeout, poll_interval)
103+
100104
data = send_request(params, opts)
101105

102106
params = {
103107
'command' => 'queryAsyncJobResult',
104108
'jobid' => data[k('jobid')]
105109
}
106110

107-
max_tries.times do
111+
max_tries(request_timeout, poll_interval).times do
108112
data = send_request(params)
109113
print "." if @verbose
110114

@@ -116,7 +120,7 @@ def send_async_request(params, opts = {})
116120
end
117121

118122
STDOUT.flush if @verbose
119-
sleep @async_poll_interval
123+
sleep poll_interval
120124
end
121125

122126
raise TimeoutError, "Asynchronous request timed out."
@@ -128,11 +132,15 @@ def validate_input!
128132
raise InputError, "API URL not set." if @api_url == nil
129133
raise InputError, "API KEY not set." if @api_key == nil
130134
raise InputError, "API SECRET KEY not set." if @secret_key == nil
131-
raise InputError, "ASYNC POLL INTERVAL must be at least 1." if @async_poll_interval < 1.0
132-
raise InputError, "ASYNC TIMEOUT must be at least 60." if @async_timeout < 60
135+
validate_async_timeouts!(@async_timeout, @async_poll_interval)
133136
raise InputError, "REQUEST RETRIES must be at least 1." if @request_retries < 1
134137
end
135138

139+
def validate_async_timeouts!(timeout, interval)
140+
raise InputError, "ASYNC POLL INTERVAL must be at least 1." if interval < 1.0
141+
raise InputError, "ASYNC TIMEOUT must be at least 60." if timeout < 60
142+
end
143+
136144
def params_to_data(params)
137145
params_arr = params.sort.map do |key, value|
138146
case value
@@ -160,8 +168,8 @@ def create_signature(data)
160168
CGI.escape(signature)
161169
end
162170

163-
def max_tries
164-
(@async_timeout / @async_poll_interval).round
171+
def max_tries(timeout, interval)
172+
(timeout / interval).round
165173
end
166174

167175
def escape(input)

0 commit comments

Comments
 (0)