Browse Source

using unicorn for rack, sinatra and rails-stripped

Patrick Falls 12 years ago
parent
commit
eaabe402d1
6 changed files with 81 additions and 45 deletions
  1. 0 6
      rack/benchmark_config
  2. 9 2
      rack/setup_ruby.py
  3. 0 30
      rack/setup_unicorn.py
  4. 10 3
      rails-stripped/setup_ruby.py
  5. 52 0
      sinatra/config/unicorn.rb
  6. 10 4
      sinatra/setup_ruby.py

+ 0 - 6
rack/benchmark_config

@@ -12,12 +12,6 @@
       "json_url": "/rack/json",
       "port": 8080,
       "sort": 15
-    },
-    "unicorn": {
-      "setup_file": "setup_unicorn",
-      "json_url": "/json",
-      "port": 8080,
-      "sort": 36
     }
   }]
 }

+ 9 - 2
rack/setup_ruby.py

@@ -2,6 +2,7 @@
 import subprocess
 import sys
 import re
+import os
 
 def start(args):
 
@@ -9,13 +10,19 @@ def start(args):
     subprocess.check_call("rvm ruby-2.0.0-p0 do bundle install --gemfile=Gemfile-ruby", shell=True, cwd="rack")
     subprocess.check_call("cp Gemfile-ruby Gemfile", shell=True, cwd="rack")
     subprocess.check_call("cp Gemfile-ruby.lock Gemfile.lock", shell=True, cwd="rack")
-    subprocess.check_call("rvm ruby-2.0.0-p0 do bundle exec passenger start -p 8080 -d -e production --pid-file=$HOME/FrameworkBenchmarks/rack/rack.pid --nginx-version=1.2.7 --max-pool-size=24", shell=True, cwd="rack")
+    subprocess.Popen("rvm ruby-2.0.0-p0 do bundle exec unicorn -E production -c config/unicorn.rb", shell=True, cwd="rack")
     return 0
   except subprocess.CalledProcessError:
     return 1
 def stop():
   try:
-    subprocess.check_call("rvm ruby-2.0.0-p0 do bundle exec passenger stop --pid-file=$HOME/FrameworkBenchmarks/rack/rack.pid", shell=True, cwd='rack')
+    p = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE)
+    out, err = p.communicate()
+    for line in out.splitlines():
+      if 'unicorn' in line and 'master' in line:
+        pid = int(line.split(None, 2)[1])
+        os.kill(pid, 9)
+    # subprocess.check_call("rvm ruby-2.0.0-p0 do bundle exec passenger stop --pid-file=$HOME/FrameworkBenchmarks/rack/rack.pid", shell=True, cwd='rack')
     subprocess.check_call("rm Gemfile", shell=True, cwd="rack")
     subprocess.check_call("rm Gemfile.lock", shell=True, cwd="rack")
     return 0

+ 0 - 30
rack/setup_unicorn.py

@@ -1,30 +0,0 @@
-
-import subprocess
-import sys
-import re
-import os
-
-def start(args):
-
-  try:
-    subprocess.check_call("rvm ruby-2.0.0-p0 do bundle install --gemfile=Gemfile-ruby", shell=True, cwd="rack")
-    subprocess.check_call("cp Gemfile-ruby Gemfile", shell=True, cwd="rack")
-    subprocess.check_call("cp Gemfile-ruby.lock Gemfile.lock", shell=True, cwd="rack")
-    subprocess.Popen("rvm ruby-2.0.0-p0 do bundle exec unicorn -E production -c config/unicorn.rb", shell=True, cwd="rack")
-    return 0
-  except subprocess.CalledProcessError:
-    return 1
-def stop():
-  try:
-    p = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE)
-    out, err = p.communicate()
-    for line in out.splitlines():
-      if 'unicorn' in line and 'master' in line:
-        pid = int(line.split(None, 2)[1])
-        os.kill(pid, 9)
-    # subprocess.check_call("rvm ruby-2.0.0-p0 do bundle exec passenger stop --pid-file=$HOME/FrameworkBenchmarks/rack/rack.pid", shell=True, cwd='rack')
-    subprocess.check_call("rm Gemfile", shell=True, cwd="rack")
-    subprocess.check_call("rm Gemfile.lock", shell=True, cwd="rack")
-    return 0
-  except subprocess.CalledProcessError:
-    return 1

+ 10 - 3
rails-stripped/setup_ruby.py

@@ -1,23 +1,30 @@
 
 import subprocess
 import sys
+import re
+import os
 import setup_util
 
 def start(args):
   setup_util.replace_text("rails-stripped/config/database-ruby.yml", "host: .*", "host: " + args.database_host)
-  
   try:
     subprocess.check_call("rvm ruby-2.0.0-p0 do bundle install --gemfile=Gemfile-ruby", shell=True, cwd="rails-stripped")
     subprocess.check_call("cp Gemfile-ruby Gemfile", shell=True, cwd="rails-stripped")
     subprocess.check_call("cp Gemfile-ruby.lock Gemfile.lock", shell=True, cwd="rails-stripped")
     subprocess.check_call("cp config/database-ruby.yml config/database.yml", shell=True, cwd="rails-stripped")
