[Erp5-report] r11425 - /erp5/trunk/utils/oood/start.py
nobody at svn.erp5.org
nobody at svn.erp5.org
Wed Nov 22 12:16:53 CET 2006
Author: kevin
Date: Wed Nov 22 12:16:52 2006
New Revision: 11425
URL: http://svn.erp5.org?rev=11425&view=rev
Log:
Add command line parameters support to manage the Pool.
Add help message.
Add new method to stop all OOo instances.
Add new generc method to properly kill instance.
Add new method to show Pool status and statistics (some work is still needed).
Use flexible params to launch OOo.
Add lots of verbose logs.
Add new method to print consistent message in log.
Update info strings.
Update copyright.
Modified:
erp5/trunk/utils/oood/start.py
Modified: erp5/trunk/utils/oood/start.py
URL: http://svn.erp5.org/erp5/trunk/utils/oood/start.py?rev=11425&r1=11424&r2=11425&view=diff
==============================================================================
--- erp5/trunk/utils/oood/start.py (original)
+++ erp5/trunk/utils/oood/start.py Wed Nov 22 12:16:52 2006
@@ -2,7 +2,8 @@
##############################################################################
#
# Copyright (c) 2002, 2006 Nexedi SARL and Contributors. All Rights Reserved.
-# Jean-Paul Smets-Solanes <jp at nexedi.com>
+# Jean-Paul Smets-Solanes <jp at nexedi.com>
+# Kevin Deldycke <kevin_AT_nexedi_DOT_com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
@@ -29,56 +30,150 @@
import os, sys
# Add oood home (= current path) as a place to look for import
+# XXX Is this magical things make oood_home parameter useless in oood config file ?
sys.path.append(os.path.abspath(os.getcwd()))
-import time
-import lib
-import config
+import getopt, time # Standard python libs
+import lib, config # ood python libs
-'''
- Starts OO instances, number defined in config under the 'pool_size' parameter.
- First, you must install OO 2.0.3 from RPMs.
+"""
+ Starts OOo instances to populate the pool.
+ Size of the pool is defined in config under the 'pool_size' parameter.
Then user directories user_0, user_1 etc. will be created automatically
- by OOo on startup.
-'''
+ by OOo on startup in the ~/tmp folder of the running user.
+ TODO: use lock file (like /var/run/oood/pool.lock) for proper pool startup managment.
+"""
-cmdtpl = '%s/soffice'
-arg1 = '-headless'
-argtpl2 = '-accept=socket,host=%s,port=%d;urp;'
-argtpl3 = '-env:UserInstallation=$SYSUSERCONFIG/./tmp/oood_instance_%d'
-#argtpl3 = '-env:UserInstallation=%s./user_%d'
-arg4 = '-nologo'
-arg5 = '-nodefault'
-arg6 = '-norestore'
+def _l(msg, instance_id, level):
+ """
+ This method put instance related messages in the log
+ """
+ LOG_HEAD = "Instance #%s - " % instance_id
+ lib.log('%s%s' % (LOG_HEAD, msg), level)
def startInstance(i):
"""
We spawn a new process and record its pid to be able to kill it if necessary.
"""
+ _l("Starting...", i, 0)
instance_port = config.pool_port_range_start + i
- print str(instance_port)
- #lib.log('starting OOo for i='+str(i))
- cmd = cmdtpl % config.uno_path
- arg2 = argtpl2 % (config.pool_host, instance_port)
- arg3 = argtpl3 % i
-# arg3 = argtpl3 % (config.pid_dir, i)
- print arg3
- pid = os.spawnlp(os.P_NOWAIT, cmd, 'soffice', arg1, arg2, arg3, arg4, arg5, arg6)
- pidfile = '%s/instance_%d.pid' % (config.pid_dir, i)
+ cmd = '%s/soffice' % config.uno_path
+ params = [ '-headless'
+ , '-accept=socket,host=%s,port=%d;urp;' % (config.pool_host, instance_port)
+ , '-env:UserInstallation=$SYSUSERCONFIG/./tmp/oood_instance_%d' % i
+ #, '-env:UserInstallation=%s/./oood_instance_%d' % (config.pid_dir, i)
+ , '-nologo'
+ , '-nodefault'
+ , '-norestore'
+ ]
+ pid = os.spawnvp(os.P_NOWAIT, cmd, params)
+ _l("Listening at %s:%s" % (config.pool_host, instance_port), i, 0)
+ pidfile = os.path.join(config.pid_dir, 'instance_%d.pid' % i)
open(pidfile, 'w').write(str(pid))
- #lib.log('OOo started, pid '+str(pid))
+ _l("has pid %s" % pid, i, 0)
+
+
+def killInstance(i):
+ _l("Kill requested", i, 1)
+ pid_file_path = os.path.join(config.pid_dir, 'instance_%d.pid' % i)
+ if os.path.exists(pid_file_path):
+ _l("Is still running or was not properly shutdown: Kill it", i, 0)
+ pid_file = open(pid_file_path, 'r')
+ instance_master_pid = int(pid_file.read())
+ pid_file.close()
+ # OOo instance span on several process, find all of them
+ pid_list = os.popen('ps -A -o pid,ppid').read().split('\n')
+ instance_pid_list = []
+ for pid in pid_list:
+ if pid.find('%s' % instance_master_pid) != -1:
+ instance_pid_list.append(int(pid.strip().split(' ')[0]))
+ if len(instance_pid_list) > 0:
+ _l("Span on several processes: %s" % ', '.join([str(x) for x in instance_pid_list]), i, 0)
+ # Kill all instance processes
+ for pid in instance_pid_list:
+ _l("Killing pid %s" % pid, i, 1)
+ os.kill(pid, 9)
+ os.remove(pid_file_path)
+ _l("Killed", i, 0)
+ else:
+ _l("Was not running or didn't exist, so no need to kill", i, 0)
+
def startAll():
- os.system('killall soffice.bin')
- os.system('killall soffice')
+ lib.log("Pool - Start all %s instances" % config.pool_size, 0)
+ stopAll()
for i in range(config.pool_size):
startInstance(i)
time.sleep(config.instance_load_time)
+ lib.log("Pool - All %s instances started" % config.pool_size, 0)
+
+
+def stopAll():
+ lib.log("Pool - Stop all %s instances" % config.pool_size, 0)
+ for i in range(config.pool_size):
+ killInstance(i)
+ lib.log("Pool - All %s instances flushed away" % config.pool_size, 0)
+
+
+def showStatus():
+ """
+ Show Pool details and statistics.
+ """
+ # TODO: dynamic verbose message
+ print "Pool:"
+ print " * Status: %s" % 'Started' #(stopped)
+ print " * Size: %s" % config.pool_size
+ print " * Uptime: %s" % 'unknown'
+ print " * OOo instance running: %s" % config.pool_size
+ print " * OOo instance stopped: %s" % 0
+ print " * OOo instance automaticcaly restarted: %s" % 0
+
+
+def usage():
+ print """Usage: %s [options]
+ -i, --init
+ Kill all previous OOo instances in the pool (if any) and re-populate it.
+ -f, --flush
+ Kill all OOo instances in the pool.
+ -s, --status, --stat
+ Show Pool statistics and status.
+ -h, --help
+ Show this screen.
+""" % sys.argv[0]
+
if __name__=='__main__':
- startAll()
+ # Get all parameters
+ try:
+ opts, args = getopt.getopt(sys.argv[1:], "hifs", ["help", "init", "flush", "status", "stat"])
+ except getopt.GetoptError:
+ # Print help information and exit on command line error
+ usage()
+ sys.exit(2)
-# vim: shiftwidth=2
+ # Start action according parameters
+ for o, a in opts:
+ if o in ("-h", "--help"):
+ usage()
+ sys.exit(0)
+ if o in ("-i", "--init"):
+ startAll()
+ sys.exit(0)
+ if o in ("-f", "--flush"):
+ stopAll()
+ sys.exit(0)
+ if o in ("-s", "--status", "--stat"):
+ showStatus()
+ sys.exit(0)
+
+ # Check args number
+ if len(opts) == 0:
+ usage()
+ # Exit on command line error
+ sys.exit(2)
+
+ # Exit on general error
+ sys.exit(1)
More information about the Erp5-report
mailing list