2 # -----------------------------------------------------------------------
3 # Copyright (C) 2008 Equinox Software, Inc.
4 # Bill Erickson <erickson@esilibrary.com>
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 # -----------------------------------------------------------------------
23 Provides an environment for managing OpenSRF services written in Python
26 import sys, getopt, os, signal
27 import osrf.system, osrf.server, osrf.app, osrf.set, osrf.json
31 Print help for the OpenSRF Python application process manager
35 Manage OpenSRF application processes
39 start -- Start a service
40 stop -- stop a service
41 restart -- restart a service
42 start_all -- Start all services
43 stop_all -- Stop all services
44 restart_all -- Restart all services
50 The OpenSRF config file
53 The OpenSRF config file context
56 The location of application PID files. Default is /tmp
59 If set, run in daemon (background) mode. This creates a PID
60 file for managing the process.
63 If set, run in 'localhost' mode
70 def get_pid_file(service):
72 Return the PID file for the named service
75 return "%s/%s.pid" % (pid_dir, service)
79 Initialize the Python service environment
85 # connect to the OpenSRF network
86 osrf.system.System.net_connect(
87 config_file = config_file, config_context = config_ctx)
92 domain = osrf.conf.get('domain')
96 settings = osrf.set.get('apps')
98 for key in settings.keys():
100 if isinstance(svc, dict) and 'language' in svc and svc['language'] == 'python':
104 def do_start(service):
106 Start the named Python service
109 pidfile = get_pid_file(service)
111 if service not in services:
112 print "* service %s is not a 'python' application" % service
115 if os.path.exists(pidfile):
116 print "* service %s already running" % service
119 print "* starting %s" % service
123 if osrf.system.System.daemonize(False):
124 return # parent process returns
127 pid_fd = open(pidfile, 'w')
128 pid_fd.write(str(os.getpid()))
131 svc_settings = services[service]
133 osrf.app.Application.load(service, svc_settings['implementation'])
134 osrf.app.Application.register_sysmethods()
135 osrf.app.Application.application.global_init()
137 controller = osrf.server.Controller(service)
138 controller.max_requests = svc_settings['unix_config']['max_requests']
139 controller.max_children = svc_settings['unix_config']['max_children']
140 controller.min_children = svc_settings['unix_config']['min_children']
141 controller.keepalive = svc_settings['keepalive']
148 Start all Python services listed in the OpenSRF configuration file
151 # You can't start more than one service without daemonizing
155 print "* starting all services for %s " % domain
156 for service in services.keys():
161 Stop all Python services listed in the OpenSRF configuration file
164 print "* stopping all services for %s " % domain
165 for service in services.keys():
168 def do_stop(service):
170 Stop the named Python service
173 pidfile = get_pid_file(service)
175 if not os.path.exists(pidfile):
176 print "* %s is not running" % service
179 print "* stopping %s" % service
181 pid_fd = open(pidfile)
185 os.kill(int(pid), signal.SIGTERM)
190 # -----------------------------------------------------
192 # Parse the command line options
193 ops, args = None, None
195 ops, args = getopt.getopt(sys.argv[1:], 'a:s:f:c:p:dhl')
196 except getopt.GetoptError, e:
197 print '* %s' % str(e)
202 if '-a' not in options:
205 action = options['-a']
207 config_file = options.get('-f', '@CONF_DIR@/opensrf_core.xml')
208 pid_dir = options.get('-p', '@PID_DIR@/run/opensrf')
210 service_name = options.get('-s')
211 config_ctx = options.get('-c', 'config.opensrf')
212 as_localhost = '-l' in options
213 as_daemon = '-d' in options
221 if action == 'start':
222 do_start(service_name)
224 elif action == 'stop':
225 do_stop(service_name)
227 elif action == 'restart':
228 do_stop(service_name)
229 do_start(service_name)
231 elif action == 'start_all':
234 elif action == 'stop_all':
237 elif action == 'restart_all':
241 elif action == 'help':