undefined method `close_connection_after_writing' - ruby-on-rails

I have project on rails 3 with multiplayer using Faye.
The error
block in close': undefined methodclose_connection_after_writing' for nil:NilClass (NoMethodError)
from /Users/ostap/.rvm/gems/ruby-1.9.3-p125#chats/gems/faye-websocket-0.4.5/lib/faye/websocket/api.rb:89:in `call'
from /Users/ostap/.rvm/gems/ruby-1.9.3-p125#chats/gems/faye-websocket-0.4.5/lib/faye/websocket/api.rb:89:in `close'
from /Users/ostap/.rvm/gems/ruby-1.9.3-p125#chats/gems/faye-websocket-0.4.5/lib/faye/websocket.rb:198:in `fail'
from /Users/ostap/.rvm/gems/ruby-1.9.3-p125#global/gems/thin-1.3.1/lib/thin/connection.rb:155:in `unbind'
from /Users/ostap/.rvm/gems/ruby-1.9.3-p125#global/gems/eventmachine-0.12.10/lib/eventmachine.rb:1417:in `event_callback'
from /Users/ostap/.rvm/gems/ruby-1.9.3-p125#global/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in `run_machine'
from /Users/ostap/.rvm/gems/ruby-1.9.3-p125#global/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in `run'
from /Users/ostap/.rvm/gems/ruby-1.9.3-p125#global/gems/thin-1.3.1/lib/thin/backends/base.rb:61:in `start'
from /Users/ostap/.rvm/gems/ruby-1.9.3-p125#global/gems/thin-1.3.1/lib/thin/server.rb:159:in `start'
from /Users/ostap/.rvm/gems/ruby-1.9.3-p125#global/gems/rack-1.4.1/lib/rack/handler/thin.rb:13:in `run'
from /Users/ostap/.rvm/gems/ruby-1.9.3-p125#global/gems/rack-1.4.1/lib/rack/server.rb:265:in `start'
from /Users/ostap/.rvm/gems/ruby-1.9.3-p125#global/gems/rack-1.4.1/lib/rack/server.rb:137:in `start'
from /Users/ostap/.rvm/gems/ruby-1.9.3-p125#global/gems/rack-1.4.1/bin/rackup:4:in `<top (required)>'
from /Users/ostap/.rvm/gems/ruby-1.9.3-p125#chats/bin/rackup:19:in `load'
from /Users/ostap/.rvm/gems/ruby-1.9.3-p125#chats/bin/rackup:19:in `<main>'
after my faye server startup with command
rackup faye.ru -s thin -E production
What can I do with such error?? What couses it? How can I handle it?

