]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/python/opensrf.py
initial steps toward OS X portage
[OpenSRF.git] / src / python / opensrf.py
1 #!/usr/bin/python
2 # -----------------------------------------------------------------------
3 # Copyright (C) 2008  Equinox Software, Inc.
4 # Bill Erickson <erickson@esilibrary.com>
5 #
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.
10 #
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.
15 #
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
19 # 02110-1301, USA
20 # -----------------------------------------------------------------------
21
22 import sys, getopt, os, signal
23 import osrf.system, osrf.server, osrf.app, osrf.set, osrf.json
24
25 def do_help():
26     print '''
27     Manage OpenSRF application processes
28
29     Options:
30         -a <action>
31             start   -- Start a service
32             stop    -- stop a service
33             restart -- restart a service
34
35         -s <service>
36             The service name
37
38         -f <config file>
39             The OpenSRF config file
40
41         -c <config context>
42             The OpenSRF config file context
43
44         -p <PID dir>
45             The location of application PID files.  Default is /tmp
46
47         -d 
48             If set, run in daemon (background) mode.  This creates a PID 
49             file for managing the process.
50
51         -h
52             Prints help message
53     '''
54     sys.exit(0)
55
56
57 # Parse the command line options
58 ops, args = None, None
59 try:
60     ops, args = getopt.getopt(sys.argv[1:], 'a:s:f:c:p:dh')
61 except getopt.GetoptError, e:
62     print '* %s' % str(e)
63     do_help()
64
65 options = dict(ops)
66
67 if '-a' not in options or '-s' not in options or '-f' not in options:
68     do_help()
69
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)
77
78
79 def do_start():
80
81     # connect to the OpenSRF network
82     osrf.system.System.net_connect(
83         config_file = config_file, config_context = config_ctx)
84
85     osrf.set.load(osrf.conf.get('domain'))
86     settings = osrf.json.to_json(osrf.set.get('apps/%s' % service))
87
88     if settings['language'].lower() != 'python':
89         print '%s is not a Python application' % service
90         return
91
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()
96
97     controller = osrf.server.Controller(service)
98     controller.max_requests = 100
99     controller.max_children = 6
100     controller.min_children = 3
101
102     if as_daemon:
103         osrf.system.System.daemonize()
104         file = open(pidfile, 'w')
105         file.write(str(os.getpid()))
106         file.close()
107
108     controller.run()
109
110 def do_stop():
111     file = open(pidfile)
112     pid = file.read()
113     file.close()
114     os.kill(int(pid), signal.SIGTERM)
115     os.remove(pidfile)
116
117
118 if action == 'start':
119     do_start()
120
121 elif action == 'stop':
122     do_stop()
123
124 elif action == 'restart':
125     do_stop()
126     do_start()
127
128 elif action == 'help':
129     do_help()