2

外部サービスに対して何百もの API 呼び出しを行うアプリケーションがあります。場合によっては、応答に時間がかかりすぎる電話もあります。

時間のかかるプロセスを見つけるためにrake_timeout gem を使用しているため、応答に時間がかかりTimeout::Errorすぎるリクエストがあるたびにスローされます。私はこのエラーを救出し、そのメソッドを再試行しています:

def new
 @make_one_external_service_call = exteral_api_fetch1(params[:id])
 @make_second_external_call = exteral_api_fetch1(@make_one_external_service_call)

  #Below code will be repeated in every method

 tries = 0
rescue Timeout::Error => e
  tries += 1
  retry if tries <= 3
  logger.error e.message 
end

これにより、メソッドは完全に再実行できます。これは非常に冗長で、毎回繰り返しています。

Timeout:Error発生した場合、そのメソッドを自動的に3回再試行するようにこれを行う方法はありますか?

4

4 に答える 4

3

そのための小さなモジュールがあります:

# in lib/retryable.rb
module Retryable

  # Options:
  # * :tries - Number of tries to perform. Defaults to 1. If you want to retry once you must set tries to 2.
  # * :on - The Exception on which a retry will be performed. Defaults to Exception, which retries on any Exception.
  # * :log - The log level to log the exception. Defaults to nil.
  #
  # If you work with something like ActiveRecord#find_or_create_by_foo, remember to put that call in a uncached { } block. That
  # forces subsequent finds to hit the database again.
  #
  # Example
  # =======
  #   retryable(:tries => 2, :on => OpenURI::HTTPError) do
  #     # your code here
  #   end
  #
  def retryable(options = {}, &block)
    opts = { :tries => 1, :on => Exception }.merge(options)

    retry_exception, retries = opts[:on], opts[:tries]

    begin
      return yield
    rescue retry_exception => e
      logger.send(opts[:log], e.message) if opts[:log]
      retry if (retries -= 1) > 0
    end

    yield
  end

end

そしてあなたのモデルよりも:

extend Retryable

def new
  retryable(:tries => 3, :on => Timeout::Error, :log =>:error) do
    @make_one_external_service_call = exteral_api_fetch1(params[:id])
    @make_second_external_call = exteral_api_fetch1(@make_one_external_service_call)
  end
  ...
end
于 2013-10-14T16:13:42.170 に答える
2

次のようなことができます。

module Foo
  def self.retryable(options = {})
    retry_times   = options[:times] || 10
    try_exception = options[:on]    || Exception

    yield if block_given?
  rescue *try_exception => e
    retry if (retry_times -= 1) > 0
    raise e
  end
end

Foo.retryable(on: Timeout::Error, times: 5) do
  # your code here
end

複数の例外を「catch」に渡すこともできます。

Foo.retryable(on: [Timeout::Error, StandardError]) do
  # your code here
end
于 2013-10-14T16:15:53.150 に答える
1

必要なのは再試行可能な宝石だと思います。

gem を使用すると、以下のようにメソッドを記述できます。

def new
  retryable :on => Timeout::Error, :times => 3 do
   @make_one_external_service_call = exteral_api_fetch1(params[:id])
   @make_second_external_call = exteral_api_fetch1(@make_one_external_service_call)
  end
end

gem の使用方法とそれが提供するその他のオプションの詳細については、ドキュメントをお読みください。

于 2013-10-14T16:07:11.197 に答える
0

そのためのヘルパーメソッドを書くことができます:

class TimeoutHelper
  def call_and_retry(tries=3)
    yield
  rescue Timeout::Error => e
    tries -= 1
    retry if tries > 0
    Rails.logger.error e.message
  end
end

(完全にテストされていません)そしてそれを経由して呼び出します

TimeoutHelper.call_and_retry { [your code] }
于 2013-10-14T16:07:18.397 に答える