diff options
author | rhenium <rhenium@rhe.jp> | 2015-03-18 22:06:27 +0900 |
---|---|---|
committer | rhenium <rhenium@rhe.jp> | 2015-03-18 22:06:36 +0900 |
commit | 181d786f2484e8078037495a17633427babef6da (patch) | |
tree | 3fc49c4a840ef4114c7b85852a0973d025012416 | |
parent | 8e96277bb5a819a001a4fdd2ba71331fc484cf3d (diff) | |
download | aclog-181d786f2484e8078037495a17633427babef6da.tar.gz |
[WIP] web: use puma instead of unicorn
-rw-r--r-- | Gemfile | 5 | ||||
-rw-r--r-- | Gemfile.lock | 23 | ||||
-rw-r--r-- | config.ru | 11 | ||||
-rw-r--r-- | config/initializers/oink.rb | 3 | ||||
-rw-r--r-- | config/initializers/puma_worker_killer.rb | 7 | ||||
-rw-r--r-- | config/puma.rb | 22 | ||||
-rw-r--r-- | config/unicorn.rb | 36 | ||||
-rw-r--r-- | example/nginx.conf | 6 | ||||
-rw-r--r-- | lib/tasks/web.rake | 30 |
9 files changed, 65 insertions, 78 deletions
@@ -15,8 +15,8 @@ gem "jquery-rails" gem "bootstrap-sass" gem "turbolinks" -gem "unicorn" -gem "unicorn-worker-killer" +gem "puma" +gem "puma_worker_killer" gem "dalli" gem "omniauth-twitter" @@ -32,6 +32,7 @@ group :development, :test do gem "web-console", "~> 2.0.0" gem "spring" gem "quiet_assets" + gem "oink" end group :test do diff --git a/Gemfile.lock b/Gemfile.lock index 41bb174..3e91357 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -140,6 +140,7 @@ GEM hashie (3.4.0) hike (1.2.3) hitimes (1.2.2) + hodel_3000_compliant_logger (0.1.1) html2haml (2.0.0) erubis (~> 2.7.0) haml (~> 4.0.0) @@ -155,7 +156,6 @@ GEM railties (>= 4.2.0) thor (>= 0.14, < 2.0) json (1.8.2) - kgio (2.9.3) listen (2.9.0) celluloid (>= 0.15.2) rb-fsevent (>= 0.9.3) @@ -188,6 +188,9 @@ GEM nenv (~> 0.1) shellany (~> 0.0) oauth (0.4.7) + oink (0.10.1) + activerecord + hodel_3000_compliant_logger omniauth (1.2.2) hashie (>= 1.2, < 4) rack (~> 1.0) @@ -203,6 +206,11 @@ GEM slop (~> 3.4) pry-rails (0.3.3) pry (>= 0.9.10) + puma (2.11.1) + rack (>= 1.1, < 2.0) + puma_worker_killer (0.0.3) + get_process_mem (~> 0.1) + puma (~> 2.7) quiet_assets (1.1.0) railties (>= 3.1, < 5.0) rabl (0.11.6) @@ -238,7 +246,6 @@ GEM activesupport (= 4.2.0) rake (>= 0.8.7) thor (>= 0.18.1, < 2.0) - raindrops (0.13.0) rake (10.4.2) rb-fsevent (0.9.4) rb-inotify (0.9.5) @@ -332,13 +339,6 @@ GEM unf (0.1.4) unf_ext unf_ext (0.0.6) - unicorn (4.8.3) - kgio (~> 2.6) - rack - raindrops (~> 0.7) - unicorn-worker-killer (0.4.3) - get_process_mem (~> 0) - unicorn (~> 4) virtus (1.0.4) axiom-types (~> 0.1) coercible (~> 1.0) @@ -374,8 +374,11 @@ DEPENDENCIES msgpack msgpack-rpc mysql2 + oink omniauth-twitter pry-rails + puma + puma_worker_killer quiet_assets rails (~> 4.2.0) rspec @@ -390,8 +393,6 @@ DEPENDENCIES twitter-text tzinfo-data uglifier - unicorn - unicorn-worker-killer web-console (~> 2.0.0) webmock yajl-ruby @@ -1,15 +1,4 @@ # This file is used by Rack-based servers to start the application. require ::File.expand_path('../config/environment', __FILE__) -if Rails.env.production? - # Unicorn self-process killer - require "unicorn/worker_killer" - - # Max requests per worker - use Unicorn::WorkerKiller::MaxRequests, 3072, 4096 - - # Max memory size (RSS) per worker - use Unicorn::WorkerKiller::Oom, (192*(1024**2)), (256*(1024**2)) -end - run Rails.application diff --git a/config/initializers/oink.rb b/config/initializers/oink.rb new file mode 100644 index 0000000..5bc53da --- /dev/null +++ b/config/initializers/oink.rb @@ -0,0 +1,3 @@ +if Rails.env.development? + Rails.application.middleware.use(Oink::Middleware, logger: Rails.logger) +end diff --git a/config/initializers/puma_worker_killer.rb b/config/initializers/puma_worker_killer.rb new file mode 100644 index 0000000..71d8483 --- /dev/null +++ b/config/initializers/puma_worker_killer.rb @@ -0,0 +1,7 @@ +PumaWorkerKiller.config do |config| + config.ram = 1024 + config.frequency = 5 # sec + config.percent_usage = 0.98 +end + +PumaWorkerKiller.start diff --git a/config/puma.rb b/config/puma.rb new file mode 100644 index 0000000..fa22c6b --- /dev/null +++ b/config/puma.rb @@ -0,0 +1,22 @@ +_rails_root = File.expand_path("../../", __FILE__) + +directory _rails_root + +_log_file = File.join(_rails_root, "log", "puma.log") +stdout_redirect _log_file, _log_file, true + +pidfile File.join(_rails_root, "tmp", "pids", "puma.pid") + +threads 4, 16 +workers 2 + +bind "unix://" + File.join(_rails_root, "tmp", "sockets", "puma.sock") +port 3000 + +on_worker_boot do + ActiveSupport.on_load(:active_record) do + ActiveRecord::Base.establish_connection + end +end + +preload_app! diff --git a/config/unicorn.rb b/config/unicorn.rb deleted file mode 100644 index fc20b0b..0000000 --- a/config/unicorn.rb +++ /dev/null @@ -1,36 +0,0 @@ -_rails_root = File.expand_path("../../", __FILE__) - -worker_processes 8 -working_directory _rails_root - -pid File.join(_rails_root, "tmp", "pids", "unicorn.pid").to_s - -listen File.join(_rails_root, "tmp", "sockets", "unicorn.sock"), backlog: 64 -listen 8080 - -_log_file = File.join(_rails_root, "log", "unicorn.log") -stderr_path _log_file -stdout_path _log_file - -preload_app true -GC.respond_to?(:copy_on_write_friendly=) and - GC.copy_on_write_friendly = true - -before_fork do |server, worker| - defined?(ActiveRecord::Base) and - ActiveRecord::Base.connection.disconnect! - - 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 - -after_fork do |server, worker| - defined?(ActiveRecord::Base) and - ActiveRecord::Base.establish_connection -end diff --git a/example/nginx.conf b/example/nginx.conf index d854e66..298f0a6 100644 --- a/example/nginx.conf +++ b/example/nginx.conf @@ -31,7 +31,7 @@ http { #gzip on; upstream aclog-webserver { - server unix:/var/webapps/aclog/tmp/sockets/unicorn.sock; + server unix:/var/webapps/aclog/tmp/sockets/puma.sock; } server { @@ -40,10 +40,10 @@ http { location / { root /var/webapps/aclog/public; - try_files $uri $uri/index.xhtml $uri.xhtml @unicorn; + try_files $uri $uri/index.xhtml $uri.xhtml @rack; } - location @unicorn { + location @rack { proxy_redirect off; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; diff --git a/lib/tasks/web.rake b/lib/tasks/web.rake index 302e57c..c75868b 100644 --- a/lib/tasks/web.rake +++ b/lib/tasks/web.rake @@ -1,5 +1,5 @@ namespace :web do - @web_pid_file = Rails.root.join("tmp", "pids", "unicorn.pid").to_s + @web_pid_file = Rails.root.join("tmp", "pids", "puma.pid").to_s def web_read_pid Integer(File.read(@web_pid_file)) rescue nil @@ -9,53 +9,53 @@ namespace :web do Process.kill(0, pid) rescue false end - desc "Start aclog collector (master) in the foreground" + desc "Start web server in the foreground" task :run do - system "unicorn -E #{Rails.env} -c #{Rails.root}/config/unicorn.rb" + system "puma -e #{Rails.env} -C #{Rails.root}/config/puma.rb" end - desc "Start web server (Unicorn)" + desc "Start web server" task :start do pid = web_read_pid if pid && process_alive?(pid) - STDERR.puts "Unicorn is already started (PID: #{pid})" + STDERR.puts "Web server is already started (PID: #{pid})" next end - system "unicorn -D -E #{Rails.env} -c #{Rails.root}/config/unicorn.rb" + system "puma -d -e #{Rails.env} -C #{Rails.root}/config/puma.rb" end - desc "Stop web server (Unicorn)" + desc "Stop web server" task :stop do pid = web_read_pid unless process_alive?(pid) - STDERR.puts "Unicorn is not running." + STDERR.puts "Puma is not running." next end - Process.kill(:QUIT, pid) + Process.kill(:TERM, pid) while process_alive?(pid) - sleep 0.1 + sleep 0.05 end end - desc "Retart web server (Unicorn)" + desc "Retart web server" task :restart do pid = web_read_pid unless process_alive?(pid) - STDERR.puts "Unicorn is not running." + STDERR.puts "Puma is not running." Rake::Task["web:start"].invoke end Process.kill("USR2", pid) end - desc "Show status of web server (Unicorn)" + desc "Show status of web server" task :status do pid = web_read_pid if pid && process_alive?(pid) - STDOUT.puts "Unicorn is running." + STDOUT.puts "Puma is running." else - STDOUT.puts "Unicorn is not running." + STDOUT.puts "Puma is not running." end end end |