Skip to content

Commit

Permalink
Add retry middleware.
Browse files Browse the repository at this point in the history
  • Loading branch information
kj committed Jul 21, 2016
1 parent e8ffffb commit 8e4e991
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
2 changes: 0 additions & 2 deletions lib/lucid_shopify/middleware/graceful_429.rb
Expand Up @@ -13,15 +13,13 @@ def call( env )
raise Shopify429Error
end
end

rescue Shopify429Error
delay = ( attempts += 1 ) ** 2

log_error "API call limit reached, retrying in #{delay * 1000} msec ..."

sleep delay
retry

end
end

Expand Down
30 changes: 30 additions & 0 deletions lib/lucid_shopify/middleware/retry.rb
@@ -0,0 +1,30 @@
module LucidShopify::Middleware
class Retry < LucidShopify::Middleware::Base

ATTEMPTS = 3
DELAY = 0.1
EXCEPTIONS = [
Faraday::Error::ConnectionFailed,
Faraday::Error::TimeoutError
]

# Retries on connection and timeout errors.
#
def call( env )
attempts = 0

begin
app.call( env.dup )
rescue *EXCEPTIONS => e
attempts += 1
raise e if attempts >= ATTEMPTS

log_error "Connection failed, retrying in #{DELAY * 1000} msec ..."

sleep DELAY
retry
end
end

end
end
2 changes: 1 addition & 1 deletion lib/lucid_shopify/session.rb
Expand Up @@ -19,7 +19,7 @@ def self.for_shop( shop, options = {} )
private

def _middleware
%i{ Graceful429 CallLogger Token }.map do |middleware|
%i{ Retry Graceful429 CallLogger Token }.map do |middleware|
LucidShopify::Middleware.const_get( middleware )
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/lucid_shopify/version.rb
@@ -1,5 +1,5 @@
module LucidShopify

VERSION = '0.2.2'
VERSION = '0.2.3'

end

0 comments on commit 8e4e991

Please sign in to comment.