Skip to content

Deploy and Monitoring

JP Barbosa edited this page Jul 23, 2015 · 4 revisions

Deploy and Monitoring

Change database to Postgres replacing sqlite3 with pg
nano Gemfile
# Use Postgres as the database for Active Record
gem 'pg'
bundle install
Update database config
nano config/database.yml
default: &default
  adapter: postgresql
  pool: 5
  timeout: 5000
development:
  <<: *default
  database: rails_apz_dev
test:
  <<: *default
  database: rails_apz_test
production:
  <<: *default
  database: rails_apz_prod  
Setup database
rake db:create
rake db:migrate
Restart Rails server and open any page to check your app
rails s
open http://localhost:3000
Create Heroku App
heroku create
Get host name
export HOST_NAME_HEROKU=`heroku info -s | grep domain_name | cut -d= -f2`
Add host name to Recaptcha domains
open https://www.google.com/recaptcha/admin#list
Set ENV variables to Heroku
heroku config:set \
       HOST_NAME=$HOST_NAME_HEROKU \
       SMTP_HOST=$SMTP_HOST \
       SMTP_PORT=$SMTP_PORT \
       SMTP_USER=$SMTP_USER \
       SMTP_PASS=$SMTP_PASS \
       RECAPTCHA_PUBLIC_KEY=$RECAPTCHA_PUBLIC_KEY \
       RECAPTCHA_PRIVATE_KEY=$RECAPTCHA_PRIVATE_KEY
Create database and Redis
heroku addons:create heroku-postgresql:hobby-dev
heroku addons:create heroku-redis:hobby-dev
Install Unicorn as the app server and add rails_12factor
nano Gemfile
# Use Unicorn as the app server
gem 'unicorn'
gem 'rails_12factor'
bundle install
Create Procfile for Unicorn and Sidekiq
nano Procfile
web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb
worker: bundle exec sidekiq -q mailers -q default
Create Unicorn config
nano config/unicorn.rb
worker_processes Integer(ENV["WEB_CONCURRENCY"] || 3)
timeout 90
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
Add Heroku to Git
git add .
git commit -m "Add Heroku config"
git push
Push to Heroku and migrate
git push heroku master
heroku run rake db:migrate
Start worker dyno
heroku ps:scale worker=1
Open the app
heroku open
Add Papertrail
heroku addons:create papertrail
heroku restart
heroku addons:open papertrail
Next step: Update README