我使用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
知道如何避免不停止循环吗?
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