2017-08-14 46 views
0

我有一個ruby腳本,它使用Bunny Gem連接到一個rabbitmq實例。該腳本了一段時間,但最終會因爲網絡而死亡:: ReadTimeout從Bunny連接網絡中解救麻煩:: ReadTimeout

E, [2017-08-13T08:48:09.671988 #21351] ERROR -- #<Bunny::Session:0x39eca20 [email protected]:5672, vhost=/, addresses=[104.196.154.25:5672]>: Uncaught exception from consumer #<Bunny::Consumer:32353120 @channel_id=1 @queue=sc_link_queue> @c 
onsumer_tag=bunny-1502631967000-46739673895>: #<Net::ReadTimeout: Net::ReadTimeout> @ /home/rails/.rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/net/protocol.rb:158:in `rbuf_fill' 
E, [2017-08-13T08:48:32.468023 #23205] ERROR -- #<Bunny::Session:0x42202a0 [email protected]:5672, vhost=/, addresses=[104.196.154.25:5672]>: Uncaught exception from consumer #<Bunny::Consumer:36695920 @channel_id=1 @queue=sc_link_queue> @c 
onsumer_tag=bunny-1502631972000-482787698591>: #<Net::ReadTimeout: Net::ReadTimeout> @ /home/rails/.rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/net/protocol.rb:158:in `rbuf_fill' 

我的劇本是這樣的

module Sc 
    class Worker 
    def initialize 
     init() 
    end 

    def self.start_headless(type) 
     Headless.new(display: 50, destroy_at_exit: false, resuse: true).start 
     worker = new 
     worker.send(type) 
    end 

    def init 
     $conn ||= Bunny.new($rabbitmq_opts) 
     $conn.start 
     @browser = Sc::Browser.new() 
    rescue Timeout::Error, Net::ReadTimeout, Selenium::WebDriver::Error::UnknownError, Errno::ECONNREFUSED, Selenium::WebDriver::Error::JavascriptError, Exception, StandardError => e 
     LOGGER.error("[x] Trouble connecting to rabbitmq, retrying...") 
     LOGGER.error("[x] #{e}") 
     LOGGER.error("[x] #{e.backtrace}") 
     retry 
    end 

    def listen_for_searches 
     channel = $conn.create_channel 
     channel.prefetch(1) 
     queue = channel.queue($rabbitmq_search_queue, durable: true) 
     exchange = channel.default_exchange 
     queue.subscribe(:manual_ack => true, :block => true) do |delivery_info, properties, payload| 
     LOGGER.info "[x] Received #{payload}" 
     payload = JSON.parse(payload) 
     scrape = Sc::Search.new(browser: @browser.browser, county: payload["name"], type: payload["type"], date_type: payload["date_type"]) 
     scrape.run 
     scrape.close 
     channel.ack(delivery_info.delivery_tag) 
     end 
    rescue Timeout::Error, Net::ReadTimeout, Selenium::WebDriver::Error::UnknownError, Errno::ECONNREFUSED, Selenium::WebDriver::Error::JavascriptError, Exception, StandardError => e 
     LOGGER.error("[x] #{e}") 
     LOGGER.error("[x] #{e.backtrace}") 
     LOGGER.error("[x] Trouble with scrape, retrying...") 
     retry 
    end 
    end 
end 

正如你可以看到我試圖從幾乎營救所有可能發生的事情。我似乎仍然無法從Net :: ReadTimeout錯誤中恢復。一旦工人死亡,您仍然可以看到它已連接到rabbitmq,但從隊列中取出的最後一個項目未被確認,它基本上是掛起的。

回答

0

我已經解決了這個問題。問題在於,在Bunny訂閱模塊內運行的所有內容都是在不同的線程中處理的,因此您需要在該模塊內部添加救援語句。