Эх сурвалжийг харах

improve setup script to write json config file

Joel Berger 11 жил өмнө
parent
commit
bcd350e246

+ 13 - 1
mojolicious/app.pl

@@ -4,9 +4,21 @@ use Mango;
 use JSON::XS 'encode_json';
 use Scalar::Util 'looks_like_number';
 
+# configuration
+
+plugin JSONConfig => {
+  file => 'app.conf',
+  default => {
+    database_host => 'localhost',
+    workers => 8,
+  },
+};
+
+app->config->{hypnotoad}{workers} = app->config->{workers};
+
 # Database connections
 
-helper mango   => sub { state $mango = Mango->new('mongodb://localhost:27017') };
+helper mango   => sub { state $mango = Mango->new('mongodb://'. shift->config->{database_host} . ':27017') };
 helper db      => sub { state $db = shift->mango->db('hello_world') };
 helper world   => sub { shift->db->collection('World') };
 helper fortune => sub { shift->db->collection('Fortune') };

+ 15 - 8
mojolicious/setup.py

@@ -1,6 +1,7 @@
 import subprocess
 import sys
-import setup_util
+#import setup_util
+import json
 from os.path import expanduser
 import os
 import getpass
@@ -8,23 +9,29 @@ import getpass
 home = expanduser("~")
 
 def start(args, logfile, errfile):
-  setup_util.replace_text("mojolicious/app.pl", "localhost", ""+ args.database_host +"")
-  setup_util.replace_text("mojolicious/nginx.conf", "USR", getpass.getuser())
-  setup_util.replace_text("mojolicious/nginx.conf", "server unix:.*\/FrameworkBenchmarks", "server unix:" + home + "/FrameworkBenchmarks")
+  # setup_util.replace_text("mojolicious/app.pl", "localhost", ""+ args.database_host +"")
+  # str(args.max_threads)
+  conf = { 
+    'database_host': args.database_host,
+    'workers': args.max_threads,
+  }
+  with open('mojolicious/app.conf', 'w') as f:
+    f.write(json.dumps(conf))
 
   try:
-    subprocess.Popen("plackup -E production -s Starman --workers=" + str(args.max_threads) + " -l " + home + "/FrameworkBenchmarks/mojolicious/frameworks-benchmark.sock -a ./app.pl", shell=True, cwd="mojolicious", stderr=errfile, stdout=logfile)
-    subprocess.check_call("sudo /usr/local/nginx/sbin/nginx -c " + home + "/FrameworkBenchmarks/mojolicious/nginx.conf", shell=True, stderr=errfile, stdout=logfile)
+    # os.environ["MOJO_MODE"] = "production"
+    subprocess.Popen("hypnotoad ./app.pl", shell=True, cwd="mojolicious", stderr=errfile, stdout=logfile)
     return 0
   except subprocess.CalledProcessError:
     return 1
+
 def stop(logfile, errfile):
   try:
-    subprocess.call("sudo /usr/local/nginx/sbin/nginx -s stop", shell=True, stderr=errfile, stdout=logfile)
+    subprocess.call("hypnotoad -s ./app.pl", shell=True, cwd="mojolicious", stderr=errfile, stdout=logfile)
     p = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE)
     out, err = p.communicate()
     for line in out.splitlines():
-      if 'starman' in line:
+      if 'hypnotoad' in line:
         pid = int(line.split(None, 2)[1])
         os.kill(pid, 15)
     return 0