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

Migrate from Unicorn to Puma

Migrate from Unicorn to Puma

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

Tetsuya Kaneko

January 08, 2020
Tweet

More Decks by Tetsuya Kaneko

Other Decks in Programming

Transcript

  1. Migrate from Unicorn to Puma 2020.01.08 Brown Bag Meeting

  2. Unicorn

  3. Unicorn • Prefork Server • Multi Processes / Single Thread

  4. Prefork • ϦΫΤετ͝ͱʹ fork ͢ΔͷͰ͸ͳ͘ɺαʔόىಈ࣌ʹϚε λʔʢ਌ʣϓϩηεΛ1ͭ࡞੒͠ɺ͔ͦ͜Βଟ਺ͷϫʔΧʔ ʢࢠʣϓϩηεΛ࡞੒͢Δ • Ϛελʔϓϩηεͷىಈ࣌ʹ Rails

    Λϩʔυ͢Δ
  5. Prefork cloak/config/unicorn/agasa.rb … worker_processes 2

  6. 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
  7. Multi Processes • ϝϞϦΞυϨεִۭ͕ؒ཭͞ΕΔ • fork͸ϝϞϦ্ͷϓϩηεͷΞυϨεۭؒΛؙ͝ͱผͷΞυϨ εۭؒʹίϐʔ͢ΔͨΊɺҰൠʹ஗͍ͱݴΘΕ͍ͯΔ • Unicorn ͸ϝϞϦʔ؅ཧͷ࠷దԽख๏ͷ1ͭͰ͋ΔCopy-on-

    Write (CoW) ͱ͍͏࢓૊ΈͰɺϝϞϦʔίϐʔͷෛՙΛ཈͑ͯ ͍Δ
  8. Copy-On-Write (Pros.) • Unix ϕʔεͷ OS ʹ copy-on-write ͕࣮૷͞Ε͍ͯΔ •

    Ϛελʔϓϩηεͷىಈ࣌ʹ͸ Rails ΛಡΈࠐΉͨΊʹϝϞϦ Λফඅ͢Δ͕ɺϫʔΧʔϓϩηε͸ϝϞϦͷࢀর࣌ʹ͸Ϛε λʔϓϩηεͷ෺ཧΞυϨεۭؒΛࢀর͢ΔͨΊɺϝϞϦΛফ අ͠ͳ͍
  9. Copy-On-Write (Cons.) • ϝϞϦͷॻ͖ࠐΈ࣌ʹ͸ॻ͖ࠐ·ΕͨϖʔδΛ਌ࢠͰڞ༗͢Δ͜ͱ͸Ͱ ͖ͳ͍ͷͰɺॻ͖ࠐΈલʹ֘౰ϝϞϦϖʔδΛࢠϓϩηεʹίϐʔͯ͠ ͔Βॻ͖ࠐΉ • ڞ༗Ͱ͖Δͱݴͬͯ΋ɺϦΫΤετΛॲཧ͍ͯ͘͠౓ʹ਌ࢠؒͷϝϞϦ ϖʔδͷࠩҟ͸େ͖͘ͳ͍ͬͯ͘ •

    ͜ΕΛ͋Δఔ౓ղܾ͢ΔͨΊʹɺNճϦΫΤετΛॲཧͨ͠ࢠϓϩηε Λ਌ϓϩηε͕ࡴͯ͠࠶fork͢Δ gem (unicorn-worker-killer) ͕͋Δ
  10. Puma

  11. Puma • Prefork Server • Multi Processes / Multi Threads

  12. Multi Threads • جຊ͸ϚϧνϓϩηεϞσϧͱಉ͘͡ɺϦΫΤετ͝ͱʹεϨουΛ ੜ੒͢Δ • εϨου͸ੜ੒ݩͷϓϩηεͱΞυϨεۭؒΛڞ༗͢ΔͨΊɺεϨο υੜ੒ͷίετ͸Ұൠʹϓϩηεੜ੒ΑΓখ͍͞ͱݴΘΕ͍ͯΔ • ϚϧνεϨουϓϩάϥϛϯά͠Α͏ͱ͢Δͱɺෳ਺ͷεϨου͕ϝ

    ϞϦΞυϨεۭؒΛڞ༗͍ͯ͠ΔͨΊɺϦιʔεڝ߹ΛϓϩάϥϚ͕ ҙࣝͯ͠ආ͚ͳ͚Ε͹ͳΒͳ͍ɻ
  13. DBͷ ID/PW มߋ࣌ʹൃੜͨ͠ෆ۩߹ • MySQL ʹ৽͍͠ϢʔβΛ௥Ճ • .env ϑΝΠϧͷ DB_USER

    ͱ DB_PASSWORD Λมߋ • rails console ʹͯɺ؀ڥม਺ʹ৽͍͠ ID/PW ͕ઃఆ͞Ε͍ͯΔ ͜ͱΛ֬ೝ • 24࣌ؒޙʹ DB ͷ઀ଓΤϥʔ͕ൃੜ
  14. 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
  15. 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
  16. 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