Download pdf - Heroku Tips and Hacks

Transcript
Page 1: Heroku Tips and Hacks

HEROKU TIPS AND HACKS

1. Create an account on Heroku.

- Go to website: http://heroku.com/

2. Install Heroku toolbelt: (https://toolbelt.heroku.com/)

- It is everything you need to get started using Heroku.

- Install: wget -qO- https://toolbelt.heroku.com/install-ubuntu.sh | sh

3. Login to Heroku

- Run: $ heroku login (enter username and password)

- If have errors about ssh key:

http://www.ajibanda.com/2013/02/reset-git-ssh-key-to-log-in-using-a-new-heroku-

account.html

4. Add gem and config asset pipeline :

- In Gemfile:

gem ‘rails_12factor’, group:production

gem ‘thin’

- In config/application.rb:

config.assets.initialize_on_precompile = false

* Remember: Must commit Gemfile.lock to server to deploy.

* More details: https://devcenter.heroku.com/articles/getting-started-with-rails3

5. CLI Heroku: $ heroku run <...>

$ heroku run rake db:migrate

$ heroku restart <name_app_heroku>

$heroku open (open website in browser)

$ heroku run rails c

$ heroku run ruby script/imap_processor start

Page 2: Heroku Tips and Hacks

- Dùng cmd: $ heroku run bash

- View list current account: $ heroku accounts

6. Push local project to Heroku

- Git init in local project:

$ git init

- Add host to current git:

$ git remote add <origin> <git@heroku/app>

([email protected]:bookmanageapp.git)

- Check remote exist or not? :

$ git remote -v

- Add and commit code

- Push code - Deploy code

$ git push [hostname] [branch] / $ git push origin/heroku master

7. Cloning Existing Heroku Applications

$ heroku login

$ heroku git:clone -a <my_app>

$ cd my_app

* More details: https://devcenter.heroku.com/articles/git-clone-heroku-app

8. Delayed Job

* More details: https://devcenter.heroku.com/articles/delayed-job

9. View processes - logs

- View processes:

$ heroku ps

=== delayed (1X): `bundle exec rake jobs:work`

delayed.1: up 2013/11/04 19:38:01 (~ 15h ago)

=== web (1X): `bundle exec thin start -R config.ru -e $RAILS_ENV -p $PORT`

web.1: up 2013/11/05 08:12:55 (~ 2h ago)

Page 3: Heroku Tips and Hacks

- View logs:

$ heroku logs / $ heroku logs --tail

$ heroku logs -p imap.1

$ heroku logs -p web.

- View db info:

$ heroku config

* More details: https://devcenter.heroku.com/articles/logging

10. Multi accounts:

- Add account:

$ heroku accounts:add <name> -- auto

- Remove account:

$ heroku accounts:remove <name>

- Using account:

$ heroku accounts:set <name>

* Remember: Config (host) in: ~/.ssh/config

* More details: https://github.com/ddollar/heroku-accounts

- Add key: $ heroku keys:add

- Ex: Found the following SSH public keys:

1) id_rsa.pub

2) identity.heroku.app_rails.pub

3) identity.heroku.personal.pub

4) identity.heroku.work.pub

5) ssh-add id_rsa.pub

Which would you like to use with your Heroku account? => 3

*More details: https://devcenter.heroku.com/articles/keys

11. Config timeout:

https://github.com/heroku/rack-timeout

http://www.rubydoc.info/gems/rack-timeout/0.0.4/frames

Page 4: Heroku Tips and Hacks

12. Access to db postgres on Heroku

- Click to Heroku Postgres Dev on website Heroku application.

- In this page, content some info:

Host : host_path (ec2-123-321-333.compute-00.compute-1.amazonaws.com)

Database: db_name_pg

User: user_name

Password: password

- Connect to database:

$ psql -U <username> -h <host> -d <database>

$ psql -U user_name -h host_path -d db_name_pg

13. View Memory (RAM), CPU, Core based on dyno sizes:

Dyno 

Size 

Memory 

(RAM) 

CPU 

Share 

Multitenan

Compute 

(2) 

Price/dyno­hou

1X  512MB  1x  yes  1x­4x  $0.05 

2X  1024MB  2x  yes  4x­8x  $0.10 

PX  6GB  100% (1)  no  40x $0.80 

 

*More details:https://devcenter.heroku.com/articles/dyno-size#available-dyno-sizes

14. Config Domain:

- Add domain:

$ heroku domain add: <example.com>

or add in Setting section on heroku website.

Page 5: Heroku Tips and Hacks

- Get domain default on heroku: http://<my_app>.heroku.com

Ex: http://my_app.herokuapp.com

- Get IP of above domain:

$ host my_app.herokuapp.com

=> my_app.herokuapp.com has address 23.23.80.206

- Go to Godaddy or others service, config your domain point to this IP.

More details: https://devcenter.heroku.com/articles/custom-domains

15. Upgrade Mongolab (From sandbox-free to Single Node SM)

Changing to or from a single-node plan on Heroku:

1. Stop your app and any other processes writing to your database

2. Backup your database using mongodump

3. Remove your existing add-on

4. Add a new add-on (see available plans here)

5. Restore your database using mongorestore

6. Change your connection string info in your app

7. Restart your app

Example: $ mongodump -h ds0123456.mongolab.com:3000 -d heroku_app -u user -p password -o . $ heroku addons:remove mongolab $ heroku addons:add mongolab:shared-single-small $ mongorestore -h <new_host>:<new_port> -d heroku_app -u user -p <new_password> .

* Get <new_password>: Run :

$ heroku config | grep MONGOLAB_URI Output:

mongodb://heroku_app:[email protected]:3000/heroku_app Compare with: mongodb://<dbuser>:<dbpassword>@ds0123456.mongolab.com:3000/<dbname>

Informations account : dbuser: dbpassword

dbpassword: new_password

dbname: heroku_app

* More details: https://devcenter.heroku.com/articles/mongolab

Page 6: Heroku Tips and Hacks

16. Improve performance with Unicorn (Concurrency)

- Add Gem:

gem ‘unicorn’

- Config:

Create a file configuration for Unicorn at config/unicorn.rb

- When using mongoid:

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

timeout 30

preload_app true

before_fork do |server, worker|

puts worker

end

after_fork do |server, worker|

puts worker

end

If using Mongoid, you don't need to manually reconnect as in the gist. Mongoid claims:

"When using Unicorn or Passenger, each time a child process is forked when using

app preloading or smart spawning, Mongoid will automatically reconnect to the

master database. If you are doing this in your application manually you may remove

your code." (http://mongoid.org/en/mongoid/docs/rails.html)

* More details: https://devcenter.heroku.com/articles/rails-unicorn

Other references:

http://railsapps.github.io/rails-heroku-tutorial.html

https://devcenter.heroku.com/articles/getting-started-with-rails3

* Developer: Nguyễn Thành Luân