i found on this page (it's in russian) http://habrahabr.ru/sandbox/45416/
add this to your faye.ru file
Faye::WebSocket.load_adapter('thin')
this made my solution
i hope it helps an thanks for the advice

I also ran into this issue after upgrading faye.
To build on #tingel2k's answer, you need to insert this:
Faye::WebSocket.load_adapter('thin')
into faye.ru before the app is started, eg:
run Faye::RackAdapter.new(...)

do not use rackup to start faye
instead, use thin start -R faye.ru -e production
this is my faye.ru file
# encoding: utf-8
require 'faye'
require File.expand_path('../config/initializers/faye_token.rb', __FILE__)
Faye::WebSocket.load_adapter('thin')
class ServerAuth
def incoming(message, callback)
if message['channel'] !~ %r{^/meta/}
if message['ext']['auth_token'] != FAYE_TOKEN
message['error'] = 'Token de autenticacao invalido'
end
end
callback.call(message)
end
end
bayeux = Faye::RackAdapter.new(:mount => '/faye', :timeout => 25, :extensions => [ServerAuth.new])
run bayeux

This is EventMachine error. This method receiver is a connection object, so i think that this error means that connection was terminated in not proper way or wasn't created.

Related

Access my model from foreverb daemon

I have the following code on a foreverb daemon to access my Availability model. but I always get the following error:
uninitialized constant Availability
Here is my daemon:
#!/usr/bin/ruby
require 'rubygems' unless defined?(Gem)
require 'forever'
require 'mongoid'
Forever.run do
every 10.seconds do
#classes = Availability.where(:availability_date.gt => Time.now.utc + 1.hours).to_a
puts #classes.count
end
end
Thanks for your help.
Note: I'm using rails3 and mongoid.
UPDATED CODE
#!/usr/bin/env ruby
require 'rubygems' unless defined?(Gem)
require 'forever'
require 'mongoid'
require File.expand_path('../config/environment', __FILE__)
Forever.run do
every 10.seconds do
#classes = Availability.where(:availability_date.gt => Time.now.utc + 1.hours).to_a
puts #classes.count
end
end
Now I'm getting the following error:
user/classes_notification.rb; tail -f -n 150 user/sample.log;
/Users/jeanosorio/.rvm/rubies/ruby-1.9.3-p484/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:126:in `require': cannot load such file -- /Users/jeanosorio/repos/blabloo/script/user/config/environment (LoadError)
from /Users/jeanosorio/.rvm/rubies/ruby-1.9.3-p484/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:126:in `require'
from user/classes_notification.rb:6:in `<main>'
UPDATE
UPDATE PATH
#!/usr/bin/env ruby
require 'rubygems' unless defined?(Gem)
require 'forever'
require 'mongoid'
# Load Rails
#ENV['RAILS_ENV'] = ARGV[0] || 'development'
require File.expand_path('../../../config/environment', __FILE__)
Forever.run do
every 10.seconds do
#classes = Availability.where(:availability_date.gt => Time.now.utc + 1.hours).to_a
puts #classes.count
end
end
Now I'm getting this eror:
script/user/classes_notification.rb; tail -f -n 150 script/user/sample.log;
WARN: Unresolved specs during Gem::Specification.reset:
thor (>= 0.15.0)
tzinfo (~> 0.3.22)
WARN: Clearing out unresolved specs.
Please report a bug if this causes problems.
/Users/jeanosorio/.rvm/gems/ruby-1.9.3-p484#global/gems/bundler-1.5.2/lib/bundler/runtime.rb:34:in `block in setup': You have already activated multi_json 1.10.1, but your Gemfile requires multi_json 1.8.2. Prepending `bundle exec` to your command may solve this. (Gem::LoadError)
from /Users/jeanosorio/.rvm/gems/ruby-1.9.3-p484#global/gems/bundler-1.5.2/lib/bundler/runtime.rb:19:in `setup'
from /Users/jeanosorio/.rvm/gems/ruby-1.9.3-p484#global/gems/bundler-1.5.2/lib/bundler.rb:119:in `setup'
from /Users/jeanosorio/.rvm/gems/ruby-1.9.3-p484#global/gems/bundler-1.5.2/lib/bundler/setup.rb:7:in `<top (required)>'
from /Users/jeanosorio/.rvm/rubies/ruby-1.9.3-p484/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:135:in `require'
from /Users/jeanosorio/.rvm/rubies/ruby-1.9.3-p484/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:135:in `rescue in require'
from /Users/jeanosorio/.rvm/rubies/ruby-1.9.3-p484/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:144:in `require'
from /Users/jeanosorio/repos/blabloo/config/boot.rb:6:in `<top (required)>'
from /Users/jeanosorio/.rvm/rubies/ruby-1.9.3-p484/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:126:in `require'
from /Users/jeanosorio/.rvm/rubies/ruby-1.9.3-p484/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:126:in `require'
from /Users/jeanosorio/repos/blabloo/config/application.rb:1:in `<top (required)>'
from /Users/jeanosorio/.rvm/rubies/ruby-1.9.3-p484/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:126:in `require'
from /Users/jeanosorio/.rvm/rubies/ruby-1.9.3-p484/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:126:in `require'
from /Users/jeanosorio/repos/blabloo/config/environment.rb:2:in `<top (required)>'
from /Users/jeanosorio/.rvm/rubies/ruby-1.9.3-p484/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:126:in `require'
from /Users/jeanosorio/.rvm/rubies/ruby-1.9.3-p484/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:126:in `require'
from script/user/classes_notification.rb:9:in `<main>'
This is very easy load Rails env just add to top of your script:
#!/usr/bin/env ruby
require File.expand_path('../config/environment', __FILE__)
Worked this simple 3 lines script in Rails.root dir.
Another examples

Sidekiq Install - uninitialized constant ActiveRecord::Base (NameError)

I am trying to integrate Sidekiq with my Rails Mongoid app. I have been following the steps as outlined Here
I also have redis & mongodb running locally. However after I run bundle then when I try to start my server I get this error....
c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/sidekiq-2.17.7/lib/sidekiq/rails.rb:4:in `hook_rails!': uninitialized constant ActiveRecord::Base (NameError)
from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/sidekiq-2.17.7/lib/sidekiq/rails.rb:16:in `block in <class:Rails>'
from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/initializable.rb:30:in `instance_exec'
from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/initializable.rb:30:in `run'
from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/initializable.rb:55:in `block in run_initializers'
from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/initializable.rb:54:in `each'
from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/initializable.rb:54:in `run_initializers'
from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/application.rb:136:in `initialize!'
from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/railtie/configurable.rb:30:in `method_missing'
from C:/Users/example/Documents/Workspace/app/config/environment.rb:5:in `<top (required)>'
My environment.rb file is as follows...
# Load the rails application
require File.expand_path('../application', __FILE__)
# Initialize the rails application
App::Application.initialize!
I previously had resque with redis working with mongoid in this app, so its a sidekiq issue. I don't have a database.yml file since I am using mongoid. I also added the steps for kiqstand but I am getting the same error. Unsure how to resolve?
Edit: I'm 90% sure its because Sidekiq is looking for ActiveRecord, however I removed ActiveRecord cause I'm using Mongo instead. My application.rb has...
# remove activerecord and run off of mongo only
#require 'rails/all'
#http://stackoverflow.com/a/9327651/1026266
%w(
action_controller
action_mailer
active_resource
rails/test_unit
sprockets
).each do |framework|
begin
require "#{framework}/railtie"
rescue LoadError
end
end
Now I just need to figure out how to tell sidekiq to not look for it?
Lack of ActiveRecord was the issue, looks like there was a pull request - https://github.com/mperham/sidekiq/pull/1090
But that was rejected, so I just had to add ActiveRecord to my app and add a dummy database.yml file.

Rails application not starting

I have a new application that is giving me errors when I do
rails server
Here is the error:
/home/agenadinik/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.7/lib/rails/script_rails_loader.rb:11: warning: Insecure world writable dir /home/agenadinik in PATH, mode 040777
/home/agenadinik/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.12/lib/bundler/runtime.rb:136: warning: Insecure world writable dir /home/agenadinik in PATH, mode 040777
=> Booting WEBrick
=> Rails 3.0.7 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
Exiting
/home/agenadinik/.rvm/gems/ruby-1.9.2-p180/gems/rack-1.2.2/lib/rack/builder.rb:35:in `eval': /home/agenadinik/workspace/udfr/config.ru:6: syntax error, unexpected keyword_end, expecting ')' (SyntaxError)
from /home/agenadinik/.rvm/gems/ruby-1.9.2-p180/gems/rack-1.2.2/lib/rack/builder.rb:35:in `parse_file'
from /home/agenadinik/.rvm/gems/ruby-1.9.2-p180/gems/rack-1.2.2/lib/rack/server.rb:162:in `app'
from /home/agenadinik/.rvm/gems/ruby-1.9.2-p180/gems/rack-1.2.2/lib/rack/server.rb:248:in `wrapped_app'
from /home/agenadinik/.rvm/gems/ruby-1.9.2-p180/gems/rack-1.2.2/lib/rack/server.rb:213:in `start'
from /home/agenadinik/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.7/lib/rails/commands/server.rb:65:in `start'
from /home/agenadinik/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.7/lib/rails/commands.rb:30:in `block in <top (required)>'
from /home/agenadinik/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.7/lib/rails/commands.rb:27:in `tap'
from /home/agenadinik/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.7/lib/rails/commands.rb:27:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
It seems to be complaining about my config.ru file. Here it is:
# This file is used by Rack-based servers to start the application.
require ::File.expand_path('../config/environment', __FILE__)
run Udfr::Application
Here is my environment.rb file:
# Load the rails application
require File.join(File.dirname(__FILE__), 'boot')
require File.expand_path('../application', __FILE__)
if RUBY_PLATFORM =~ /java/
require 'rubygems'
RAILS_CONNECTION_ADAPTERS = %w(jdbc)
end
# Initialize the rails application
Rails::Initializer.run do |config|
Udfr::Application.initialize!
I changed that file according to these instructions:
http://www.digitalsanctum.com/2007/07/24/jruby-deploying-a-rails-application-on-tomcat/
Any idea what the problem is?
The instructions you followed are for Rails 2 and will not work with your Rails 3 app.
The updated instructions for getting Ruby on Rails to work on jruby are to edit your gemfile and place your gem requirements there.
In Gemfile:
if defined?(JRUBY_VERSION)
gem 'activerecord-jdbc-adapter'
end
Then run bundle.

Can't run rails server after installing custom-err-msg

After I install the custom-err-msg plugin, I get an error when I try to run rails server:
$ rails server
=> Booting WEBrick
=> Rails 3.0.3 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
Exiting
/home/jason/rails/snip/vendor/plugins/custom-err-msg/init.rb:3: uninitialized constant ActiveRecord::Errors (NameError)
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/plugin.rb:81
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/initializable.rb:25:in `instance_exec'
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/initializable.rb:25:in `run'
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/initializable.rb:50:in `run_initializers'
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/initializable.rb:49:in `each'
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/initializable.rb:49:in `run_initializers'
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/application.rb:134:in `initialize!'
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/application.rb:77:in `send'
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.3/lib/rails/application.rb:77:in `method_missing'
from /home/jason/rails/snip/config/environment.rb:5
from /home/jason/rails/snip/config.ru:3:in `require'
from /home/jason/rails/snip/config.ru:3
from /usr/lib/ruby/gems/1.8/gems/rack-1.2.1/lib/rack/builder.rb:46:in `instance_eval'
from /usr/lib/ruby/gems/1.8/gems/rack-1.2.1/lib/rack/builder.rb:46:in `initialize'
from /home/jason/rails/snip/config.ru:1:in `new'
from /home/jason/rails/snip/config.ru:1
Any idea why this is happening?
Here's an updated version that will work with Rails 3.
https://github.com/seansawyer/custom-err-msg
To fix the undefined method keys for nil:NilClass error, a method in lib/custom_error_message.rb needs to be changed to this:
def starts_with_humanized_column_followed_by_circumflex?(message)
keys.any? do |column|
humanized = #base.class.human_attribute_name column.to_s.split('.').last.to_s
message.match(/^#{humanized} \^/)
end
end
Someone, please send him a pull request!
This plugin is not compatible with Rails 3. I would recommend finding a fork that is or forking it and updating yourself.

Rack middleware deadlock

I include this simple Rack Middleware in a Rails application:
class Hello
def initialize(app)
#app = app
end
def call(env)
[200, {"Content-Type" => "text/html"}, "Hello"]
end
end
Plug it in inside environment.rb:
...
Dir.glob("#{RAILS_ROOT}/lib/rack_middleware/*.rb").each do |file|
require file
end
Rails::Initializer.run do |config|
config.middleware.use Hello
...
I'm using Rails 2.3.5, Webrick 1.3.1, ruby 1.8.7
When the application is started in production mode, everything works as expected - every request is intercepted by the Hello middleware, and "Hello" is returned. However, when run in development mode, the very first request works returning "Hello", but the next request hangs.
Interrupting webrick while it is in the hung state yields this:
^C[2010-03-24 14:31:39] INFO going to shutdown ...
deadlock 0xb6efbbc0: sleep:- - /usr/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/reloader.rb:31
deadlock 0xb7d1b1b0: sleep:J(0xb6efbbc0) (main) - /usr/lib/ruby/1.8/webrick/server.rb:113
Exiting
/usr/lib/ruby/1.8/webrick/server.rb:113:in `join': Thread(0xb7d1b1b0): deadlock (fatal)
from /usr/lib/ruby/1.8/webrick/server.rb:113:in `start'
from /usr/lib/ruby/1.8/webrick/server.rb:113:in `each'
from /usr/lib/ruby/1.8/webrick/server.rb:113:in `start'
from /usr/lib/ruby/1.8/webrick/server.rb:23:in `start'
from /usr/lib/ruby/1.8/webrick/server.rb:82:in `start'
from /usr/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/handler/webrick.rb:14:in `run'
from /usr/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/commands/server.rb:111
from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
from script/server:3
Something to do with the class reloader in development mode. There is also mention of deadlock in the exception.
Any ideas what might be causing this? Any recommendations as to the best approach to debug this?
UPDATE
$ script/console
Loading development environment (Rails 2.3.5)
>> app.get '/'
=> 200
>> app.get '/'
ThreadError: stopping only thread
note: use sleep to stop forever
from /usr/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/reloader.rb:31:in `lock'
from /usr/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/reloader.rb:31:in `run'
from /usr/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/dispatcher.rb:108:in `call'
from /usr/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/lint.rb:47:in `_call'
from /usr/lib/ruby/gems/1.8/gems/rack-1.0.1/lib/rack/lint.rb:35:in `call'
from /usr/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/integration.rb:316:in `process'
from /usr/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/integration.rb:197:in `get'
from (irb):2
Looks like it might be related to this issue:
https://rails.lighthouseapp.com/projects/8994/tickets/3153-actioncontrollerintegrationsession-broken-in-234
I've come up with a hack that will get me by the time being. It's on the ticket mentioned above.
https://rails.lighthouseapp.com/projects/8994/tickets/3153-actioncontrollerintegrationsession-broken-in-234

Resources