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

Migrate from Unicorn to Puma

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers. →

Migrate from Unicorn to Puma

Unicorn から Puma への移行で変わること。また、社内で発生した問題とその原因について。

Avatar for Tetsuya Kaneko

Tetsuya Kaneko

January 08, 2020

More Decks by Tetsuya Kaneko

Other Decks in Programming

Transcript

  1. Prefork $ ps ux | grep unicor n 16935 ?

    S 0:04 unicorn master -c /home/devuser/www/ecbo_cloak/current/config/unicorn/agasa.rb -E deployment - D 16992 ? S 0:00 unicorn worker[0] -c /home/devuser/www/ecbo_cloak/current/config/unicorn/agasa.rb -E deployment - D 16993 ? S 0:00 unicorn worker[1] -c /home/devuser/www/ecbo_cloak/current/config/unicorn/agasa.rb -E deployment -D
  2. Copy-On-Write (Pros.) • Unix ϕʔεͷ OS ʹ copy-on-write ͕࣮૷͞Ε͍ͯΔ •

    Ϛελʔϓϩηεͷىಈ࣌ʹ͸ Rails ΛಡΈࠐΉͨΊʹϝϞϦ Λফඅ͢Δ͕ɺϫʔΧʔϓϩηε͸ϝϞϦͷࢀর࣌ʹ͸Ϛε λʔϓϩηεͷ෺ཧΞυϨεۭؒΛࢀর͢ΔͨΊɺϝϞϦΛফ අ͠ͳ͍
  3. DBͷ ID/PW มߋ࣌ʹൃੜͨ͠ෆ۩߹ • MySQL ʹ৽͍͠ϢʔβΛ௥Ճ • .env ϑΝΠϧͷ DB_USER

    ͱ DB_PASSWORD Λมߋ • rails console ʹͯɺ؀ڥม਺ʹ৽͍͠ ID/PW ͕ઃఆ͞Ε͍ͯΔ ͜ͱΛ֬ೝ • 24࣌ؒޙʹ DB ͷ઀ଓΤϥʔ͕ൃੜ
  4. DBͷ ID/PW มߋ࣌ʹൃੜͨ͠ෆ۩߹ (cont.) cloak/config/unicorn/production.r b preload_app tru e before_fork

    do |server, worker | # there's no need for the master process to hold a connectio n if defined?(ActiveRecord::Base ) ActiveRecord::Base.connection.disconnect ! en d # Before forking, kill the master process that belongs to the .oldbin PID . # This enables 0 downtime deploys . old_pid = "#{server.config[:pid]}.oldbin " if File.exist?(old_pid) && server.pid != old_pi d begi n Process.kill("QUIT", File.read(old_pid).to_i ) rescue Errno::ENOENT, Errno::ESRC H # someone else did our job for u s en d en d end
  5. DBͷ ID/PW มߋ࣌ʹൃੜͨ͠ෆ۩߹ (cont.) cloak/config/unicorn/production.rb … after_fork do |server, worker

    | if defined?(ActiveRecord::Base ) ActiveRecord::Base.establish_connectio n en d end
  6. DBͷ ID/PW มߋ࣌ʹൃੜͨ͠ෆ۩߹ (cont.) • Dotenv • initialized in your

    Rails app during the before_con fi guration callback, which is fi red when the Application constant is de fi ned in con fi g/application.rb • unicorn-worker-killer • Automatically restart Unicorn workers based on 1) max number of requests and 2) max memory