2011-04-27 4 views
2

Railsプロジェクトの一環として、Rails RESTful Webサービスフロントエンドからのコマンドを実行するためにデーモンをメッセージキューリスナーとして使用します。Rails 3 + Daemons gem:モデル照会時の例外

プロトタイプを簡単にするため、Daemons gemを使用して非常に単純なデーモンを作成しています。今、それは超簡単です。ここでは、コードは次のとおりです。基本的に、それはデーモンのものを必要とし、その後、私のRubyの環境が必要です

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 

、その後、デーモンにして起動します。デーモンは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のは、この例外がスローされた理由を任意のアイデア?環境を必要とするだけでなく、レール環境をデーモンに「ブートストラップ」するために必要な追加ステップがありますか?

+1

後にロガーを再インスタンス化しようとしますRails.logger = ActiveSupport :: BufferedLogger.new( '/ path/to/log')を使用してDaemons.run_proc( 'aeon_server')を実行します – Roman

+0

これが実行されました。ありがとう、トン。そのコメントを「回答」にしたいのであれば、それをマークしてあなたがクレジットを得るようにします。 –

答えて

4

プロセスがfork-on-spawned/what-it-called-on-windowsのときにファイル記述子に関するいくつかの複雑さがあります。

あなたはRails.logger = activesupportのでDaemons.run_proc( 'aeon_server')を行った後、ロガーを再インスタンス化しよう:: BufferedLogger.new( '/パス/ /ログインする')

+0

これはトリックでした。それはWindows上でも良いキャッチです。 Linux(アプリケーションの最終的なホーム)にデプロイするときにそのコードを実行してはいけませんか、またはロガーをインスタンス化するだけで安全ですか? –

+2

通常、フォークするときに行うのが良い考えです。また、新しいロガーインスタンスを割り当てる前に、Rails.logger.close(レスキューIOError)を呼び出すようにしてください。 – Roman

+0

完了しました。再度、感謝します。これは今、チャンピオンのように働いています。 –