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
39 The OpenSRF config file
42 The OpenSRF config file context
45 The location of application PID files. Default is /tmp
48 If set, run in daemon (background) mode. This creates a PID
49 file for managing the process.
57 # Parse the command line options
58 ops, args = None, None
60 ops, args = getopt.getopt(sys.argv[1:], 'a:s:f:c:p:dh')
61 except getopt.GetoptError, e:
67 if '-a' not in options or '-s' not in options or '-f' not in options:
70 action = options['-a']
71 service = options['-s']
72 config_file = options['-f']
73 config_ctx = options.get('-c', 'config.opensrf')
74 pid_dir = options.get('-p', '/tmp')
75 as_daemon = '-d' in options
76 pidfile = "%s/osrf_py_%s.pid" % (pid_dir, service)
81 # connect to the OpenSRF network
82 osrf.system.System.net_connect(
83 config_file = config_file, config_context = config_ctx)
85 osrf.set.load(osrf.conf.get('domain'))
86 settings = osrf.json.to_json(osrf.set.get('apps/%s' % service))
88 if settings['language'].lower() != 'python':
89 print '%s is not a Python application' % service
92 # XXX load the settings configs...
93 osrf.app.Application.load(service, 'osrf.apps.example') # XXX example only for now
94 osrf.app.Application.register_sysmethods()
95 osrf.app.Application.application.global_init()
97 controller = osrf.server.Controller(service)
98 controller.max_requests = 100
99 controller.max_children = 6
100 controller.min_children = 3
103 osrf.system.System.daemonize()
104 file = open(pidfile, 'w')
105 file.write(str(os.getpid()))
114 os.kill(int(pid), signal.SIGTERM)
118 if action == 'start':
121 elif action == 'stop':
124 elif action == 'restart':
128 elif action == 'help':