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
Related
I am encountering errors when I am using Ruby's File.exist? method in middleware. I have no idea why.
This is the background. I am improving some old webapp which hosts around 100GB of photos and growing. I need to replicate production environment on my computer, but I don't want to download all those files. It would be great if app could check if given file exists in my filesystem and either serve it or redirect to production server.
I thought I could do simple rack app as middleware:
require 'rack/utils'
class FetchMissingPicturesMiddleware
def initialize(app)
#app = app
end
def call(env)
if env["PATH_INFO"].starts_with? "/system/attachments/" && !File.exist?(Rails.root.join('public').join(env["PATH_INFO"][1..-1]))
[307, { "Location" => "http://production.example.com" + env["PATH_INFO"] }, ""]
else
#app.call(env)
end
end
end
However, following error is thrown (not always, for some pictures it just works):
[2012-11-26 14:28:12] ERROR ThreadError: thread 0x10aed55e8 tried to join itself
/Users/skale/.rvm/gems/ruby-1.8.7-p371/gems/actionpack-2.3.9/lib/action_controller/reloader.rb:31:in `lock'
/Users/skale/.rvm/gems/ruby-1.8.7-p371/gems/actionpack-2.3.9/lib/action_controller/reloader.rb:31:in `run'
/Users/skale/.rvm/gems/ruby-1.8.7-p371/gems/actionpack-2.3.9/lib/action_controller/dispatcher.rb:108:in `call'
/Users/skale/.rvm/gems/ruby-1.8.7-p371/gems/rails-2.3.9/lib/rails/rack/static.rb:31:in `call'
/Users/skale/.rvm/gems/ruby-1.8.7-p371/gems/rack-1.1.3/lib/rack/urlmap.rb:47:in `call'
/Users/skale/.rvm/gems/ruby-1.8.7-p371/gems/rack-1.1.3/lib/rack/urlmap.rb:41:in `each'
/Users/skale/.rvm/gems/ruby-1.8.7-p371/gems/rack-1.1.3/lib/rack/urlmap.rb:41:in `call'
/Users/skale/.rvm/gems/ruby-1.8.7-p371/gems/rails-2.3.9/lib/rails/rack/log_tailer.rb:17:in `call'
/Users/skale/.rvm/gems/ruby-1.8.7-p371/gems/rack-1.1.3/lib/rack/content_length.rb:13:in `call'
/Users/skale/.rvm/gems/ruby-1.8.7-p371/gems/rack-1.1.3/lib/rack/handler/webrick.rb:48:in `service'
/Users/skale/.rvm/rubies/ruby-1.8.7-p371/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
/Users/skale/.rvm/rubies/ruby-1.8.7-p371/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
/Users/skale/.rvm/rubies/ruby-1.8.7-p371/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
/Users/skale/.rvm/rubies/ruby-1.8.7-p371/lib/ruby/1.8/webrick/server.rb:162:in `start'
/Users/skale/.rvm/rubies/ruby-1.8.7-p371/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
/Users/skale/.rvm/rubies/ruby-1.8.7-p371/lib/ruby/1.8/webrick/server.rb:95:in `start'
/Users/skale/.rvm/rubies/ruby-1.8.7-p371/lib/ruby/1.8/webrick/server.rb:92:in `each'
/Users/skale/.rvm/rubies/ruby-1.8.7-p371/lib/ruby/1.8/webrick/server.rb:92:in `start'
/Users/skale/.rvm/rubies/ruby-1.8.7-p371/lib/ruby/1.8/webrick/server.rb:23:in `start'
/Users/skale/.rvm/rubies/ruby-1.8.7-p371/lib/ruby/1.8/webrick/server.rb:82:in `start'
/Users/skale/.rvm/gems/ruby-1.8.7-p371/gems/rack-1.1.3/lib/rack/handler/webrick.rb:14:in `run'
/Users/skale/.rvm/gems/ruby-1.8.7-p371/gems/rails-2.3.9/lib/commands/server.rb:111
script/server:6:in `require'
script/server:6
I am surprised with this issue because it's run inside mutex provided by Rack::Lock. Moving this middleware before Rack::Lock does not help. After removing File.exist? errors are gone. This is Ruby 1.8.7 with freshest (1.8.24) rubygems on Rails 2.3.9.
Thanks for your help.
Actually, the problem was bit different. The last element of the response array should be an array too.
wrong:
[307, { "Location" => "http://production.example.com" + env["PATH_INFO"] }, ""]
good:
[307, { "Location" => "http://production.example.com" + env["PATH_INFO"] }, []]
Whenever I attempt to create or update an environment that our app uses, I get the following:
NoMethodError (undefined method `base_url' for #<HashWithIndifferentAccess:0x107162f08>):
app/controllers/environments_controller.rb:64:in `check_base_url_for_https'
app/controllers/environments_controller.rb:56:in `update'
haml (3.0.22) lib/sass/plugin/rack.rb:41:in `call'
airbrake (3.0.4) lib/airbrake/rack.rb:27:in `call'
airbrake (3.0.4) lib/airbrake/user_informer.rb:12:in `call'
/Users/jasonbodak/.rvm/rubies/ree-1.8.7-2011.03/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
/Users/jasonbodak/.rvm/rubies/ree-1.8.7-2011.03/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
/Users/jasonbodak/.rvm/rubies/ree-1.8.7-2011.03/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
/Users/jasonbodak/.rvm/rubies/ree-1.8.7-2011.03/lib/ruby/1.8/webrick/server.rb:162:in `start'
/Users/jasonbodak/.rvm/rubies/ree-1.8.7-2011.03/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
/Users/jasonbodak/.rvm/rubies/ree-1.8.7-2011.03/lib/ruby/1.8/webrick/server.rb:95:in `start'
/Users/jasonbodak/.rvm/rubies/ree-1.8.7-2011.03/lib/ruby/1.8/webrick/server.rb:92:in `each'
/Users/jasonbodak/.rvm/rubies/ree-1.8.7-2011.03/lib/ruby/1.8/webrick/server.rb:92:in `start'
/Users/jasonbodak/.rvm/rubies/ree-1.8.7-2011.03/lib/ruby/1.8/webrick/server.rb:23:in `start'
/Users/jasonbodak/.rvm/rubies/ree-1.8.7-2011.03/lib/ruby/1.8/webrick/server.rb:82:in `start'
As I stated in the title, I recently upgraded to Rails 2.3.11. This error never occurred before. Here is the code in my environments_controller.rb that is being executed when the error occurs:
def check_base_url_for_https
#environment = params[:environment]
if /^https:\/\//i =~ #environment.base_url
#ajax_flash = "<ul class='notice'><li>The website you are trying to test is a secure site. If you are using self-signed SSL certificates please see our <a href='http://support.janova.us'>Support Site</a> and search for SSL for important tips on how to access your site.</li></ul>"
end
end
Does anyone know why this code (specifically the line if /^https:\/\//i =~ #environment.base_url no longer works in Rails 2.3.11?
I would like to add that the code in my app/models/environment.rb appears to be sound too:
def base_url
self[:base_url].try(:gsub, /\$/, '')
end
Does anyone see anything wrong with the code above?
I figured it out: params[:environment] was defined as a hash. Thank you, Frost, for forcing me to see this. Therefore, I changed the line #environment = params[:environment] to #environment = Environment.new(params[:environment]) and it worked.
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.
I'm trying to use the twitter gem in a rails 3 app but keep getting the following error:
TypeError: can't convert Pathname into String
Here's the code I'm trying to run:
class Tweet < ActiveRecord::Base
def self.test_tweet
Twitter.user_timeline("sferik").first.text
end
end
Here's the full error message:
TypeError: can't convert Pathname into String
from /home/shane/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/net/http.rb:658:in `initialize'
from /home/shane/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/net/http.rb:658:in `new'
from /home/shane/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/net/http.rb:658:in `connect'
from /home/shane/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/net/http.rb:637:in `do_start'
from /home/shane/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/net/http.rb:626:in `start'
from /home/shane/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/net/http.rb:1168:in `request'
from /home/shane/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/net/http.rb:888:in `get'
from /home/shane/.rvm/gems/ruby-1.9.2-p290#rails312/gems/faraday-0.7.6/lib/faraday/adapter/net_http.rb:59:in `call'
from /home/shane/.rvm/gems/ruby-1.9.2-p290#rails312/gems/faraday-0.7.6/lib/faraday/response.rb:8:in `call'
from /home/shane/.rvm/gems/ruby-1.9.2-p290#rails312/gems/faraday-0.7.6/lib/faraday/response.rb:8:in `call'
from /home/shane/.rvm/gems/ruby-1.9.2-p290#rails312/gems/faraday-0.7.6/lib/faraday/response.rb:8:in `call'
from /home/shane/.rvm/gems/ruby-1.9.2-p290#rails312/gems/faraday-0.7.6/lib/faraday/request/url_encoded.rb:14:in `call'
from /home/shane/.rvm/gems/ruby-1.9.2-p290#rails312/gems/faraday-0.7.6/lib/faraday/request/multipart.rb:13:in `call'
from /home/shane/.rvm/gems/ruby-1.9.2-p290#rails312/gems/twitter-2.1.1/lib/twitter/request/multipart_with_file.rb:17:in `call'
from /home/shane/.rvm/gems/ruby-1.9.2-p290#rails312/gems/twitter-2.1.1/lib/twitter/request/phoenix.rb:13:in `call'
from /home/shane/.rvm/gems/ruby-1.9.2-p290#rails312/gems/faraday-0.7.6/lib/faraday/connection.rb:210:in `run_request'
from /home/shane/.rvm/gems/ruby-1.9.2-p290#rails312/gems/twitter-2.1.1/lib/twitter/request.rb:23:in `request'
from /home/shane/.rvm/gems/ruby-1.9.2-p290#rails312/gems/twitter-2.1.1/lib/twitter/request.rb:11:in `get'
from /home/shane/.rvm/gems/ruby-1.9.2-p290#rails312/gems/twitter-2.1.1/lib/twitter/client/timelines.rb:208:in `user_timeline'
from /home/shane/.rvm/gems/ruby-1.9.2-p290#rails312/gems/twitter-2.1.1/lib/twitter.rb:17:in `method_missing'
from /home/shane/projects/bv_data/app/models/tweet.rb:5:in `test_tweet'
from (irb):1
from /home/shane/.rvm/gems/ruby-1.9.2-p290#rails312/gems/railties-3.1.2/lib/rails/commands/console.rb:45:in `start'
from /home/shane/.rvm/gems/ruby-1.9.2-p290#rails312/gems/railties-3.1.2/lib/rails/commands/console.rb:8:in `start'
from /home/shane/.rvm/gems/ruby-1.9.2-p290#rails312/gems/railties-3.1.2/lib/rails/commands.rb:40:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
I get this error even when running the same code from the console:
Twitter.user_timeline("sferik").first.text
I created a blank test app with the same version of rails and the twitter gem and it runs no problem so I'm not sure what the problem is.
As I had this working before in the app I went back through my commits until I could find a point were it was working and what I did to break it.
So in case anyone happens to be in the same position what I had added was a fix_ssl.rb file in config/initializers which was to address a previous SSL connection issue. Removing this file allowed the Twitter gem to work correctly again.
config/initializers/fix_ssl.rb
require 'open-uri'
require 'net/https'
module Net
class HTTP
alias_method :original_use_ssl=, :use_ssl=
def use_ssl=(flag)
self.ca_file = Rails.root.join('lib/ca-bundle.crt')
self.verify_mode = OpenSSL::SSL::VERIFY_PEER
self.original_use_ssl = flag
end
end
end
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.