Hi all,
I''m fighting with the after_fork hook and my sinatra application. The
Sinatra app is using active_record,
In my unciron.rb file I''m using
preload_app true
after_fork do |server, worker|
? defined?(ActiveRecord::Base) and
??? ActiveRecord::Base.establish_connection
End
but I always get the error:
ERROR -- : ActiveRecord::AdapterNotSpecified (ActiveRecord::AdapterNotSpecified)
I''m using unicorn (4.3.1), sinatra (1.3.2), activerecord (3.2.6), ruby
1.9.3p194
The app (test.rb):
require ''rubygems''
require ''sinatra''
require ''active_record''
ActiveRecord::Base.establish_connection(
? :adapter => ''sqlite3'',
? :database => ''/tmp/dbfile''
)
class User < ActiveRecord::Base
end
get ''/'' do
? @users = User.all
? "This is a Sinatra test. We have #{@users.count} users."
end
The config.ru
require ''./test''
run Sinatra::Application
The config/unicorn.rb
@dir = ''/home/frankr/tmp/sinatra/''
pidfile = " #{@dir}log/unicorn.pid"
worker_processes 3
preload_app true
GC.respond_to?(:copy_on_write_friendly=) and GC.copy_on_write_friendly = true
timeout 30
listen ''/tmp/sinatra.socket'', :backlog => 64
pid pidfile
stderr_path "#{@dir}log/unicorn.stderr.log"
stdout_path "#{@dir}log/unicorn.stdout.log"
before_fork do |server, worker|
? defined?(ActiveRecord::Base) and
??? ActiveRecord::Base.connection.disconnect!
? end
end
after_fork do |server, worker|
? defined?(ActiveRecord::Base) and
??? ActiveRecord::Base.establish_connection
end
when I start? unicorn
??? unicorn -c config/unicorn.rb -E production -D -l 0.0.0.0:3001
the unicorn.stderr.log file is growing and growing and repeating the same error
message:
.
E, [2012-08-31T07:18:39.232254 #29793] ERROR -- :
ActiveRecord::AdapterNotSpecified (ActiveRecord::AdapterNotSpecified)
/home/frankr/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activerecord-3.2.6/lib/active_record/connection_adapters/abstract/connection_specification.rb:22:in
`spec''
/home/frankr/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activerecord-3.2.6/lib/active_record/connection_adapters/abstract/connection_specification.rb:127:in
`establish_connection''
config/unicorn.rb:54:in `block in reload''
/home/frankr/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/unicorn-4.3.1/lib/unicorn/http_server.rb:565:in
`call''
/home/frankr/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/unicorn-4.3.1/lib/unicorn/http_server.rb:565:in
`init_worker_process''
/home/frankr/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/unicorn-4.3.1/lib/unicorn/http_server.rb:589:in
`worker_loop''
/home/frankr/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/unicorn-4.3.1/lib/unicorn/http_server.rb:487:in
`spawn_missing_workers''
/home/frankr/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/unicorn-4.3.1/lib/unicorn/http_server.rb:137:in
`start''
/home/frankr/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/unicorn-4.3.1/bin/unicorn:121:in
`<top (required)>''
/home/frankr/.rbenv/versions/1.9.3-p194/bin/unicorn:23:in `load''
/home/frankr/.rbenv/versions/1.9.3-p194/bin/unicorn:23:in
`<main>''
I, [2012-08-31T07:18:39.232514 #29790]? INFO -- : master process ready
E, [2012-08-31T07:18:39.232758 #29796] ERROR -- :
ActiveRecord::AdapterNotSpecified (ActiveRecord::AdapterNotSpecified)
/home/frankr/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activerecord-3.2.6/lib/active_record/connection_adapters/abstract/connection_specification.rb:22:in
`spec''
/home/frankr/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activerecord-3.2.6/lib/active_record/connection_adapters/abstract/connection_specification.rb:127:in
`establish_connection''
config/unicorn.rb:54:in `block in reload''
/home/frankr/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/unicorn-4.3.1/lib/unicorn/http_server.rb:565:in
`call''
/home/frankr/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/unicorn-4.3.1/lib/unicorn/http_server.rb:565:in
`init_worker_process''
/home/frankr/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/unicorn-4.3.1/lib/unicorn/http_server.rb:589:in
`worker_loop''
/home/frankr/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/unicorn-4.3.1/lib/unicorn/http_server.rb:487:in
`spawn_missing_workers''
/home/frankr/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/unicorn-4.3.1/lib/unicorn/http_server.rb:137:in
`start''
/home/frankr/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/unicorn-4.3.1/bin/unicorn:121:in
`<top (required)>''
/home/frankr/.rbenv/versions/1.9.3-p194/bin/unicorn:23:in `load''
/home/frankr/.rbenv/versions/1.9.3-p194/bin/unicorn:23:in
`<main>''
E, [2012-08-31T07:18:39.232976 #29790] ERROR -- : reaped #<Process::Status:
pid 29793 exit 1> worker=0
.
I get the same error, when using different database adapter. On the other hand
everything is working fine, when repeating the database configuration in
after_fork hook, or completely remove database configuration from Sinatra app
into after_fork hook. But this is not a solution for me. First I don''t
like to double code, second I need the database configuration in the Sinatra app
for running tests .
My question is what am I doing wrong? All documentation says that
??defined?(ActiveRecord::Base) and
??? ActiveRecord::Base.establish_connection
? end
should work.
Regards,
Frank