拥有以下长期运行的佣金批次:
class SyncStarredRepo
include Mongoid::Document
def self.update
User.all.map do |user|
if user.email != "admin@gitwatcher.com"
begin
conn = Faraday.new "https://api.github.com"
i = 1
begin
resp = conn.get "/users/#{user.nickname}/starred?page=#{i}"
resp.body.each do |repo|
attempts = 0
begin
existing_repo = user.watchlists.where( html_url: "#{repo['html_url']}" ).batch_size(30)
if existing_repo.exists?
existing_repo.first.update_attributes!(
html_url: "#{repo['html_url']}",
description: "#{repo['description']}"
)
else
user.watchlists.create!(
html_url: "#{repo['html_url']}",
description: "#{repo['description']}"
)
end
rescue Moped::Errors::CursorNotFound => c
attempts += 1
puts "error message: #{c.message}"
if attempts < 3
puts "Retrying... Attempt #: #{attempts.to_s}"
sleep(3)
retry
else
puts "already retried 3 times, go to the next"
next
end
end
end
i += 1
end until resp.body.empty?
rescue TypeError => e
puts "message error (e.inspect): #{e.inspect}"
puts "go to the next user"
next
rescue
p $!, *$@
next
end
end
end
end
end
我希望能够捕获Moped :: Errors :: CursorNotFound异常,但它不起作用。
在crontab日志中,我收到此消息:
rake aborted!
The operation: "GET MORE"
failed with error "cursor 2509315744412059250 not found"
然后批处理停止并退出,而我正在描述救援消息和下一个循环操作。
更新
我试图通过这样做来“扩展”可捕获的异常集 StandardError :
rescue
p $!, *$@
next
end
...以防万一,即使 CursorNotFound 继承自标准错误的 MongoError ,你也可以看到{{3 }}
更新
我刚刚使用rake使用的完整 SyncStarredRepo 类更新了以前的简化代码,以防我在循环逻辑中缺少某些内容,开始/救援/结束等等...... / p>
更新
按照rake -t
打印的堆栈跟踪:
rake aborted!
The operation: "GET MORE"
failed with error "cursor 5781947091640256877 not found"
/var/www/gitwatcher/vendor/bundle/ruby/1.9.1/gems/moped-1.3.2/lib/moped/node.rb:210:in `get_more'
/var/www/gitwatcher/vendor/bundle/ruby/1.9.1/gems/moped-1.3.2/lib/moped/cursor.rb:44:in `get_more'
/var/www/gitwatcher/vendor/bundle/ruby/1.9.1/gems/moped-1.3.2/lib/moped/cursor.rb:29:in `each'
/var/www/gitwatcher/vendor/bundle/ruby/1.9.1/gems/moped-1.3.2/lib/moped/query.rb:77:in `each'
/var/www/gitwatcher/vendor/bundle/ruby/1.9.1/gems/moped-1.3.2/lib/moped/query.rb:77:in `each'
/var/www/gitwatcher/vendor/bundle/ruby/1.9.1/bundler/gems/mongoid-92cad16e9fbf/lib/mongoid/contextual/mongo.rb:133:in `block in each'
/var/www/gitwatcher/vendor/bundle/ruby/1.9.1/bundler/gems/mongoid-92cad16e9fbf/lib/mongoid/contextual/mongo.rb:605:in `selecting'
/var/www/gitwatcher/vendor/bundle/ruby/1.9.1/bundler/gems/mongoid-92cad16e9fbf/lib/mongoid/contextual/mongo.rb:132:in `each'
/var/www/gitwatcher/vendor/bundle/ruby/1.9.1/bundler/gems/mongoid-92cad16e9fbf/lib/mongoid/contextual.rb:18:in `each'
/var/www/gitwatcher/app/models/sync_starred_repo.rb:15:in `map'
/var/www/gitwatcher/app/models/sync_starred_repo.rb:15:in `update'
/var/www/gitwatcher/lib/tasks/scheduler.rake:9:in `block in <top (required)>'
/usr/local/rvm/gems/ruby-1.9.3-p194@rails-3.2.8/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call'
/usr/local/rvm/gems/ruby-1.9.3-p194@rails-3.2.8/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `block in execute'
/usr/local/rvm/gems/ruby-1.9.3-p194@rails-3.2.8/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `each'
/usr/local/rvm/gems/ruby-1.9.3-p194@rails-3.2.8/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `execute'
/usr/local/rvm/gems/ruby-1.9.3-p194@rails-3.2.8/gems/rake-0.9.2.2/lib/rake/task.rb:158:in `block in invoke_with_call_chain'
/usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/usr/local/rvm/gems/ruby-1.9.3-p194@rails-3.2.8/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
/usr/local/rvm/gems/ruby-1.9.3-p194@rails-3.2.8/gems/rake-0.9.2.2/lib/rake/task.rb:144:in `invoke'
/usr/local/rvm/gems/ruby-1.9.3-p194@rails-3.2.8/gems/rake-0.9.2.2/lib/rake/application.rb:116:in `invoke_task'
/usr/local/rvm/gems/ruby-1.9.3-p194@rails-3.2.8/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block (2 levels) in top_level'
/usr/local/rvm/gems/ruby-1.9.3-p194@rails-3.2.8/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `each'
/usr/local/rvm/gems/ruby-1.9.3-p194@rails-3.2.8/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block in top_level'
/usr/local/rvm/gems/ruby-1.9.3-p194@rails-3.2.8/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/usr/local/rvm/gems/ruby-1.9.3-p194@rails-3.2.8/gems/rake-0.9.2.2/lib/rake/application.rb:88:in `top_level'
/usr/local/rvm/gems/ruby-1.9.3-p194@rails-3.2.8/gems/rake-0.9.2.2/lib/rake/application.rb:66:in `block in run'
/usr/local/rvm/gems/ruby-1.9.3-p194@rails-3.2.8/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/usr/local/rvm/gems/ruby-1.9.3-p194@rails-3.2.8/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run'
/usr/local/rvm/gems/ruby-1.9.3-p194@rails-3.2.8/gems/rake-0.9.2.2/bin/rake:33:in `<top (required)>'
/usr/local/rvm/gems/ruby-1.9.3-p194@rails-3.2.8/bin/rake:23:in `load'
/usr/local/rvm/gems/ruby-1.9.3-p194@rails-3.2.8/bin/rake:23:in `<main>'
Tasks: TOP => update_starred_list_per_user
答案 0 :(得分:1)
User.all.map
期间出现错误,因此您必须将begin
.. rescue
块包围在整个声明中。
基于初始信息的旧答案:
你在循环之外进行抢救,因此 retry
并没有完全符合您的预期(next
另一方面会提升SyntaxError
(1.9)或LocalJumpError
(1.8))。实际上,retry
中的rescue
只会导致begin
.. rescue
块内的代码再次运行。
正确的代码应如下所示:
attempts = 0
begin
i = 1
resp = conn.get "/users/#{user.nickname}/starred?page=#{i}"
resp.body.each do |repo|
begin
existing_repo = user.watchlists.where( html_url: "#{repo['html_url']}" )
if existing_repo.exists?
existing_repo.first.update_attributes!(
html_url: "#{repo['html_url']}",
description: "#{repo['description']}"
)
else
user.watchlists.create!(
html_url: "#{repo['html_url']}",
description: "#{repo['description']}"
)
end
rescue Moped::Errors::CursorNotFound => c
attempts += 1
puts "error message: #{c.message}"
if attempts < 3
puts "Retrying... Attempt #: #{attempts.to_s}"
sleep(3)
retry
else
puts "already retried 3 times, go to the next"
next
end
end
end
i += 1
end until resp.body.empty?
击> <击> 撞击>
答案 1 :(得分:0)
虽然我没有仔细查看您的代码,但请注意,只有rescue
的所有异常都会被捕获。为此,您需要rescue Exception
。