nbudin / heroku_external_db

Makes it easy to connect to external databases from a Heroku app
MIT License
40 stars 5 forks source link

Can't migrate to unicorn on heroku #11

Closed fkchang closed 11 years ago

fkchang commented 11 years ago

We have a Rails 3.2 app we are trying to migrate to unicorn on heroku. The heroku recommended config bombs out because it's trying to hit DATABASE_URL instead of EXTERNAL_DATABASE_URL. So per the advice from heroku (have a ticket there for this issue, they recommended we file here because we're using this gem) I set DATABASE_URL to match EXTERNAL_DATABASE_URL. That failed with a

2013-07-09T17:29:18.847565+00:00 app[web.1]: Mysql2::Error (Access denied for user 'root'@'ip-10-119-51-78.ec2.internal' (using password: YES)):

which implies to me that it wasn't getting all the authentication issue, so I set matching DATABASE_* to these fields we have set

EXTERNAL_DATABASE_CA: staging/ca-cert.pem EXTERNAL_DATABASE_CERT: staging/client-cert.pem EXTERNAL_DATABASE_KEY: staging/client-key.pem

But that doesn't apparently work. Heroku redirected me here after I told them that.

I'm thinking we can't be the only people running an external mysql DB on heroku on unicorn and there's got to be a config. It apparently runs on our staging instance w/the ActiveRecord lines commented out, but I'm hesitant to diverge from Heroku recommended config on production w/o knowing everything.

our unicorn.rb below with the ActiveRecord calls commented out


# config/unicorn.rb

worker_processes Integer(ENV['WEB_CONCURRENCY'] || 3)

# per Diego's suggestion, not necessary for alaa but maybe as a basis
# for all of our apps.  Heroku recommendation is 15
timeout Integer(ENV['WEB_TIMEOUT'] || 30)
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 sent QUIT'
  end

   # defined?(ActiveRecord::Base) and
   # ActiveRecord::Base.establish_connection
end
nbudin commented 11 years ago

In the case of Cedar (I assume you're on Cedar because you're using Unicorn), you can actually use a DATABASE_URL like this:

mysql2://user:pass@host/database?sslca=staging/ca-cert.pem&sslkey=staging/client-key.pem&sslcert=staging/client-cert.pem

In such a case, heroku_external_db shouldn't even be necessary. It is necessary on Bamboo because Bamboo's database.yml generator will ignore the extra parameters at the end of the URL, but on Cedar it will add anything you put as a URL parameter into the config.

fkchang commented 11 years ago

I think that did it, I had to change pathnames a bit. Does this mean that if I have the heroku_external_db gem, I'm connecting 2x - it worked commenting out those lines, and now w/the fixed DATABASE_URL, the unicorns spin up?

Thanks

nbudin commented 11 years ago

Yeah, I think so. With Cedar you shouldn't really need heroku_external_db anymore. I'm planning to investigate that further and update the README to reflect that fact.