0

具有以下长期运行的 rake 批次:

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,后者继承自StandardError,如您在此处看到的。

更新

我刚刚更新了以前的简化代码,使用 rake 使用的完整SyncStarredRepo类,以防万一我在循环逻辑中遗漏了一些东西,开始/救援/结束等等......

更新

按照以下方式打印的堆栈跟踪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
4

2 回答 2

1

期间引发了错误,User.all.map因此您必须将begin..rescue块包裹在整个语句周围。

基于初始信息的旧答案:

您正在循环之外进行救援,因此retry不会完全按照您next的预期进行(另一方面会引发SyntaxError(1.9)或LocalJumpError(1.8))。实际上,retry在 a 中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?

于 2012-12-29T15:54:41.400 回答
0

尽管我没有仔细查看您的代码,但请注意,bare无法rescue捕获所有异常。为此,您需要.rescue Exception

于 2012-12-29T19:51:53.437 回答