Added retrying

This commit is contained in:
Senad Uka
2018-08-13 15:01:28 +02:00
parent 92cadb76ed
commit 5684e58826
7 changed files with 77 additions and 14 deletions

View File

@@ -1,15 +1,32 @@
require 'json'
require 'sinatra/base'
require 'sinatra/custom_logger'
require_relative '../http/query'
require_relative '../exceptions'
module Pruning
module API
class App < Sinatra::Base
before { content_type :json }
after { serialise_response }
set :show_exceptions, true
error Pruning::Exceptions::UnexpectedError do
status 500
end
error Pruning::Exceptions::OriginCannotFindTheResource do
status 404
end
get '/' do
"Try /tree/:name"
end
private
def serialise_response
return unless content_type == 'application/json'
response.body = [JSON(response.body)]

View File

@@ -1,16 +1,18 @@
require 'rest-client'
require 'retries'
require_relative 'app'
require_relative '../repos/tree'
require_relative '../pruner'
require_relative '../exceptions'
module Pruning
module API
class Tree < App
get '/tree/:name' do
tree_repo = Pruning::Repos::Tree.new(RestClient, ENV['TREE_SOURCE_API_HOSTNAME'])
complete_tree = tree_repo.get(query.name)
pruner = Pruning::Processing::Pruner.new(complete_tree)
pruner.prune_tree(query.indicator_ids)
tree_repo = Pruning::Repos::Tree.new(RestClient, ENV['TREE_SOURCE_API_HOSTNAME'])
complete_tree = tree_repo.get(query.name)
pruner = Pruning::Processing::Pruner.new(complete_tree)
pruner.prune_tree(query.indicator_ids)
end
end
end

13
lib/exceptions.rb Normal file
View File

@@ -0,0 +1,13 @@
module Pruning
module Exceptions
class ServerErrorOnOrigin < StandardError
end
class OriginCannotFindTheResource < StandardError
end
class UnexpectedError < StandardError
end
end
end

View File

@@ -1,6 +1,7 @@
require 'rest-client'
require 'retries'
require 'json'
require_relative '../exceptions'
module Pruning
module Repos
@@ -11,8 +12,19 @@ module Pruning
end
def get(name)
resp = @client.get(url(name))
JSON(resp.body)
with_retries(max_tries: 3, rescue: [Pruning::Exceptions::ServerErrorOnOrigin]) do
begin
resp = @client.get(url(name))
rescue RestClient::ExceptionWithResponse => e
if e.response.code != 404
raise Pruning::Exceptions::ServerErrorOnOrigin
else
raise Pruning::Exceptions::OriginCannotFindTheResource
end
end
return JSON(resp.body)
end
raise Pruning::Exceptions::UnexpectedError
end
private