Rails 3 +守护进程宝石:查询模型时例外 [英] Rails 3 + Daemons gem: Exception when querying model

查看:93
本文介绍了Rails 3 +守护进程宝石:查询模型时例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为我的rails项目的一部分,我们将使用守护进程作为消息队列侦听器来执行来自Rails RESTful web服务前端的命令。



易于原型设计,我们使用守护进程创建一个非常简单的守护进程。现在,这非常简单。代码如下:

  require'rubygems'
require'守护进程'

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

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

基本上,它需要守护进程的东西,然后需要我的Ruby环境,然后启动守护进程。守护进程只是试图查询Empires表中的所有内容。然后它再次入睡并重新执行。



执行'all'时,出现以下异常:

  C:/Ruby/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/buffered_logger.rb:109:在写入中:封闭流(IOError)
来自C:/Ruby/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/buffered_logger.rb:109:在`block in flush'中$来自< internal:prelude>的b $ b:10:在`同步'
从C:/Ruby/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/buffered_logger .rb:102:在`flush'中
来自C:/Ruby/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/buffered_logger.rb:126:在`auto_flush '来自C的
:/Ruby/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/buffered_logger.rb:67:在'add'中
从C: /Ruby/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/buffered_logger.rb:78:in`debug'$ b $ 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 :在`执行'
从C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/connection_adapters/sqlite_adapter.rb:284:在`select'$来自C的b $ b:/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:在`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
来自C:/Ruby/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/relation/finder_methods.rb:143:在所有中
来自C :'all'
from脚本/ aeon_server_control.rb:9:'block'(2 levels)in< main>'
from script / aeon_server_control.rb:7:in'loop'
from script / aeon_server_control.rb:7:在`block in< main>'
from C:/Ruby/lib/ruby/gems/1.9.1/gems/daemons-1.1.3/lib/ daemons / application.rb:249:在`call'中
来自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'$ b $ c from C:/Ruby/lib/ruby/gems/1.9.1/gems/daemons-1.1.3/lib/daemons/application.rb:260:在`start_proc'
从C:/ Ruby / lib / ruby​​ / gems / 1.9.1 / gems / daemons-1.1.3 / lib / daemons / application.rb:293:在`start'
从C:/Ruby/lib/ruby/gems/1.9。 1 / gems / daemons-1.1.3 / lib / daemons / controller.rb:73:在`run'
从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从C:/Ruby/lib/ruby/gems/1.9.1/gems/daemons-1.1.3/lib调用
中的/daemons-1.1.3/lib/daemons/cmdline.rb:109:`` /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脚本/ aeon_server_control.rb:6:`< main>'



<任何想法为什么ActiveSupport抛出这个异常?我需要额外的步骤来将我的rails环境引导到守护进程中,而不仅仅是需要环境吗?

对于文件描述符来说,当进程被分叉/产生/不管它是什么时候被称为在windows上的时候,都会有一些错综复杂的情况。

尝试在记录器之后重新实例化你使用Rails.logger = ActiveSupport :: BufferedLogger.new('/ path / to / log')做了Daemons.run_proc('aeon_server')

As part of my rails project, we are going to use a daemon as a message queue listener to execute commands coming from a Rails RESTful webservice front end.

For ease of prototyping, we are using the Daemons gem to create a very simple daemon. Right now, it's super simple. Here is the code:

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

Basically, it requires the things for daemons, then requires my Ruby environment, then launches in to the daemon. The daemon simply attempts to query everything from the Empires table. Then it sleeps and does it again.

When it goes to execute 'all', I get the following exception:

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>'

Any idea why ActiveSupport is throwing this exception? Are there additional steps I need to "bootstrap" my rails environment in to the daemon, beyond just requiring the environment?

解决方案

There're some intricacies with regard to file descriptors when the process is forked/spawned/whatever-it-is-called-on-windows.

Try to reinstantiate a logger after you do Daemons.run_proc('aeon_server') with Rails.logger = ActiveSupport::BufferedLogger.new('/path/to/log')

这篇关于Rails 3 +守护进程宝石:查询模型时例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