1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

[dynect|dns] Automatically poll jobs if we get them. Closes #575

This commit is contained in:
Dan Peterson 2011-11-04 15:00:22 -03:00
parent a5c5f7c332
commit 7ba6df8f14

View file

@ -26,6 +26,8 @@ module Fog
request :post_zone
request :put_zone
class JobIncomplete < Error; end
class Mock
def initialize(options={})
@dynect_customer = options[:dynect_customer]
@ -80,18 +82,23 @@ module Fog
def request(params)
begin
# any request could redirect to a job
params[:expects] = Array(params[:expects]) | [307]
params[:headers] ||= {}
params[:headers]['Content-Type'] = 'application/json'
params[:headers]['API-Version'] = @version
params[:headers]['Auth-Token'] = auth_token unless params[:path] == "Session"
params[:path] = "#{@path}/#{params[:path]}"
params[:path] = "#{@path}/#{params[:path]}" unless params[:path] =~ %r{^#{Regexp.escape(@path)}/}
response = @connection.request(params.merge!({:host => @host}))
unless response.body.empty?
if response.status == 307
response = poll_job(response)
elsif !response.body.empty?
response.body = MultiJson.decode(response.body)
end
response
response
rescue Excon::Errors::HTTPStatusError => error
if @auth_token && error.message =~ /login: (Bad or expired credentials|inactivity logout)/
@auth_token = nil
@ -103,6 +110,21 @@ module Fog
response
end
def poll_job(response, time_to_wait = 10)
job_location = response.headers['Location']
Fog.wait_for(time_to_wait) do
response = request(:expects => 200, :method => :get, :path => job_location)
response.body['status'] != 'incomplete'
end
if response.body['status'] == 'incomplete'
raise JobIncomplete.new("Job #{response.body['job_id']} is still incomplete")
end
response
end
end
end