Rails eventmachine not initialized evma_install_oneshot_timer
faye, redis, ruby, ruby-on-rails, ruby-on-rails-4
Solution
I use this code with 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
and when needs to send data just:
Wrapper.publush("the truth is out there")
Problem
I'm using `Rails 4.1.1`, `Thin 1.6.2`, `Redis 2.8.9` (with `Hiredis` driver), and `faye-rails` gem. I use `faye-rails` to subscribe to changes on a few models, and I use `Redis` for other (unrelated) stuff. When I tried to call `create` on a `faye-observed` model (let's say Apple), it throws an error like this: ``` 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>' ``` Notice the `RuntimeError: eventmachine not initialized: evma_install_oneshot_timer`. The error alternates between that, and `ArgumentError: wrong number of arguments (0 for 2..3)`. After some time digging, the error only occurs when I'm trying to save an observed Model. Any help would be gladly appreciated.