setup.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import os
  2. import sys
  3. import time
  4. import setup_util
  5. import subprocess
  6. import multiprocessing
  7. def get_env_for_database(args):
  8. if args.database == 'MySQL':
  9. driver = 'mysql'
  10. dbname = 'hello_world'
  11. else:
  12. driver = 'sqlite'
  13. dbname = os.environ['ULIB_ROOT'] + '/db/%.*s'
  14. return {
  15. 'UMEMPOOL': '135,0,0,34,8465,129,-17,-22,41',
  16. 'ORM_DRIVER': driver,
  17. 'ORM_OPTION': "host=" + args.database_host + " user=benchmarkdbuser password=benchmarkdbpass character-set=utf8 dbname=" + dbname
  18. }
  19. def start(args, logfile, errfile):
  20. try:
  21. ulib_root = os.environ['ULIB_ROOT']
  22. fcfg = ulib_root + "/benchmark.cfg"
  23. fprg = ulib_root + "/bin/userver_tcp"
  24. # 1. Change ULib Server configuration
  25. setup_util.replace_text(fcfg, "PREFORK_CHILD .*", "PREFORK_CHILD " + str(multiprocessing.cpu_count()))
  26. # 2. Start ULib Server (userver_tcp)
  27. subprocess.Popen( "%s -c %s" % (fprg, fcfg), shell=True, stdout=logfile, stderr=errfile, env = get_env_for_database(args))
  28. return 0
  29. except subprocess.CalledProcessError:
  30. return 1
  31. def stop(logfile, errfile):
  32. try:
  33. logfile.write( "ULib: setup.py STOP\n")
  34. # Stop ULib Server (userver_tcp)
  35. subprocess.check_call("kill -TERM $( cat $ULIB_ROOT/userver_tcp.pid )", shell=True, stderr=errfile, stdout=logfile)
  36. time.sleep(3);
  37. p = subprocess.Popen(['pgrep', 'userver_tcp'], stdout=subprocess.PIPE)
  38. out, err = p.communicate()
  39. for line in out.splitlines():
  40. if 'userver_tcp' in line:
  41. pid = int(line.split(None, 2)[1])
  42. os.kill(pid, 9)
  43. subprocess.call("rm -f $ULIB_ROOT/userver_tcp.pid", shell=True, stderr=errfile, stdout=logfile)
  44. return 0
  45. except subprocess.CalledProcessError:
  46. return 1