1 # -----------------------------------------------------------------------
2 # Copyright (C) 2008 Equinox Software, Inc.
3 # Bill Erickson <erickson@esilibrary.com>
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 # -----------------------------------------------------------------------
21 import os, sys, threading, logging, fcntl, socket, errno, signal, time
22 import osrf.log, osrf.conf, osrf.net, osrf.system, osrf.stack, osrf.app
25 # used to define the size of the PID/size leader in
26 # status and data messages passed to and from children
29 class Controller(object):
31 OpenSRF forking request server.
34 def __init__(self, service):
35 self.service = service # service name
36 self.application = None
37 self.max_requests = 0 # max child requests
38 self.max_children = 0 # max num of child processes
39 self.min_childen = 0 # min num of child processes
40 self.num_children = 0 # current num children
41 self.child_idx = 0 # current index into the children array
42 self.children = [] # list of children
43 self.osrf_handle = None # xmpp handle
44 self.routers = [] # list of registered routers
46 # Global status socketpair. All children relay their
47 # availability info to the parent through this socketpair.
48 self.read_status, self.write_status = socket.socketpair()
51 settings = osrf.set.get('activeapps.%s' % self.service)
55 ''' Closes management sockets, kills children, reaps children, exits '''
57 osrf.log.log_info("Shutting down...")
58 self.cleanup_routers()
60 self.read_status.shutdown(socket.SHUT_RDWR)
61 self.write_status.shutdown(socket.SHUT_RDWR)
62 self.read_status.close()
63 self.write_status.close()
65 for child in self.children:
66 child.read_data.shutdown(socket.SHUT_RDWR)
67 child.write_data.shutdown(socket.SHUT_RDWR)
68 child.read_data.close()
69 child.write_data.close()
71 os.kill(0, signal.SIGKILL)
72 self.reap_children(True)
76 def handle_signals(self):
77 ''' Installs SIGINT and SIGTERM handlers '''
78 def handler(signum, frame):
80 signal.signal(signal.SIGINT, handler)
81 signal.signal(signal.SIGTERM, handler)
86 osrf.net.clear_network_handle()
90 time.sleep(.5) # give children a chance to connect before we start taking data
91 self.osrf_handle = osrf.system.System.net_connect(resource = '%s_listener' % self.service)
93 # clear the recv callback so inbound messages do not filter through the opensrf stack
94 self.osrf_handle.receive_callback = None
96 # connect to our listening routers
97 self.register_routers()
100 osrf.log.log_debug("entering main server loop...")
101 while True: # main server loop
105 data = self.osrf_handle.recv(-1).to_xml()
107 if self.try_avail_child(data):
110 if self.try_new_child(data):
113 self.wait_for_child()
115 except KeyboardInterrupt:
117 #except Exception, e:
118 #osrf.log.log_error("server exiting with exception: %s" % e.message)
122 def try_avail_child(self, data):
123 ''' Trys to send current request data to an available child process '''
125 while ctr < self.num_children:
127 if self.child_idx >= self.num_children:
129 child = self.children[self.child_idx]
132 osrf.log.log_internal("sending data to available child")
133 self.write_child(child, data)
140 def try_new_child(self, data):
141 ''' Tries to spawn a new child to send request data to '''
142 if self.num_children < self.max_children:
143 osrf.log.log_internal("spawning new child to handle data")
144 child = self.spawn_child()
145 self.write_child(child, data)
149 def try_wait_child(self, data):
150 ''' Waits for a child to become available '''
151 osrf.log.log_warn("No children available, waiting...")
152 child = self.check_status(True)
153 self.write_child(child, data)
156 def write_child(self, child, data):
157 ''' Sends data to the child process '''
158 child.available = False
159 child.write_data.sendall(str(len(data)).rjust(SIZE_PAD) + data)
163 def check_status(self, block=False):
164 ''' Checks to see if any children have indicated they are done with
165 their current request. If block is true, this will wait
166 indefinitely for a child to be free. '''
171 pid = self.read_status.recv(SIZE_PAD)
174 self.read_status.setblocking(0)
175 pid = self.read_status.recv(SIZE_PAD)
176 except socket.error, e:
177 if e.args[0] != errno.EAGAIN:
179 self.read_status.setblocking(1)
183 child = [c for c in self.children if c.pid == pid][0]
184 child.available = True
189 def reap_children(self, done=False):
190 ''' Uses waitpid() to reap the children. If necessary, new children are spawned '''
197 (pid, status) = os.waitpid(0, options)
200 self.spawn_children()
202 osrf.log.log_debug("reaping child %d" % pid)
203 self.num_children -= 1
204 self.children = [c for c in self.children if c.pid != pid]
208 def spawn_children(self):
209 ''' Launches up to min_children child processes '''
210 while self.num_children < self.min_children:
213 def spawn_child(self):
214 ''' Spawns a new child process '''
217 child.read_data, child.write_data = socket.socketpair()
218 child.pid = os.fork()
221 self.num_children += 1
222 self.children.append(child)
223 osrf.log.log_debug("spawned child %d : %d total" % (child.pid, self.num_children))
226 child.pid = os.getpid()
231 def register_routers(self):
232 ''' Registers this application instance with all configured routers '''
233 routers = osrf.conf.get('routers.router')
235 if not isinstance(routers, list):
238 for router in routers:
239 if isinstance(router, dict):
240 if not 'services' in router or \
241 self.service in router['services']['service']:
242 target = "%s@%s/router" % (router['name'], router['domain'])
243 self.register_router(target)
245 router_name = osrf.conf.get('router_name')
246 target = "%s@%s/router" % (router_name, router)
247 self.register_router(target)
250 def register_router(self, target):
251 ''' Registers with a single router '''
252 osrf.log.log_info("registering with router %s" % target)
253 self.routers.append(target)
255 reg_msg = osrf.net.NetworkMessage(
257 body = 'registering...',
258 router_command = 'register',
259 router_class = self.service
262 self.osrf_handle.send(reg_msg)
264 def cleanup_routers(self):
265 ''' Un-registers with all connected routers '''
266 for target in self.routers:
267 osrf.log.log_info("un-registering with router %s" % target)
268 unreg_msg = osrf.net.NetworkMessage(
270 body = 'un-registering...',
271 router_command = 'unregister',
272 router_class = self.service
274 self.osrf_handle.send(unreg_msg)
278 ''' Models a single child process '''
280 def __init__(self, controller):
281 self.controller = controller # our Controller object
282 self.num_requests = 0 # how many requests we've served so far
283 self.read_data = None # the child reads data from the controller on this socket
284 self.write_data = None # the controller sends data to the child on this socket
285 self.available = True # true if this child is not currently serving a request
286 self.pid = 0 # my process id
290 ''' Loops, processing data, until max_requests is reached '''
293 size = int(self.read_data.recv(SIZE_PAD) or 0)
294 data = self.read_data.recv(size)
295 osrf.log.log_internal("recv'd data " + data)
296 osrf.stack.push(osrf.net.NetworkMessage.from_xml(data))
297 self.num_requests += 1
298 if self.num_requests == self.controller.max_requests:
301 except KeyboardInterrupt:
303 # run the exit handler
304 osrf.app.Application.application.child_exit()
306 def send_status(self):
307 ''' Informs the controller that we are done processing this request '''
308 fcntl.lockf(self.controller.write_status.fileno(), fcntl.LOCK_EX)
310 self.controller.write_status.sendall(str(self.pid).rjust(SIZE_PAD))
312 fcntl.lockf(self.controller.write_status.fileno(), fcntl.LOCK_UN)
315 ''' Connects the opensrf xmpp handle '''
316 osrf.net.clear_network_handle()
317 osrf.system.System.net_connect(resource = '%s_drone' % self.controller.service)
318 osrf.app.Application.application.child_init()