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

@@ -10,3 +10,5 @@ gem "rack-indifferent", "~> 1.2"
gem "sinatra-router", "~> 0.2.4" gem "sinatra-router", "~> 0.2.4"
gem "rest-client", "~> 2.0" gem "rest-client", "~> 2.0"
gem "sinatra-contrib", "~> 2.0"

View File

@@ -1,14 +1,25 @@
GEM GEM
remote: https://rubygems.org/ remote: https://rubygems.org/
specs: specs:
activesupport (5.2.1)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
backports (3.11.3)
concurrent-ruby (1.0.5)
domain_name (0.5.20180417) domain_name (0.5.20180417)
unf (>= 0.0.5, < 1.0.0) unf (>= 0.0.5, < 1.0.0)
dotenv (2.5.0) dotenv (2.5.0)
http-cookie (1.0.3) http-cookie (1.0.3)
domain_name (~> 0.5) domain_name (~> 0.5)
i18n (1.1.0)
concurrent-ruby (~> 1.0)
mime-types (3.1) mime-types (3.1)
mime-types-data (~> 3.2015) mime-types-data (~> 3.2015)
mime-types-data (3.2016.0521) mime-types-data (3.2016.0521)
minitest (5.11.3)
multi_json (1.13.1)
mustermann (1.0.2) mustermann (1.0.2)
netrc (0.11.0) netrc (0.11.0)
rack (2.0.5) rack (2.0.5)
@@ -26,9 +37,20 @@ GEM
rack (~> 2.0) rack (~> 2.0)
rack-protection (= 2.0.3) rack-protection (= 2.0.3)
tilt (~> 2.0) tilt (~> 2.0)
sinatra-contrib (2.0.3)
activesupport (>= 4.0.0)
backports (>= 2.8.2)
multi_json
mustermann (~> 1.0)
rack-protection (= 2.0.3)
sinatra (= 2.0.3)
tilt (>= 1.3, < 3)
sinatra-router (0.2.4) sinatra-router (0.2.4)
sinatra (>= 1.4, < 3.0) sinatra (>= 1.4, < 3.0)
thread_safe (0.3.6)
tilt (2.0.8) tilt (2.0.8)
tzinfo (1.2.5)
thread_safe (~> 0.1)
unf (0.1.4) unf (0.1.4)
unf_ext unf_ext
unf_ext (0.0.7.5) unf_ext (0.0.7.5)
@@ -42,6 +64,7 @@ DEPENDENCIES
rest-client (~> 2.0) rest-client (~> 2.0)
retries (~> 0.0.5) retries (~> 0.0.5)
sinatra (~> 2.0) sinatra (~> 2.0)
sinatra-contrib (~> 2.0)
sinatra-router (~> 0.2.4) sinatra-router (~> 0.2.4)
BUNDLED WITH BUNDLED WITH

View File

@@ -1,15 +1,9 @@
require 'bundler/setup' require 'bundler/setup'
require 'dotenv/load' require 'dotenv/load'
require 'rack/indifferent' require 'rack/indifferent'
require 'sinatra/router'
require_relative 'lib/api/tree.rb' require_relative 'lib/api/tree.rb'
app = Sinatra::Router.new do run Pruning::API::Tree
mount Pruning::API::Tree
end
map '/' do
run app
end

View File

@@ -1,15 +1,32 @@
require 'json' require 'json'
require 'sinatra/base' require 'sinatra/base'
require 'sinatra/custom_logger'
require_relative '../http/query' require_relative '../http/query'
require_relative '../exceptions'
module Pruning module Pruning
module API module API
class App < Sinatra::Base class App < Sinatra::Base
before { content_type :json } before { content_type :json }
after { serialise_response } 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 private
def serialise_response def serialise_response
return unless content_type == 'application/json' return unless content_type == 'application/json'
response.body = [JSON(response.body)] response.body = [JSON(response.body)]

View File

@@ -1,16 +1,18 @@
require 'rest-client' require 'rest-client'
require 'retries'
require_relative 'app' require_relative 'app'
require_relative '../repos/tree' require_relative '../repos/tree'
require_relative '../pruner' require_relative '../pruner'
require_relative '../exceptions'
module Pruning module Pruning
module API module API
class Tree < App class Tree < App
get '/tree/:name' do get '/tree/:name' do
tree_repo = Pruning::Repos::Tree.new(RestClient, ENV['TREE_SOURCE_API_HOSTNAME']) tree_repo = Pruning::Repos::Tree.new(RestClient, ENV['TREE_SOURCE_API_HOSTNAME'])
complete_tree = tree_repo.get(query.name) complete_tree = tree_repo.get(query.name)
pruner = Pruning::Processing::Pruner.new(complete_tree) pruner = Pruning::Processing::Pruner.new(complete_tree)
pruner.prune_tree(query.indicator_ids) pruner.prune_tree(query.indicator_ids)
end end
end 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 'rest-client'
require 'retries' require 'retries'
require 'json' require 'json'
require_relative '../exceptions'
module Pruning module Pruning
module Repos module Repos
@@ -11,8 +12,19 @@ module Pruning
end end
def get(name) def get(name)
resp = @client.get(url(name)) with_retries(max_tries: 3, rescue: [Pruning::Exceptions::ServerErrorOnOrigin]) do
JSON(resp.body) 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 end
private private