Browse Source

mruby setup script

Keenan Brock 10 years ago
parent
commit
66a40fb18f

+ 4 - 4
frameworks/Ruby/nginx_mruby/nginx.conf

@@ -14,7 +14,7 @@ http {
 #        https://github.com/matsumoto-r/ngx_mruby/wiki/Class-and-Method#nginxrequest-class
 
     # db connection pool
-    mruby_init_worker_code 'Userdata.new("my_#{Process.pid}").db = MySQL::Database.new("localhost", "root", "", "benchmark")';
+    mruby_init_worker_code 'Userdata.new("my_#{Process.pid}").db = MySQL::Database.new("DBHOSTNAME", "root", "", "benchmark")';
     mruby_exit_worker_code 'db = Userdata.new("my_#{Process.pid}").db ; db.close if db';
     server {
         listen       8000;
@@ -37,17 +37,17 @@ http {
 
         location ~ /db {
             default_type "application/json";
-            mruby_content_handler '/usr/local/nginx-1.4.7mruby/html/db.rb' cache;
+            mruby_content_handler 'CWD/nginx_mruby/db.rb' cache;
         }
 
         location ~ /queries {
             default_type "application/json";
-            mruby_content_handler '/usr/local/nginx-1.4.7mruby/html/queries.rb' cache;
+            mruby_content_handler 'CWD/nginx_mruby/queries.rb' cache;
         }
 
         location ~ /fotunes {
             default_type "application/html";
-            mruby_content_handler '/usr/local/nginx-1.4.7mruby/html/fotunes.rb' cache;
+            mruby_content_handler 'CWD/nginx_mruby/fotunes.rb' cache;
         }
 
 

+ 25 - 0
frameworks/Ruby/nginx_mruby/queries.rb

@@ -0,0 +1,25 @@
+r = Nginx::Request.new
+
+num_queries = r.var.arg_queries.to_i
+num_queries = 1 if num_queries < 0
+num_queries = 500 if num_queries > 500
+
+# https://github.com/mattn/mruby-mysql/blob/master/example/example.rb
+class Worlds
+  def db
+    @db ||= Userdata.new("my_#{Process.pid}").db
+  end
+
+  def find(id)
+    db.execute("select * from World where id = ?", id) do |row, fields|
+      return Hash[fields.zip(row)]
+    end
+  end
+
+  def save(world)
+    db.execute("update World set randomNumber = ? where id = ?", world["randomNumber"], world["randomNumber"])
+  end
+end
+
+ret = num_queries.times.map { World.find(rand(10000)) }
+Nginx.rputs JSON::stringify(ret)

+ 16 - 0
frameworks/Ruby/nginx_mruby/setup.py

@@ -0,0 +1,16 @@
+import subprocess
+#import sys
+import setup_util
+#import os
+
+def start(args, logfile, errfile):
+  setup_util.replace_text("nginx_mruby/nginx.conf", "CWD", args.troot)
+  setup_util.replace_text("nginx_mruby/nginx.conf", "DBHOSTNAME", args.database_host)
+  subprocess.Popen('sudo /usr/local/nginx_mruby/nginx/sbin/nginx -c $TROOT/nginx.conf -g "worker_processes ' + str((args.max_threads)) + ';"', shell=True, cwd="nginx_mruby", stderr=errfile, stdout=logfile)
+
+  return 0
+
+def stop(logfile, errfile):
+  subprocess.Popen('sudo /usr/local/nginx_mruby/nginx/sbin/nginx -c $TROOT/nginx.conf -s stop', shell=True, cwd="nginx_mruby", stderr=errfile, stdout=logfile)
+
+  return 0

+ 4 - 4
frameworks/Ruby/nginx_mruby/source_code

@@ -1,4 +1,4 @@
-./db.rb
-./fortune.rb
-./queries.rb
-./updates.rb
+./nginx_mruby/db.rb
+./nginx_mruby/fortune.rb
+./nginx_mruby/queries.rb
+./nginx_mruby/updates.rb

+ 31 - 0
frameworks/Ruby/nginx_mruby/updates.rb

@@ -0,0 +1,31 @@
+r = Nginx::Request.new
+
+num_queries = r.var.arg_queries.to_i
+num_queries = 1 if num_queries < 0
+num_queries = 500 if num_queries > 500
+
+class Worlds
+  def db
+    @db ||= Userdata.new("my_#{Process.pid}").db
+  end
+
+  def find(id)
+    db.execute("select * from World where id = ?", id) do |row, fields|
+      return Hash[fields.zip(row)]
+    end
+  end
+
+  def save(world)
+    #TODO: consider execute_batch
+    db.execute("update World set randomNumber = ? where id = ?", world["randomNumber"], world["randomNumber"])
+  end
+end
+
+ret = num_queries.times.map { World.find(rand(10000)) }
+
+ret.each do |world|
+  world["randomNumber"] = rand(10000) + 1
+  World.save(world)
+end
+
+Nginx.rputs JSON::stringify(ret)

+ 8 - 3
toolset/setup/linux/webservers/nginx_mruby.sh

@@ -1,10 +1,13 @@
 #!/bin/bash
 
-PREFIX=/src/local/nginx_mruby
+PREFIX=/usr/local/nginx_mruby
 
-RETCODE=$(fw_exists ${PREFIX})
-[ "$RETCODE" != 0 ] || { return 0; }
+RETCODE=$(fw_exists nginx_mruby.installed)
+[ ! "$RETCODE" == 0 ] || { return 0; }
 
+fw_depends rvm
+rvm install ruby-2.0.0-p0
+rvm use ruby-2.0.0-p0
 #fw_depends nginx lua
 
 #fw_get http://openresty.org/download/ngx_openresty-1.7.4.1.tar.gz
@@ -23,3 +26,5 @@ if [ "$RETCODE" != 0 ] ; then
 fi
 
 NGINX_CONFIG_OPT_ENV="--prefix=${PREFIX} --with-http_stub_status_module" sh build.sh
+
+touch $IROOT/nginx_mruby.installed