-    subprocess.check_call("rvm ruby-2.0.0-p0 do bundle exec passenger start -p 8080 -d -e production --pid-file=$HOME/FrameworkBenchmarks/rails/rails.pid --nginx-version=1.2.7 --max-pool-size=24", shell=True, cwd="rails-stripped")
+    subprocess.Popen("rvm ruby-2.0.0-p0 do bundle exec unicorn_rails -E production -c config/unicorn.rb", shell=True, cwd="rails-stripped")
     return 0
   except subprocess.CalledProcessError:
     return 1
 def stop():
   try:
-    subprocess.check_call("rvm ruby-2.0.0-p0 do bundle exec passenger stop --pid-file=$HOME/FrameworkBenchmarks/rails/rails.pid", shell=True, cwd='rails-stripped')
+    p = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE)
+    out, err = p.communicate()
+    for line in out.splitlines():
+      if 'unicorn' in line and 'master' in line:
+        pid = int(line.split(None, 2)[1])
+        os.kill(pid, 9)
+    # subprocess.check_call("rvm ruby-2.0.0-p0 do bundle exec passenger stop --pid-file=$HOME/FrameworkBenchmarks/rack/rack.pid", shell=True, cwd='rack')
     subprocess.check_call("rm Gemfile", shell=True, cwd="rails-stripped")
     subprocess.check_call("rm Gemfile.lock", shell=True, cwd="rails-stripped")
     return 0

+ 52 - 0
sinatra/config/unicorn.rb

@@ -0,0 +1,52 @@
+worker_processes 8
+
+preload_app true
+GC.respond_to?(:copy_on_write_friendly=) and
+  GC.copy_on_write_friendly = true
+
+  before_fork do |server, worker|
+    # the following is highly recomended for Rails + "preload_app true"
+    # as there's no need for the master process to hold a connection
+    #defined?(ActiveRecord::Base) and
+    #  ActiveRecord::Base.connection.disconnect!
+
+    # The following is only recommended for memory/DB-constrained
+    # installations.  It is not needed if your system can house
+    # twice as many worker_processes as you have configured.
+    #
+    # # This allows a new master process to incrementally
+    # # phase out the old master process with SIGTTOU to avoid a
+    # # thundering herd (especially in the "preload_app false" case)
+    # # when doing a transparent upgrade.  The last worker spawned
+    # # will then kill off the old master process with a SIGQUIT.
+    # 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
+    #
+    # Throttle the master from forking too quickly by sleeping.  Due
+    # to the implementation of standard Unix signal handlers, this
+    # helps (but does not completely) prevent identical, repeated signals
+    # from being lost when the receiving process is busy.
+    # sleep 1
+  end
+
+  after_fork do |server, worker|
+    # per-process listener ports for debugging/admin/migrations
+    # addr = "127.0.0.1:#{9293 + worker.nr}"
+    # server.listen(addr, :tries => -1, :delay => 5, :tcp_nopush => true)
+
+    # the following is *required* for Rails + "preload_app true",
+    #defined?(ActiveRecord::Base) and
+    #  ActiveRecord::Base.establish_connection
+
+    # if preload_app is true, then you may also want to check and
+    # restart any other shared sockets/descriptors such as Memcached,
+    # and Redis.  TokyoCabinet file handles are safe to reuse
+    # between any number of forked children (assuming your kernel
+    # correctly implements pread()/pwrite() system calls)
+  end

+ 10 - 4
sinatra/setup_ruby.py

@@ -1,22 +1,28 @@
 
 import subprocess
 import sys
-import setup_util
+import re
+import os
 
 def start(args):
-  setup_util.replace_text("sinatra/hello_world.rb", ":host => '.*'", ":host => '" + args.database_host + "'")
 
   try:
     subprocess.check_call("rvm ruby-2.0.0-p0 do bundle install --gemfile=Gemfile-ruby", shell=True, cwd="sinatra")
     subprocess.check_call("cp Gemfile-ruby Gemfile", shell=True, cwd="sinatra")
     subprocess.check_call("cp Gemfile-ruby.lock Gemfile.lock", shell=True, cwd="sinatra")
-    subprocess.check_call("rvm ruby-2.0.0-p0 do bundle exec passenger start -p 8080 -d -e production --pid-file=$HOME/FrameworkBenchmarks/sinatra/sinatra.pid --nginx-version=1.2.7 --max-pool-size=24", shell=True, cwd="sinatra")
+    subprocess.Popen("rvm ruby-2.0.0-p0 do bundle exec unicorn -E production -c config/unicorn.rb", shell=True, cwd="sinatra")
     return 0
   except subprocess.CalledProcessError:
     return 1
 def stop():
   try:
-    subprocess.check_call("rvm ruby-2.0.0-p0 do bundle exec passenger stop --pid-file=$HOME/FrameworkBenchmarks/sinatra/sinatra.pid", shell=True, cwd='sinatra')
+    p = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE)
+    out, err = p.communicate()
+    for line in out.splitlines():
+      if 'unicorn' in line and 'master' in line:
+        pid = int(line.split(None, 2)[1])
+        os.kill(pid, 9)
+    # subprocess.check_call("rvm ruby-2.0.0-p0 do bundle exec passenger stop --pid-file=$HOME/FrameworkBenchmarks/sinatra/sinatra.pid", shell=True, cwd='sinatra')
     subprocess.check_call("rm Gemfile", shell=True, cwd="sinatra")
     subprocess.check_call("rm Gemfile.lock", shell=True, cwd="sinatra")
     return 0