Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rails: da manutenção ao deploy

Rails: da manutenção ao deploy

Escrevi minha aplicação Rails. E agora?

- Que ferramentas podem ajudar a encontrar problemas de performance?
- Conheça melhor aquela rake task que você nem lembrava que existia.
- Como reaproveitar lógica entre diversas aplicações Rails?
- Quero testar meus scripts de deploy em minha máquina, o que fazer?
- Unicorn ou Thin? Nginx ou Apache? O que usar?

Nesse talk vou mostrar algumas ferramentas que podem ajudar o seu dia a dia como usuário do framework Rails. São dicas que podem acelerar a produtividade e responder aquelas perguntas que acabam aparecendo quando suas primeira aplicações Rails entram em produção

Ricardo Valeriano

October 20, 2012
Tweet

More Decks by Ricardo Valeriano

Other Decks in Programming

Transcript

  1. Objetivo: conhecer melhor as partes que surgem lá no `rails

    new` e são massageadas até o `cap deploy`
  2. NÃO pode mais usar Rails (> =3.1.x) sem Coffee-Script NÃO

    pode mais usar Rails (> =3.1.x) sem Coffee-Script
  3. NÃO pode mais usar Rails (> =3.1.x) sem Coffee-Script NÃO

    pode mais usar Rails (> =3.1.x) sem Coffee-Script @dhh approves
  4. Gemfile # Gems used only for assets and not required

    # in production environments by default. group :assets do # gem 'sass-rails', '~> 3.2.3' # gem 'coffee-rails', '~> 3.2.1' # gem 'therubyracer', :platforms => :ruby gem 'uglifier', '>= 1.0.3' end
  5. Gemfile # Gems used only for assets and not required

    # in production environments by default. group :assets do # gem 'sass-rails', '~> 3.2.3' # gem 'coffee-rails', '~> 3.2.1' # gem 'therubyracer', :platforms => :ruby gem 'uglifier', '>= 1.0.3' end
  6. Gemfile # Gems used only for assets and not required

    # in production environments by default. group :assets do # gem 'sass-rails', '~> 3.2.3' # gem 'coffee-rails', '~> 3.2.1' # gem 'therubyracer', :platforms => :ruby gem 'uglifier', '>= 1.0.3' end
  7. Gemfile # Gems used only for assets and not required

    # in production environments by default. group :assets do # gem 'sass-rails', '~> 3.2.3' # gem 'coffee-rails', '~> 3.2.1' gem 'therubyracer', :platforms => :ruby gem 'uglifier', '>= 1.0.3' end
  8. Gemfile # Gems used only for assets and not required

    # in production environments by default. group :assets do # gem 'sass-rails', '~> 3.2.3' # gem 'coffee-rails', '~> 3.2.1' gem 'therubyracer', :platforms => :ruby gem 'uglifier', '>= 1.0.3' end
  9. template # remove o index e o rails.png remove_file 'public/index.html'

    remove_file 'public/images/rails.png' # comenta sass e conffee no Gemfile # e adiciona rspec gsub_file "Gemfile", /(gem 'sass-rails',.*)/, '# \1' gsub_file "Gemfile", /(gem 'coffee-rails',.*)/, '# \1' gem "rspec-rails", :group => [:development, :test]
  10. template # remove o index e o rails.png remove_file 'public/index.html'

    remove_file 'public/images/rails.png' # comenta sass e conffee no Gemfile # e adiciona rspec gsub_file "Gemfile", /(gem 'sass-rails',.*)/, '# \1' gsub_file "Gemfile", /(gem 'coffee-rails',.*)/, '# \1' gem "rspec-rails", :group => [:development, :test] # resolve dependências usando bundler run "bundle install"
  11. template # remove o index e o rails.png remove_file 'public/index.html'

    remove_file 'public/images/rails.png' # comenta sass e conffee no Gemfile # e adiciona rspec gsub_file "Gemfile", /(gem 'sass-rails',.*)/, '# \1' gsub_file "Gemfile", /(gem 'coffee-rails',.*)/, '# \1' gem "rspec-rails", :group => [:development, :test] # resolve dependências usando bundler run "bundle install" # gera o básico necessário para o rspec generate "rspec:install" append_file ".rspec", "--require spec_helper"
  12. $ rails g model --help Usage: rails generate model NAME

    [field[:type][:index] field[:type][:index]]
  13. config/initializers/ awesome_xoxial_media.rb Photomatic::Application.configure do path = "config/awesome_xoxial_media.yml" options = YAML.load(IO.read(path))[Rails.env]

    end config.axm_key = options["key"] config.axm_secret = options["secret"] options = options.with_indifferent_access
  14. config/initializers/ awesome_xoxial_media.rb Photomatic::Application.configure do path = "config/awesome_xoxial_media.yml" options = YAML.load(IO.read(path))[Rails.env]

    end config.axm_key = options[:key] config.axm_secret = options[:secret] options = options.with_indifferent_access
  15. app/models/post.rb class Post < ActiveRecord::Base attr_accessible :body, :title def notify_xoxial(user)

    xoxial_options = { key: Rails.application.config.axm_key, secret: Rails.application.config.axm_secret } XoxialMedia.publish title, user.xoxial_id, xoxial_options end end
  16. app/models/post.rb class Post < ActiveRecord::Base attr_accessible :body, :title def notify_xoxial(user)

    xoxial_options = { key: Rails.application.config.axm_key, secret: Rails.application.config.axm_secret } XoxialMedia.publish title, user.xoxial_id, xoxial_options end end
  17. app/models/post.rb class Post < ActiveRecord::Base attr_accessible :body, :title def notify_xoxial(user)

    xoxial_options = { key: Rails.application.config.axm_key, secret: Rails.application.config.axm_secret } XoxialMedia.publish title, user.xoxial_id, xoxial_options end end
  18. app/models/post.rb class Post < ActiveRecord::Base attr_accessible :body, :title def notify_xoxial(user)

    xoxial_options = { key: Rails.application.config.axm_key, secret: Rails.application.config.axm_secret } XoxialMedia.publish title, user.xoxial_id, xoxial_options end end Rails.application.config.axm_key
  19. config/initializers/ awesome_xoxial_media.rb module Photomatic mattr_accessor :axm_key, :axm_secret path = "config/awesome_xoxial_media.yml"

    options = YAML.load(IO.read(path))[Rails.env] options = options.with_indifferent_access self.axm_key = options[:key] self.axm_secret = options[:secret] end
  20. config/initializers/ awesome_xoxial_media.rb module Photomatic mattr_accessor :axm_key, :axm_secret path = "config/awesome_xoxial_media.yml"

    options = YAML.load(IO.read(path))[Rails.env] options = options.with_indifferent_access self.axm_key = options[:key] self.axm_secret = options[:secret] end
  21. config/initializers/ awesome_xoxial_media.rb module Photomatic mattr_accessor :axm_key, :axm_secret path = "config/awesome_xoxial_media.yml"

    options = YAML.load(IO.read(path))[Rails.env] options = options.with_indifferent_access self.axm_key = options[:key] self.axm_secret = options[:secret] end
  22. config/initializers/ awesome_xoxial_media.rb module Photomatic mattr_accessor :axm_key, :axm_secret path = "config/awesome_xoxial_media.yml"

    options = YAML.load(IO.read(path))[Rails.env] options = options.with_indifferent_access self.axm_key = options[:key] self.axm_secret = options[:secret] end
  23. app/models/post.rb class Post < ActiveRecord::Base attr_accessible :body, :title def notify_xoxial(user)

    xoxial_options = { key: Rails.application.config.axm_key, secret: Rails.application.config.axm_secret } XoxialMedia.publish title, user.xoxial_id, xoxial_options end end
  24. app/models/post.rb class Post < ActiveRecord::Base attr_accessible :body, :title def notify_xoxial(user)

    xoxial_options = { key: Photomatic.axm_key, secret: Photomatic.axm_secret } XoxialMedia.publish title, user.xoxial_id, xoxial_options end end
  25. config/initializers/ awesome_xoxial_media.rb module Photomatic mattr_accessor :axm_key, :axm_secret path = "config/awesome_xoxial_media.yml"

    options = YAML.load(IO.read(path))[Rails.env] options = options.with_indifferent_access self.axm_key = options[:key] self.axm_secret = options[:secret] end
  26. config/initializers/ awesome_xoxial_media.rb module Photomatic mattr_accessor :axm_key, :axm_secret path = "config/awesome_xoxial_media.yml"

    options = options.with_indifferent_access self.axm_key = options[:key] self.axm_secret = options[:secret] end options = YAML.load(IO.read(path))[Rails.env]
  27. config/initializers/ awesome_xoxial_media.rb module Photomatic mattr_accessor :axm_key, :axm_secret path = "config/awesome_xoxial_media.yml"

    options = options.with_indifferent_access self.axm_key = options[:key] self.axm_secret = options[:secret] end template = ERB.new(IO.read(path)).result options = YAML.load(IO.read(path))[Rails.env]
  28. config/initializers/ awesome_xoxial_media.rb module Photomatic mattr_accessor :axm_key, :axm_secret path = "config/awesome_xoxial_media.yml"

    options = options.with_indifferent_access self.axm_key = options[:key] self.axm_secret = options[:secret] end template = ERB.new(IO.read(path)).result options = YAML.load(IO.read(path))[Rails.env]
  29. config/initializers/ awesome_xoxial_media.rb module Photomatic mattr_accessor :axm_key, :axm_secret path = "config/awesome_xoxial_media.yml"

    options = options.with_indifferent_access self.axm_key = options[:key] self.axm_secret = options[:secret] end template = ERB.new(IO.read(path)).result options = YAML.load(IO.read(path))[Rails.env]
  30. config/initializers/ awesome_xoxial_media.rb module Photomatic mattr_accessor :axm_key, :axm_secret path = "config/awesome_xoxial_media.yml"

    options = options.with_indifferent_access self.axm_key = options[:key] self.axm_secret = options[:secret] end template = ERB.new(IO.read(path)).result options = YAML.load(template)[Rails.env]
  31. config/initializers/ awesome_xoxial_media.rb module Photomatic mattr_accessor :axm_key, :axm_secret path = "config/awesome_xoxial_media.yml"

    options = options.with_indifferent_access self.axm_key = options[:key] self.axm_secret = options[:secret] end template = ERB.new(IO.read(path)).result options = YAML.load(template)[Rails.env]
  32. config/initializers/ awesome_xoxial_media.rb module Photomatic mattr_accessor :axm_key, :axm_secret path = "config/awesome_xoxial_media.yml"

    options = options.with_indifferent_access self.axm_key = options[:key] self.axm_secret = options[:secret] end template = ERB.new(IO.read(path)).result options = YAML.load(template)[Rails.env] # FULEIRO: em prod usar vars AXM_KEY e AXM_SECRET
  33. app/controllers/post_controller.rb class PostsController < ApplicationController # GET /posts # GET

    /posts.json def index @posts = Post.order(:id) logger.info @posts.explain respond_to do |format| format.html # index.html.erb format.json { render json: @posts } end end # ...
  34. app/controllers/post_controller.rb class PostsController < ApplicationController # GET /posts # GET

    /posts.json def index @posts = Post.order(:id) logger.info @posts.explain respond_to do |format| format.html # index.html.erb format.json { render json: @posts } end end # ...
  35. app/controllers/post_controller.rb class PostsController < ApplicationController # GET /posts # GET

    /posts.json def index @posts = Post.order(:id) logger.info @posts.explain respond_to do |format| format.html # index.html.erb format.json { render json: @posts } end end # ...
  36. app/controllers/post_controller.rb class PostsController < ApplicationController # GET /posts # GET

    /posts.json def index @posts = Post.order(:id) logger.info @posts.explain respond_to do |format| format.html # index.html.erb format.json { render json: @posts } end end # ...
  37. config/environments/development.rb # Log the query plan for queries taking #

    more than this (works # with SQLite, MySQL, and PostgreSQL) config.active_record. auto_explain_threshold_in_seconds = 0.5
  38. app/models/ post.rb & author.rb class Post < ActiveRecord::Base attr_accessible :body,

    :title belongs_to :author end class Author < ActiveRecord::Base attr_accessible :email, :name has_many :posts end
  39. todos os (10) posts @posts = Post.order("created_at") @posts.each do |post|

    puts post.title end select * from posts 10 vezes
  40. todos os (10) posts @posts = Post.order("created_at") @posts.each do |post|

    puts post.title end select * from posts 10 vezes
  41. todos os (10) posts @posts = Post.order("created_at") @posts.each do |post|

    puts post.title end select * from posts puts post.author.name
  42. todos os (10) posts @posts = Post.order("created_at") @posts.each do |post|

    puts post.title end select * from posts puts post.author.name select * from author where id = ?
  43. todos os (10) posts @posts = Post.order("created_at") @posts.each do |post|

    puts post.title end select * from posts puts post.author.name select * from author where id = ? N (10) vezes
  44. vendor/engines/photoniac_engine/ photoniac_engine.gemspec Gem::Specification.new do |s| #... s.name = "photoniac_engine" s.version

    = PhotoniacEngine::VERSION #... s.add_dependency "carrierwave" s.add_dependency "mini_magick" #... end
  45. vendor/engines/photoniac_engine/ photoniac_engine.gemspec Gem::Specification.new do |s| #... s.name = "photoniac_engine" s.version

    = PhotoniacEngine::VERSION #... s.add_dependency "carrierwave" s.add_dependency "mini_magick" #... end
  46. Vagrant $ vagrant box add base \ http://files.vagrantup.com/lucid64.box $ vagrant

    init $ vagrant up $ vagrant ssh $ vagrant package $ vagrant box add railsbox \ package.box
  47. SO