2013-02-11 70 views
0

我有一個Ruby應用程序,每小時執行一個rake tweet從數據庫發出一條推文。香港專業教育學院發現一對夫婦倍的鳴叫處理不當被送到,看着登錄後我發現:抓住一個特定的錯誤,然後再試一次

2013-02-11T19:00:39+00:00 app[scheduler.8796]: Connecting to database specified by DATABASE_URL 
2013-02-11T19:00:39+00:00 app[scheduler.8796]: (__-){ Twitter is over capacity. 
2013-02-11T19:00:39+00:00 app[scheduler.8796]: /app/vendor/bundle/ruby/1.9.1/gems/twitter-4.5.0/lib/twitter/response/raise_error.rb:21:in `on_complete' 
2013-02-11T19:00:39+00:00 app[scheduler.8796]: /app/vendor/bundle/ruby/1.9.1/gems/faraday-0.8.5/lib/faraday/response.rb:9:in `block in call' 
2013-02-11T19:00:39+00:00 app[scheduler.8796]: rake aborted! 
2013-02-11T19:00:39+00:00 app[scheduler.8796]: /app/vendor/bundle/ruby/1.9.1/gems/faraday-0.8.5/lib/faraday/response.rb:8:in `call' 
2013-02-11T19:00:39+00:00 app[scheduler.8796]: /app/vendor/bundle/ruby/1.9.1/gems/faraday-0.8.5/lib/faraday/response.rb:8:in `call' 
2013-02-11T19:00:39+00:00 app[scheduler.8796]: /app/vendor/bundle/ruby/1.9.1/gems/faraday-0.8.5/lib/faraday/response.rb:63:in `on_complete' 
2013-02-11T19:00:39+00:00 app[scheduler.8796]: /app/vendor/bundle/ruby/1.9.1/gems/faraday-0.8.5/lib/faraday/request/multipart.rb:13:in `call' 
2013-02-11T19:00:39+00:00 app[scheduler.8796]: /app/vendor/bundle/ruby/1.9.1/gems/faraday-0.8.5/lib/faraday/response.rb:8:in `call' 
2013-02-11T19:00:39+00:00 app[scheduler.8796]: /app/vendor/bundle/ruby/1.9.1/gems/faraday-0.8.5/lib/faraday/connection.rb:110:in `post' 
2013-02-11T19:00:39+00:00 app[scheduler.8796]: /app/vendor/bundle/ruby/1.9.1/gems/twitter-4.5.0/lib/twitter/request/multipart_with_file.rb:14:in `call' 
2013-02-11T19:00:39+00:00 app[scheduler.8796]: /app/vendor/bundle/ruby/1.9.1/gems/faraday-0.8.5/lib/faraday/connection.rb:245:in `run_request' 
2013-02-11T19:00:39+00:00 app[scheduler.8796]: /app/vendor/bundle/ruby/1.9.1/gems/faraday-0.8.5/lib/faraday/request/url_encoded.rb:14:in `call' 
2013-02-11T19:00:39+00:00 app[scheduler.8796]: /app/lib/tasks/scheduler.rake:11:in `block in <top (required)>' 
2013-02-11T19:00:39+00:00 app[scheduler.8796]: /app/vendor/bundle/ruby/1.9.1/gems/twitter-4.5.0/lib/twitter/api/tweets.rb:129:in `update' 
2013-02-11T19:00:39+00:00 app[scheduler.8796]: /app/vendor/bundle/ruby/1.9.1/gems/twitter-4.5.0/lib/twitter.rb:52:in `method_missing' 
2013-02-11T19:00:39+00:00 app[scheduler.8796]: /app/vendor/bundle/ruby/1.9.1/gems/twitter-4.5.0/lib/twitter/api/utils.rb:82:in `object_from_response' 
2013-02-11T19:00:39+00:00 app[scheduler.8796]: /app/vendor/bundle/ruby/1.9.1/gems/twitter-4.5.0/lib/twitter/client.rb:81:in `request' 
2013-02-11T19:00:39+00:00 app[scheduler.8796]: /app/vendor/bundle/ruby/1.9.1/gems/twitter-4.5.0/lib/twitter/client.rb:70:in `post' 
2013-02-11T19:00:39+00:00 app[scheduler.8796]: (See full trace by running task with --trace) 
2013-02-11T19:00:39+00:00 app[scheduler.8796]: Tasks: TOP => tweet 
2013-02-11T19:00:41+00:00 heroku[scheduler.8796]: Process exited with status 1 

我怎麼能去,直到它被髮送捕獲此特定錯誤和重試鳴叫?

在我耙文件:

task :tweet => :environment do 
if ApprovedTweet.all.size > 0 
    t = ApprovedTweet.first 
    Twitter.update(t.text) 
    t.destroy 
end 

回答

1

儘可能人們可以從Twitter gem's documentation知道的,Twitter::Error::TooManyRequests好像是你試圖捕獲錯誤。

如果我是你,我不會再發送推文直到它被髮送(特別是在一個rake任務中 - 你會很容易地以一個殭屍進程結束),而是會允許在發生故障前進行一些重試:

begin  
    Twitter.update(t.text) 
rescue Twitter::Error::TooManyRequests => e 
    retries ||= 0 
    retries += 1 
    if retries < ARBITRARY_MAX_RETRY_LIMIT 
    # optionally sleep or delay for a while 
    # ... or just log failure 
    retry 
    else 
    raise e # or another custom error of yours 
    end 
end 
相關問題