2011-04-27 36 views
2

作爲我的rails項目的一部分,我們將使用守護進程作爲消息隊列偵聽器來執行來自Rails RESTful web服務前端的命令。Rails 3 +守護進程gem:查詢模型時出現異常

爲了便於原型設計,我們使用守護進程gem來創建一個非常簡單的守護進程。現在,這非常簡單。下面是代碼:

require 'rubygems' 
require 'daemons' 

require File.expand_path('../../config/environment', __FILE__) 

Daemons.run_proc('aeon_server') do 
    loop do 
     empires = Empire.all 
     sleep(5) 
    end 
end 

基本上,它需要守護的東西,那麼需要我的Ruby環境,然後啓動到後臺程序。守護進程只是試圖查詢Empires表中的所有內容。然後它再睡一遍。

當它關係到執行「所有」,我得到以下異常:

C:/Ruby/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/buffered_logger.rb:109:in `write': closed stream (IOError) 
from C:/Ruby/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/buffered_logger.rb:109:in `block in flush' 
from <internal:prelude>:10:in `synchronize' 
from C:/Ruby/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/buffered_logger.rb:102:in `flush' 
from C:/Ruby/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/buffered_logger.rb:126:in `auto_flush' 
from C:/Ruby/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/buffered_logger.rb:67:in `add' 
from C:/Ruby/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/buffered_logger.rb:78:in `debug' 
from C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/connection_adapters/abstract_adapter.rb:206:in `rescue in log' 
from C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/connection_adapters/abstract_adapter.rb:199:in `log' 
from C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/connection_adapters/sqlite_adapter.rb:135:in `execute' 
from C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/connection_adapters/sqlite_adapter.rb:284:in `select' 
from C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in `select_all' 
from C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/connection_adapters/abstract/query_cache.rb:56:in `select_all' 
from C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/base.rb:468:in `find_by_sql' 
from C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/relation.rb:64:in `to_a' 
from C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/relation/finder_methods.rb:143:in `all' 
from C:in `all' 
from script/aeon_server_control.rb:9:in `block (2 levels) in <main>' 
from script/aeon_server_control.rb:7:in `loop' 
from script/aeon_server_control.rb:7:in `block in <main>' 
from C:/Ruby/lib/ruby/gems/1.9.1/gems/daemons-1.1.3/lib/daemons/application.rb:249:in `call' 
from C:/Ruby/lib/ruby/gems/1.9.1/gems/daemons-1.1.3/lib/daemons/application.rb:249:in `block in start_proc' 
from C:/Ruby/lib/ruby/gems/1.9.1/gems/daemons-1.1.3/lib/daemons/application.rb:260:in `call' 
from C:/Ruby/lib/ruby/gems/1.9.1/gems/daemons-1.1.3/lib/daemons/application.rb:260:in `start_proc' 
from C:/Ruby/lib/ruby/gems/1.9.1/gems/daemons-1.1.3/lib/daemons/application.rb:293:in `start' 
from C:/Ruby/lib/ruby/gems/1.9.1/gems/daemons-1.1.3/lib/daemons/controller.rb:73:in `run' 
from C:/Ruby/lib/ruby/gems/1.9.1/gems/daemons-1.1.3/lib/daemons.rb:195:in `block in run_proc' 
from C:/Ruby/lib/ruby/gems/1.9.1/gems/daemons-1.1.3/lib/daemons/cmdline.rb:109:in `call' 
from C:/Ruby/lib/ruby/gems/1.9.1/gems/daemons-1.1.3/lib/daemons/cmdline.rb:109:in `catch_exceptions' 
from C:/Ruby/lib/ruby/gems/1.9.1/gems/daemons-1.1.3/lib/daemons.rb:194:in `run_proc' 
from script/aeon_server_control.rb:6:in `<main>' 

任何想法,爲什麼的ActiveSupport拋出此異常?除了需要環境之外,是否還需要額外的步驟來將我的rails環境引導到守護進程?

+1

嘗試後,一個重新實例記錄儀你使用Rails.logger = ActiveSupport :: BufferedLogger.new('/ path/to/log')做Daemons.run_proc('aeon_server') – Roman 2011-04-27 20:39:01

+0

這樣做。萬分感謝。如果你想讓這個評論成爲「答案」,我會將它標記出來,這樣你就可以獲得信用。 – 2011-04-27 20:50:22

回答

4

當進程分叉/產生/不管它被稱爲windows時,關於文件描述符有一些複雜性。

嘗試一個重新實例記錄你Rails.logger =的ActiveSupport做Daemons.run_proc( 'aeon_server')後:: BufferedLogger.new( '/路徑/到/日誌')

+0

這個技巧。不錯,它也在Windows上。我應該在部署到Linux(應用程序的最終主目錄)時不運行該代碼,還是僅僅實例化記錄器是安全的? – 2011-04-27 20:55:45

+2

通常在分叉時這樣做是個好主意。在分配新的記錄器實例之前,還要嘗試調用Rails.logger.close(使用rescue IOError)。 – Roman 2011-04-27 20:59:20

+0

完成並完成。再次感謝。這現在像一個冠軍。 – 2011-04-27 21:42:47