[Erp5-report] r11460 - /erp5/trunk/utils/oood/runserw.py

nobody at svn.erp5.org nobody at svn.erp5.org
Thu Nov 23 17:08:43 CET 2006


Author: kevin
Date: Thu Nov 23 17:08:42 2006
New Revision: 11460

URL: http://svn.erp5.org?rev=11460&view=rev
Log:
Use switch parameters to start and stop server.
Add required methods to manage server startup and stop nicely.
Add methods to show server status and statistics (not finished yet).

Modified:
    erp5/trunk/utils/oood/runserw.py

Modified: erp5/trunk/utils/oood/runserw.py
URL: http://svn.erp5.org/erp5/trunk/utils/oood/runserw.py?rev=11460&r1=11459&r2=11460&view=diff
==============================================================================
--- erp5/trunk/utils/oood/runserw.py (original)
+++ erp5/trunk/utils/oood/runserw.py Thu Nov 23 17:08:42 2006
@@ -28,54 +28,156 @@
 #
 ##############################################################################
 
-import sys
-import atexit
-from serw import *
+import getopt, sys, os, signal, lib
+
+
+
+def showServerStatus():
+  """
+    Show Server details and statistics.
+  """
+  # TODO: must be done (!)
+  print "===== Server Statistics ====="
+  print "Status: %s" % "TODO"
+  print "Uptime: %s" % "TODO"
+  print "Auto-reboot since launch: %s" % "TODO"
+
+
+def usage():
+  print """Usage: %s [options]
+  --start
+      Start the server.
+  -s, --stop
+      Stop the server.
+  --status, --stat
+      Show server statistics and status.
+  -d, --debug
+      Start the server in debug mode.
+  -h, --help
+      Show this screen.
+""" % sys.argv[0]
+
 
 
 if __name__ == '__main__':
 
-  # Create a lock file to not run more than one server and to save the pid
-  lock_file_path = os.path.join(config.pid_dir, 'server_pid.lock')
-  if os.path.exists(lock_file_path):
-    lib.log("Server - Can't start a new server, there is already one running", 2)
-    sys.exit(1)
-  lock_file = open(lock_file_path, 'w')
-  server_pid = os.getpid()
-  lock_file.write(str(server_pid))
-  lock_file.close()
-  lib.log("Server - Started as pid %s" % server_pid, 0)
+  # Parse options and parameters
+  try:
+    opts, args = getopt.getopt( sys.argv[1:]
+                              , "hd"
+                              , ["help", "stop", "status", "stat", "start", "debug"]
+                              )
+    # Check args number
+    if len(opts) == 0:
+      raise getopt.GetoptError('', '')
+  except getopt.GetoptError:
+    # Print help information and exit on command line error
+    usage()
+    sys.exit(2)
 
-  # Clean the lock file before exit
-  def cleanLock(file=lock_file_path):
-    if os.path.exists(file):
-      os.remove(file)
-  atexit.register(cleanLock)
+  # Initialize default variables
+  debug = False
+  start = False
+  stop  = False
 
-  # Start the server core
-  proc = Procesor()
+  # Start action according parameters
+  for o, a in opts:
+    if o in ("-h", "--help"):
+      usage()
+      sys.exit(0)
+    elif o in ("--status", "--stat"):
+      showServerStatus()
+      sys.exit(0)
+    elif o in ("-d", "--debug"):
+      debug = True
+    elif o in ("--stop"):
+      stop = True
+    elif o in ("--start"):
+      start = True
 
-  # Debug mode or not ?
-  try:
-    debug = (sys.argv[1] == 'debug')
-  except IndexError:
-    debug = False
-  if debug:
-    lib.log("Server - Started in debug mode", 1)
 
-  # Some debug stuff
-  if debug:
-    d = open('test.odt').read()
-    print proc.run_makepdf('test.odt', base64.encodestring(d))[0]
-    #d=open('doc/test.doc').read()
-    #print proc.run_setmetadata('test.odt',base64.encodestring(d),{'title':'zz'})[0]
+  if stop:
+    #########################################################
+    ## Use the lock/pid file to terminate the server.
+    #########################################################
+    sys.path.append('/etc/oood/') # Big ad-hoc hack
+    import config
+    lock_file_path = os.path.join(config.pid_dir, 'server_pid.lock')
+    # Get the server process id which is written in the lock file.
+    if not os.path.exists(lock_file_path):
+      lib.log("Server - Can't stop server: it was not running !", 1)
+      sys.exit(1)
+    lock_file = open(lock_file_path, 'r')
+    server_pid = int(lock_file.read())
+    lock_file.close()
+    os.kill(server_pid, signal.SIGTERM)
+    lib.log("Server - Stopped properly", 0)
     sys.exit(0)
 
-  # Run the server forever
-  ser = MySerw((config.server_host, config.server_port), allow_none = True)
-  ser.register_instance(proc)
-  ser.serve_forever()
 
-  # Clean up before exit
-  cleanLock()
-  sys.exit(0)
+  elif start or debug:
+    #########################################################
+    ## Run the XML-RPC server
+    #########################################################
+    from serw import *
+
+    def cleanAndExit():
+      """
+        Remove the lock file and exit cleanly.
+      """
+      import config
+      lib.log("Server - Remove lock file and stop server", 0)
+      lock_file_path = os.path.join(config.pid_dir, 'server_pid.lock')
+      if os.path.exists(lock_file_path):
+        os.remove(lock_file_path)
+      sys.exit(0)
+
+    def signalHandler(signum, frame):
+      """
+        Catch signal to stop the server
+      """
+      lib.log("Server - Stopped by signal %s" % signum, 0)
+      cleanAndExit()
+
+    if debug:
+      lib.log("Server - Started in debug mode", 1)
+
+    # Some signals signify a stop request
+    signal.signal(signal.SIGINT,  signalHandler)
+    signal.signal(signal.SIGTERM, signalHandler)
+
+    # Create a lock file to not run more than one server and to save the pid
+    lock_file_path = os.path.join(config.pid_dir, 'server_pid.lock')
+    if os.path.exists(lock_file_path):
+      lib.log("Server - Can't start a new server: another one is already started or the previous server wasn't properly stopped", 2)
+      sys.exit(1)
+    lock_file = open(lock_file_path, 'w')
+    server_pid = os.getpid()
+    lock_file.write(str(server_pid))
+    lock_file.close()
+    lib.log("Server - Started as process %s" % server_pid, 0)
+
+    # Start the server core
+    proc = Procesor()
+
+    # Some debug stuff
+    if debug:
+      d = open('test.odt').read()
+      print proc.run_makepdf('test.odt', base64.encodestring(d))[0]
+      #d=open('doc/test.doc').read()
+      #print proc.run_setmetadata('test.odt',base64.encodestring(d),{'title':'zz'})[0]
+      cleanAndExit()
+
+    # Run the server forever
+    ser = MySerw((config.server_host, config.server_port), allow_none = True)
+    ser.register_instance(proc)
+    lib.log("Server - Will serve indefinitely until you kill or interrupt it...", 0)
+    ser.serve_forever()
+
+    # Clean up and exit
+    cleanAndExit()
+
+
+  # Exit on general error
+  usage()
+  sys.exit(1)




More information about the Erp5-report mailing list