【问题标题】:How prevent the script stops? for 504 Gateway Timeout Rails如何防止脚本停止?用于 504 网关超时 Rails
【发布时间】:2014-07-02 19:26:49
【问题描述】:

我用https://github.com/ollieglass/share-counter

我的脚本

posts.each do |pp|

    begin
      puts pp.post_link

      url = URI.parse("#{pp.post_link}")
      req = Net::HTTP.new(url.host, url.port)
      res = req.request_head(url.path)

      if res.code == "200"

        shared_social_count = ShareCounter.all "#{pp.post_link}"

        unless shared_social_count.blank?

          sd = SharedCount.find_or_create_by post_id: "#{pp.id}"

          sd.twitter = shared_social_count[:twitter]
          sd.facebook = shared_social_count[:facebook]
          sd.linkedin = shared_social_count[:linkedin]
          sd.googleplus = shared_social_count[:googleplus]

          sd.save

        end
      end
    rescue Exception => e
       puts "caught exception #{e}! ohnoes!"

      next

    end
  end

运行良好一段时间,但一些消息导致错误并停止循环

http://www.prnoticias.com/index.php/comunicacion/1224-foro-mundial/20130612-la-asesora-del-papa-tambien-estara-en-la-wprf-
Failed 1 attempt(s) - 504 Gateway Timeout
Failed 2 attempt(s) - 504 Gateway Timeout
Failed 3 attempt(s) - 504 Gateway Timeout
rake aborted!
Exception: Exception
RestClient::GatewayTimeout: 504 Gateway Timeout

知道如何避免不停止循环吗?

【问题讨论】:

    标签: ruby-on-rails ruby exception ruby-on-rails-4 timeout


    【解决方案1】:
    if res.code == "504"
        next
        ## create any log entry to know this url return 504
    end
    

    这将捕获 504 错误并跳到数组中的下一个值我的建议移至 sidekiq,这样管理起来会更容易:

    https://github.com/mperham/sidekiq

    http://railscasts.com/episodes/366-sidekiq

    【讨论】:

      猜你喜欢
      • 2011-04-26
      • 2017-11-21
      • 2013-04-06
      • 1970-01-01
      • 2016-10-11
      • 2012-02-25
      • 1970-01-01
      • 2013-09-04
      • 2012-08-06
      相关资源
      最近更新 更多