Search code examples
ruby-on-railspostgresqlnginxunicorndokku

uninitialized constant ActiveRecord (NameError) wiith Dokku + nginx + unicorn


I run a Rails App on digitalocean using Dokku + nginx + PostgreSQL. The deployed App is running using WEBrick as a default server and I want to migrate to unicorn.

Gemfile

group :production do
  gem 'pg'
  gem 'unicorn'
end

I have used the Heroku configs for Unicorn

Procfile

web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb

config/unicorn.rb

worker_processes Integer(ENV["WEB_CONCURRENCY"] || 3)
timeout 15
preload_app true

before_fork do |server, worker|
  Signal.trap 'TERM' do
    puts 'Unicorn master intercepting TERM and sending myself QUIT instead'
    Process.kill 'QUIT', Process.pid
  end

  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.connection.disconnect!
end

after_fork do |server, worker|
  Signal.trap 'TERM' do
    puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to send QUIT'
  end

  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.establish_connection
end

And this is how the log look like

root@oktobTest:~# dokku logs oktob
I, [2015-04-25T18:30:29.195191 #13]  INFO -- : Refreshing Gem list
config.ru:4:in `block in <main>': uninitialized constant ActiveRecord (NameError)
    from /app/vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/builder.rb:55:in `instance_eval'
    from /app/vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/builder.rb:55:in `initialize'
    from config.ru:1:in `new'
    from config.ru:1:in `<main>'
    from /app/vendor/bundle/ruby/2.1.0/gems/unicorn-4.8.3/lib/unicorn.rb:48:in `eval'
    from /app/vendor/bundle/ruby/2.1.0/gems/unicorn-4.8.3/lib/unicorn.rb:48:in `block in builder'
    from /app/vendor/bundle/ruby/2.1.0/gems/unicorn-4.8.3/lib/unicorn/http_server.rb:764:in `call'
    from /app/vendor/bundle/ruby/2.1.0/gems/unicorn-4.8.3/lib/unicorn/http_server.rb:764:in `build_app!'
    from /app/vendor/bundle/ruby/2.1.0/gems/unicorn-4.8.3/lib/unicorn/http_server.rb:137:in `start'
    from /app/vendor/bundle/ruby/2.1.0/gems/unicorn-4.8.3/bin/unicorn:126:in `<top (required)>'
    from /app/vendor/bundle/ruby/2.1.0/bin/unicorn:23:in `load'
    from /app/vendor/bundle/ruby/2.1.0/bin/unicorn:23:in `<main>'

When I run the docker ps command I only see the PostgreSQL process and there is no process for the rails server.

root@oktobTest:~# docker ps
CONTAINER ID        IMAGE                     COMMAND                CREATED             STATUS              PORTS                     NAMES
fbdbb516b19c        postgresql/oktob:latest   "/usr/bin/start_pgsq   2 hours ago         Up 2 hours          0.0.0.0:49153->5432/tcp   elegant_ardinghelli

Solution

  • I asked the question on the dokku Github page and got an answer

    So adding active_record gem to the config/unicorn.rb would solve the issue as:

    require 'rubygems'
    require 'active_record'
    
    worker_processes Integer(ENV["WEB_CONCURRENCY"] || 3)
    timeout 15
    preload_app true
    
    before_fork do |server, worker|
      Signal.trap 'TERM' do
        puts 'Unicorn master intercepting TERM and sending myself QUIT instead'
        Process.kill 'QUIT', Process.pid
      end
    
      defined?(ActiveRecord::Base) and
        ActiveRecord::Base.connection.disconnect!
    end
    
    after_fork do |server, worker|
      Signal.trap 'TERM' do
        puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to send QUIT'
      end
    
      defined?(ActiveRecord::Base) and
        ActiveRecord::Base.establish_connection
    end
    

    And this is the result

    Using docker ps

    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4a43ac670998 dokku/oktob:latest "/start web" 12 minutes ago Up 12 minutes grave_yonath fbdbb516b19c postgresql/oktob:latest "/usr/bin/start_pgsq 6 days ago Up 6 days 0.0.0.0:49153->5432/tcp elegant_ardinghelli

    Using htop

    enter image description here