]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/python/opensrf.py
implemented the majority of server-side python. still need to add settings server...
[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
24
25 def help():
26     print '''
27     Manage one or more an OpenSRF applications
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
49     '''
50     sys.exit(0)
51
52
53 # Parse the command line options
54 ops, args = getopt.getopt(sys.argv[1:], 'a:s:f:c:p:d')
55 options = dict(ops)
56
57 if '-a' not in options or '-s' not in options or '-f' not in options:
58     help()
59
60 action = options['-a']
61 service = options['-s']
62 config_file = options['-f']
63 config_ctx = options.get('-c', 'opensrf')
64 pid_dir = options.get('-p', '/tmp')
65 as_daemon = '-d' in options
66 pidfile = "%s/osrf_py_%s.pid" % (pid_dir, service)
67
68
69 if action == 'start':
70
71     # connect to the OpenSRF network
72     osrf.system.System.net_connect(
73         config_file = config_file, config_context = config_ctx)
74
75     # XXX load the settings configs...
76     osrf.app.Application.load(service, 'osrf.apps.example') # XXX example only for now
77     osrf.app.Application.register_sysmethods()
78     osrf.app.Application.application.global_init()
79
80     controller = osrf.server.Controller(service)
81     controller.max_requests = 10
82     controller.max_children = 6
83     controller.min_children = 3
84
85     if as_daemon:
86         osrf.system.System.daemonize()
87         file = open(pidfile, 'w')
88         file.write(str(os.getpid()))
89         file.close()
90
91     controller.run()
92
93 elif action == 'stop':
94     file = open(pidfile)
95     pid = file.read()
96     file.close()
97     os.kill(int(pid), signal.SIGTERM)
98     os.remove(pidfile)
99
100