【问题标题】:Rails populate db rake task OpenURI::HTTPError: 500 Internal Server ErrorRails 填充 db rake 任务 OpenURI::HTTPError: 500 Internal Server Error
【发布时间】:2016-01-20 19:51:04
【问题描述】:

我正在尝试执行 rake 任务以从 JSON API fixer.io 填充 db, 但是当我输入我的耙子时:
耙分贝:填充
出现此错误:

OpenURI::HTTPError: 500 Internal Server Error
/home/jakub/Documents/workspace/exch/lib/tasks/populate.rake:16:in `block (5 levels) in <top (required)>'
/home/jakub/Documents/workspace/exch/lib/tasks/populate.rake:13:in `block (4 levels) in <top (required)>'
/home/jakub/Documents/workspace/exch/lib/tasks/populate.rake:12:in `each'
/home/jakub/Documents/workspace/exch/lib/tasks/populate.rake:12:in `block (3 levels) in <top (required)>'
/home/jakub/Documents/workspace/exch/lib/tasks/populate.rake:11:in `each'
/home/jakub/Documents/workspace/exch/lib/tasks/populate.rake:11:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:populate
(See full trace by running task with --trace)

这是我在 lib/tasks 中的 rake 任务 (populate.rake):

require 'open-uri'

namespace :db do
  desc "Erase and fill database"
  task :populate => :environment do
    require 'populator'

    [ConversionRate].each(&:delete_all)
    n = JSON.load(open('http://api.fixer.io/latest'))["rates"].keys.count
    currencies = JSON.load(open('http://api.fixer.io/latest'))["rates"].keys
    currencies.each do |curr1|
      currencies.each do |curr2|
        ConversionRate.populate 1 do |cr|
          cr.currency1 = curr1
          cr.currency2 = curr2
          cr.conversion_rate = JSON.load(open('http://api.fixer.io/latest?base=' + curr1))["rates"][curr2]
      end
    end
  end
  end
end

请帮帮我,我不知道是什么导致了这个问题。

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4 rake


    【解决方案1】:

    主要问题在这里,您没有获取"rates"的密钥

    currencies = JSON.load(open('http://api.fixer.io/latest')).keys
    
    currencies = JSON.load(open('http://api.fixer.io/latest'))["rates"].keys
    

    require 'open-uri'
    
    namespace :db do
      desc "Erase and fill database"
      task :populate => :environment do
        require 'populator'
    
        [ConversionRate].each(&:delete_all)
    
        latest_data = JSON.load(open('http://api.fixer.io/latest'))
    
        currencies = latest_data["rates"].keys
        n = currencies.count
    
        currencies.each do |curr1|
          currencies.each do |curr2|
            ConversionRate.populate 1 do |cr|
              #take care of any error, as we are going to call third party api here
              begin
                cr.currency1 = curr1
                cr.currency2 = curr2
                cr.conversion_rate = JSON.load(open('http://api.fixer.io/latest?base=' + curr1))["rates"][curr2]
              rescue => e
                puts "error #{e}"
              end
            end
          end
          #give a bit rest
          sleep 2
        end
      end
    end
    

    【讨论】:

    • 是的,我注意到缺少键 :D
    • 但同样的错误仍然发生......我想我对这个 API 的请求太多了?有没有办法解决这个问题?只有当我像currencies[0..2].each 一样使这个循环变小时,它才会显示这个错误
    • 试着把睡眠也放在里面
    • 我将睡眠 2 更改为睡眠 4,效果非常棒!填充 930 次转化需要 1 小时以上 :D 感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 2017-02-20
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 2015-08-16
    相关资源
    最近更新 更多