Rails eventmachine 未初始化 evma_install_oneshot_timer

Posted

技术标签:

【中文标题】Rails eventmachine 未初始化 evma_install_oneshot_timer【英文标题】:Rails eventmachine not initialized evma_install_oneshot_timer 【发布时间】:2014-07-21 21:13:08 【问题描述】:

我正在使用Rails 4.1.1Thin 1.6.2Redis 2.8.9(带有Hiredis 驱动程序)和faye-rails gem。

我使用faye-rails 订阅一些模型的更改, 我将Redis 用于其他(不相关的)内容。

当我尝试在 faye-observed 模型(比如说 Apple)上调用 create 时,它会引发如下错误:

2.1.1 :001 > Apple.create
   (0.1ms)  BEGIN
  SQL (0.4ms)  INSERT INTO `apples` (`created_at`, `updated_at`) VALUES ('2014-06-01 17:26:54', '2014-06-01 17:26:54')
   (7.6ms)  ROLLBACK
RuntimeError: eventmachine not initialized: evma_install_oneshot_timer
    from /home/david/.rvm/gems/ruby-2.1.1/gems/eventmachine-1.0.3/lib/eventmachine.rb:323:in `add_oneshot_timer'
    from /home/david/.rvm/gems/ruby-2.1.1/gems/eventmachine-1.0.3/lib/eventmachine.rb:323:in `add_timer'
    from /home/david/.rvm/gems/ruby-2.1.1/gems/eventmachine-1.0.3/lib/em/timers.rb:12:in `initialize'
    from /home/david/.rvm/gems/ruby-2.1.1/gems/eventmachine-1.0.3/lib/em/deferrable.rb:173:in `new'
    from /home/david/.rvm/gems/ruby-2.1.1/gems/eventmachine-1.0.3/lib/em/deferrable.rb:173:in `timeout'
    from /home/david/.rvm/gems/ruby-2.1.1/gems/faye-1.0.1/lib/faye/protocol/envelope.rb:11:in `initialize'
    from /home/david/.rvm/gems/ruby-2.1.1/gems/faye-1.0.1/lib/faye/protocol/client.rb:357:in `new'
    from /home/david/.rvm/gems/ruby-2.1.1/gems/faye-1.0.1/lib/faye/protocol/client.rb:357:in `transport_send'
    from /home/david/.rvm/gems/ruby-2.1.1/gems/faye-1.0.1/lib/faye/protocol/client.rb:346:in `block in send'
    from /home/david/.rvm/gems/ruby-2.1.1/gems/faye-1.0.1/lib/faye/protocol/extensible.rb:23:in `call'
    from /home/david/.rvm/gems/ruby-2.1.1/gems/faye-1.0.1/lib/faye/protocol/extensible.rb:23:in `pipe_through_extensions'
    from /home/david/.rvm/gems/ruby-2.1.1/gems/faye-1.0.1/lib/faye/protocol/client.rb:343:in `send'
    from /home/david/.rvm/gems/ruby-2.1.1/gems/faye-1.0.1/lib/faye/protocol/client.rb:92:in `handshake'
    from /home/david/.rvm/gems/ruby-2.1.1/gems/faye-1.0.1/lib/faye/protocol/client.rb:131:in `connect'
    from /home/david/.rvm/gems/ruby-2.1.1/gems/faye-1.0.1/lib/faye/protocol/client.rb:270:in `publish'
    from /home/david/.rvm/gems/ruby-2.1.1/gems/faye-rails-2.0.0/lib/faye-rails/controller.rb:45:in `publish'
... 27 levels...
    from /home/david/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.1/lib/active_record/connection_adapters/abstract/database_statements.rb:219:in `within_new_transaction'
    from /home/david/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.1/lib/active_record/connection_adapters/abstract/database_statements.rb:211:in `transaction'
    from /home/david/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.1/lib/active_record/transactions.rb:208:in `transaction'
    from /home/david/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.1/lib/active_record/transactions.rb:326:in `with_transaction_returning_status'
    from /home/david/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.1/lib/active_record/transactions.rb:268:in `block in save'
    from /home/david/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.1/lib/active_record/transactions.rb:283:in `rollback_active_record_state!'
    from /home/david/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.1/lib/active_record/transactions.rb:267:in `save'
    from /home/david/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.1/lib/active_record/persistence.rb:34:in `create'
    from (irb):1
    from /home/david/.rvm/gems/ruby-2.1.1/gems/railties-4.1.1/lib/rails/commands/console.rb:90:in `start'
    from /home/david/.rvm/gems/ruby-2.1.1/gems/railties-4.1.1/lib/rails/commands/console.rb:9:in `start'
    from /home/david/.rvm/gems/ruby-2.1.1/gems/railties-4.1.1/lib/rails/commands/commands_tasks.rb:69:in `console'
    from /home/david/.rvm/gems/ruby-2.1.1/gems/railties-4.1.1/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
    from /home/david/.rvm/gems/ruby-2.1.1/gems/railties-4.1.1/lib/rails/commands.rb:17:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'

注意RuntimeError: eventmachine not initialized: evma_install_oneshot_timer

错误在那个和ArgumentError: wrong number of arguments (0 for 2..3)之间交替出现。

经过一段时间的挖掘,该错误仅在我尝试保存观察到的模型时发生。

任何帮助将不胜感激。

【问题讨论】:

您的控制器是否扩展FayeRails::Controller?你的FayeRails::Middleware 正在使用中吗? 嗨。你能检查一下控制台EM.reactor_running? 【参考方案1】:

我用这个代码和 Faye 一起使用

$faye = Faye::Client.new('http://localhost/faye')
class Wrapper
    def self.publish(text)
       run_event_machine
        $faye.publish("/base", text)
    end

    def self.run_event_machine
        Thread.new  EM.run  unless EM.reactor_running?
        Thread.pass until EM.reactor_running?
    end
end

以及何时需要发送数据:

Wrapper.publush("the truth is out there")

【讨论】:

【参考方案2】:

您正在尝试从 rails 控制台调用 Faye(和 EventMachine)。 Rails 控制台不运行 EventMachine,只有 webserver 运行。如果您需要它工作,您应该在 rails 控制台或 rake 任务中使用:Faye.ensure_reactor_running!

【讨论】:

以上是关于Rails eventmachine 未初始化 evma_install_oneshot_timer的主要内容,如果未能解决你的问题,请参考以下文章

无法在 Rails 控制台中加载 Ruby EventMachine - 没有要加载的文件

Ruby on Rails Eventmachine 段错误

Rails - 无法运行应用程序:无法加载 EventMachine C 扩展;

安装eventmachine(1.0.3)时出错[重复]

无法加载 EventMachine C 扩展;使用纯红宝石反应器

Rails:NameError:未初始化的常量