3
3

More than 5 years have passed since last update.

railsでAmazon-ecsがAmazon::RequestError (HTTP Response: 503 Service Unavailable)となるとき

Last updated at Posted at 2016-01-21

症状

railsにamazon-acsをrequireして商品情報検索してるのに

hoge_controller.rb
class HogehogeController < ApplicationController
  def hoge
    @res = Amazon::Ecs.item_search('9784797372274',
             :search_index   => 'Books',
             :response_group => 'Medium',
             :country        => 'jp'
           )
  end
end

viewでインスタンス参照すると

hoge.html.erb
<%= @res.first_item.get('ItemAttributes/Title') %>
<img src="<%= @res.first_item.get('MediumImage/URL') %>" />

Amazon::RequestError (HTTP Response: 503 Service Unavailable)

となってうまくいかない

原因

リクエストの間隔が短いのがいけないらしい
今回の場合controllerとviewでそれぞれリクエスト送っているので間隔を開ければ良い

hoge_controller.rb
class HogehogeController < ApplicationController
  def hoge
    @res = Amazon::Ecs.item_search('9784797372274',
             :search_index   => 'Books',
             :response_group => 'Medium',
             :country        => 'jp'
           )
    sleep(1) #これを追加,最適な秒数はよくわかりません
  end
end

追記

よく考えたら普通にcontroller内でリクエストの内容をインスタンス変数に代入すればそもそもviewでリクエストする必要ない

お世話になったサイト

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3