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 # -----------------------------------------------------------------------
22 import sys, getopt, os, signal
23 import osrf.system, osrf.server, osrf.app, osrf.set, osrf.json
27 Manage OpenSRF application processes
31 start -- Start a service
32 stop -- stop a service
33 restart -- restart a service
34 start_all -- Start all services
35 stop_all -- Stop all services
36 restart_all -- Restart all services
42 The OpenSRF config file
45 The OpenSRF config file context
48 The location of application PID files. Default is /tmp
51 If set, run in daemon (background) mode. This creates a PID
52 file for managing the process.
55 If set, run in 'localhost' mode
63 # Parse the command line options
64 ops, args = None, None
66 ops, args = getopt.getopt(sys.argv[1:], 'a:s:f:c:p:dhl')
67 except getopt.GetoptError, e:
73 if '-a' not in options or '-f' not in options:
76 action = options['-a']
77 config_file = options['-f']
78 pid_dir = options['-p']
80 service = options.get('-s')
81 config_ctx = options.get('-c', 'config.opensrf')
82 as_localhost = '-l' in options
83 as_daemon = '-d' in options
90 def get_pid_file(service):
91 return "%s/%s.pid" % (pid_dir, service)
97 # connect to the OpenSRF network
98 osrf.system.System.net_connect(
99 config_file = config_file, config_context = config_ctx)
104 domain = osrf.conf.get('domain')
106 osrf.set.load(domain)
108 settings = osrf.set.get('apps')
110 for key in settings.keys():
112 if isinstance(svc, dict) and svc['language'] == 'python':
116 def do_start(service):
118 pidfile = get_pid_file(service)
120 if service not in services:
121 print "* service %s is not a 'python' application" % service
124 if os.path.exists(pidfile):
125 print "* service %s already running" % service
128 print "* starting %s" % service
132 if osrf.system.System.daemonize(False):
133 return # parent process returns
136 file = open(pidfile, 'w')
137 file.write(str(os.getpid()))
140 settings = services[service];
142 osrf.app.Application.load(service, settings['implementation'])
143 osrf.app.Application.register_sysmethods()
144 osrf.app.Application.application.global_init()
146 controller = osrf.server.Controller(service)
147 controller.max_requests = settings['unix_config']['max_requests']
148 controller.max_children = settings['unix_config']['max_children']
149 controller.min_children = settings['unix_config']['min_children']
150 controller.keepalive = settings['keepalive']
156 print "* starting all services for %s " % domain
157 for service in services.keys():
161 print "* stopping all services for %s " % domain
162 for service in services.keys():
165 def do_stop(service):
166 pidfile = get_pid_file(service)
168 if not os.path.exists(pidfile):
169 print "* %s is not running" % service
172 print "* stopping %s" % service
178 os.kill(int(pid), signal.SIGTERM)
183 # -----------------------------------------------------
187 if action == 'start':
190 elif action == 'stop':
193 elif action == 'restart':
197 elif action == 'start_all':
200 elif action == 'stop_all':
203 elif action == 'restart_all':
207 elif action == 'help':