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

Rails in production - RubyConfBY 22 Mar 2015

Rails in production - RubyConfBY 22 Mar 2015

Sergey Nartimov

March 22, 2015
Tweet

More Decks by Sergey Nartimov

Other Decks in Programming

Transcript

  1. Development $ rails server => Booting WEBrick => Rails 4.1.10

    application starting in development on http://0.0.0.0:3000 => Run `rails server -h` for more startup options => Notice: server is listening on all interfaces (0.0.0.0). Consider using 127.0.0.1 (--binding option) => Ctrl-C to shutdown server [2015-03-21 10:29:23] INFO WEBrick 1.3.1 [2015-03-21 10:29:23] INFO ruby 2.2.1 (2015-02-26) [x86_64-darwin14] [2015-03-21 10:29:23] INFO WEBrick::HTTPServer#start: pid=42401 port=3000
  2. Nginx server { listen 80; server_name myapp.example.com; root /home/deploy/myapp/public; location

    / { try_files $uri @app; } location @app { proxy_set_header Client-Ip $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $host; proxy_pass http://localhost:3000; } }
  3. Nginx Load%balancing upstream myapp { server http://host1:3000; server http://host2:3000; }

    server { # ... location @app { # ... proxy_pass http://myapp; } }
  4. Unicorn Zero%down)me+restart before_fork do |server, worker| old_pid = "#{server.config[:pid]}.oldbin" if

    old_pid != server.pid begin sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU Process.kill(sig, File.read(old_pid).to_i) rescue Errno::ENOENT, Errno::ESRCH end end end $ kill -USR2 `cat tmp/pids/unicorn.pid`
  5. Deploy'error'example diff --git a/app/models/user.rb b/app/models/user.rb index 40bc07c..3ebc69f 100644 --- a/app/models/user.rb

    +++ b/app/models/user.rb @@ -1,2 +1,5 @@ class User + def greeting + 'Hey!' + end end
  6. Deploy'error'example diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index 1ca0e26..2aeea96 100644 --- a/app/views/users/show.html.erb

    +++ b/app/views/users/show.html.erb @@ -1,4 +1,6 @@ <dl> <dt>Username</dt> <dd><%= @user.username %></dd> + <dt>Greeting</dt> + <dd><%= @user.greeting %></dd> </dl>
  7. Releases /home/deploy/myapp ├── current -> /home/deploy/myapp/releases/20150120114500/ ├── releases │ ├──

    20150080072500 │ ├── 20150090083000 │ ├── 20150100093500 │ ├── 20150110104000 │ └── 20150120114500 ├── repo │ └── <VCS related data> ├── revisions.log └── shared └── <linked_files and linked_dirs>
  8. Removing)columns 1)#Make#the#app#ignore#the#column class User def self.columns super.reject { |c| c.name

    == 'full_name' } end end 2)#Remove#the#column 3)#Remove#the#code#that#ignored#the#column
  9. Performance*metrics module MethodTracer def trace_method(method, name=nil) name ||= "#{self.name.underscore.gsub('/', '.')}.#{method}"

    original_method = :"_untraced_method_#{method}" return if method_defined?(original_method) alias_method original_method, method define_method method do |*args, &block| ActiveSupport::Notifications.instrument("performance.#{name}") do send(original_method, *args, &block) end end end end
  10. Performance*metrics logstash = LogStashLogger.new(port: 5228) ActiveSupport::Notifications.subscribe /^performance\./ do |*args| event

    = ActiveSupport::Notifications::Event.new(*args) logstash.info(metric: event.name, duration: event.duration) end