1 # ----------------------------------------------------------------
2 # Copyright (C) 2010 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.
14 # ----------------------------------------------------------------
15 package OpenSRF::Server;
18 use OpenSRF::Transport;
19 use OpenSRF::Application;
20 use OpenSRF::Utils::Config;
21 use OpenSRF::Transport::PeerHandle;
22 use OpenSRF::Utils::SettingsClient;
23 use OpenSRF::Utils::Logger qw($logger);
24 use OpenSRF::Transport::SlimJabber::Client;
25 use POSIX qw/:sys_wait_h :errno_h/;
26 use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
29 our $chatty = 1; # disable for production
31 use constant STATUS_PIPE_DATA_SIZE => 12;
34 my($class, $service, %args) = @_;
35 my $self = bless(\%args, $class);
37 $self->{service} = $service; # service name
38 $self->{num_children} = 0; # number of child processes
39 $self->{osrf_handle} = undef; # xmpp handle
40 $self->{routers} = []; # list of registered routers
41 $self->{active_list} = []; # list of active children
42 $self->{idle_list} = []; # list of idle children
44 $self->{min_spare_children} ||= 0;
46 $self->{max_spare_children} = $self->{min_spare_children} + 1 if
47 $self->{max_spare_children} and
48 $self->{max_spare_children} <= $self->{min_spare_children};
53 # ----------------------------------------------------------------
54 # Disconnects from routers and waits for child processes to exit.
55 # ----------------------------------------------------------------
60 $logger->info("server: shutting down and cleaning up...");
62 # don't get sidetracked by signals while we're cleaning up.
63 # it could result in unexpected behavior with list traversal
64 $SIG{CHLD} = 'IGNORE';
66 # terminate the child processes
67 $self->kill_child($_) for
68 (@{$self->{idle_list}}, @{$self->{active_list}});
71 $self->unregister_routers;
73 $self->{osrf_handle}->disconnect;
75 # clean up our dead children
76 $self->reap_children(1);
78 exit(0) unless $no_exit;
82 # ----------------------------------------------------------------
83 # Waits on the jabber socket for inbound data from the router.
84 # Each new message is passed off to a child process for handling.
85 # At regular intervals, wake up for min/max spare child maintenance
86 # ----------------------------------------------------------------
90 $logger->set_service($self->{service});
92 $SIG{$_} = sub { $self->cleanup; } for (qw/INT TERM QUIT/);
93 $SIG{CHLD} = sub { $self->reap_children(); };
95 $self->spawn_children;
96 $self->build_osrf_handle;
97 $self->register_routers;
104 $self->{child_died} = 0;
106 my $msg = $self->{osrf_handle}->process($wait_time);
108 # we woke up for any reason, reset the wait time to allow
109 # for idle maintenance as necessary
114 if(my $child = pop(@{$self->{idle_list}})) {
116 # we have an idle child to handle the request
117 $chatty and $logger->internal("server: passing request to idle child $child");
118 push(@{$self->{active_list}}, $child);
119 $self->write_child($child, $msg);
121 } elsif($self->{num_children} < $self->{max_children}) {
123 # spawning a child to handle the request
124 $chatty and $logger->internal("server: spawning child to handle request");
125 $self->write_child($self->spawn_child(1), $msg);
129 $logger->warn("server: no children available, waiting...");
130 $self->check_status(1); # block until child is available
132 my $child = pop(@{$self->{idle_list}});
133 push(@{$self->{active_list}}, $child);
134 $self->write_child($child, $msg);
139 # don't perform idle maint immediately when woken by SIGCHLD
140 unless($self->{child_died}) {
142 # when we hit equilibrium, there's no need for regular
143 # maintenance, so set wait_time to 'forever'
144 $wait_time = -1 unless $self->perform_idle_maintenance;
150 # ----------------------------------------------------------------
151 # Launch a new spare child or kill an extra spare child. To
152 # prevent large-scale spawning or die-offs, spawn or kill only
153 # 1 process per idle maintenance loop.
154 # Returns true if any idle maintenance occurred, 0 otherwise
155 # ----------------------------------------------------------------
156 sub perform_idle_maintenance {
159 # spawn 1 spare child per maintenance loop if necessary
160 if( $self->{min_spare_children} and
161 $self->{num_children} < $self->{max_children} and
162 scalar(@{$self->{idle_list}}) < $self->{min_spare_children} ) {
164 $chatty and $logger->internal("server: spawning spare child");
168 # kill 1 excess spare child per maintenance loop if necessary
169 } elsif($self->{max_spare_children} and
170 $self->{num_children} > $self->{min_children} and
171 scalar(@{$self->{idle_list}}) > $self->{max_spare_children} ) {
173 $chatty and $logger->internal("server: killing spare child");
183 my $child = shift || pop(@{$self->{idle_list}}) or return;
184 $chatty and $logger->internal("server: killing child $child");
185 kill('TERM', $child->{pid});
188 # ----------------------------------------------------------------
189 # Jabber connection inbound message arrive on.
190 # ----------------------------------------------------------------
191 sub build_osrf_handle {
194 my $conf = OpenSRF::Utils::Config->current;
195 my $username = $conf->bootstrap->username;
196 my $password = $conf->bootstrap->passwd;
197 my $domain = $conf->bootstrap->domain;
198 my $port = $conf->bootstrap->port;
199 my $resource = $self->{service} . '_listener_' . $conf->env->hostname;
201 $logger->debug("server: inbound connecting as $username\@$domain/$resource on port $port");
203 $self->{osrf_handle} =
204 OpenSRF::Transport::SlimJabber::Client->new(
205 username => $username,
206 resource => $resource,
207 password => $password,
212 $self->{osrf_handle}->initialize;
216 # ----------------------------------------------------------------
217 # Sends request data to a child process
218 # ----------------------------------------------------------------
220 my($self, $child, $msg) = @_;
221 my $xml = $msg->to_xml;
222 syswrite($child->{pipe_to_child}, $xml);
225 # ----------------------------------------------------------------
226 # Checks to see if any child process has reported its availability
227 # In blocking mode, blocks until a child has reported.
228 # ----------------------------------------------------------------
230 my($self, $block) = @_;
232 my $read_set = IO::Select->new;
233 $read_set->add($_->{pipe_to_child}) for @{$self->{active_list}};
235 my @handles = $read_set->can_read(($block) ? undef : 0) or return;
239 for my $pipe (@handles) {
240 sysread($pipe, $pid, STATUS_PIPE_DATA_SIZE) or next;
241 push(@pids, int($pid));
244 $chatty and $logger->internal("server: ".scalar(@pids)." children reporting for duty: (@pids)");
249 # move the children from the active list to the idle list
250 for my $proc (@{$self->{active_list}}) {
251 if(grep { $_ == $proc->{pid} } @pids) {
252 push(@{$self->{idle_list}}, $proc);
254 push(@new_actives, $proc);
258 $self->{active_list} = [@new_actives];
260 $chatty and $logger->internal(sprintf(
261 "server: %d idle and %d active children after status update",
262 scalar(@{$self->{idle_list}}), scalar(@{$self->{active_list}})));
265 # ----------------------------------------------------------------
266 # Cleans up any child processes that have exited.
267 # In shutdown mode, block until all children have washed ashore
268 # ----------------------------------------------------------------
270 my($self, $shutdown) = @_;
271 $self->{child_died} = 1;
275 my $pid = waitpid(-1, ($shutdown) ? 0 : WNOHANG);
278 $chatty and $logger->internal("server: reaping child $pid");
280 my ($child) = grep {$_->{pid} == $pid} (@{$self->{active_list}}, @{$self->{idle_list}});
282 close($child->{pipe_to_parent});
283 close($child->{pipe_to_child});
284 delete $child->{$_} for keys %$child; # destroy with a vengeance
286 $self->{num_children}--;
287 $self->{active_list} = [ grep { $_->{pid} != $pid } @{$self->{active_list}} ];
288 $self->{idle_list} = [ grep { $_->{pid} != $pid } @{$self->{idle_list}} ];
291 $self->spawn_children unless $shutdown;
293 $chatty and $logger->internal(sprintf(
294 "server: %d idle and %d active children after reap_children",
295 scalar(@{$self->{idle_list}}), scalar(@{$self->{active_list}})));
299 # ----------------------------------------------------------------
300 # Spawn up to max_children processes
301 # ----------------------------------------------------------------
304 $self->spawn_child while $self->{num_children} < $self->{min_children};
307 # ----------------------------------------------------------------
308 # Spawns a new child. If $active is set, the child goes directly
309 # into the active_list.
310 # ----------------------------------------------------------------
312 my($self, $active) = @_;
314 my $child = OpenSRF::Server::Child->new($self);
316 # socket for sending message data to the child
318 $child->{pipe_to_child},
319 $child->{pipe_to_parent},
320 AF_UNIX, SOCK_STREAM, PF_UNSPEC)) {
321 $logger->error("server: error creating data socketpair: $!");
325 $child->{pipe_to_child}->autoflush(1);
326 $child->{pipe_to_parent}->autoflush(1);
328 $child->{pid} = fork();
330 if($child->{pid}) { # parent process
331 $self->{num_children}++;
335 push(@{$self->{active_list}}, $child);
337 push(@{$self->{idle_list}}, $child);
340 $chatty and $logger->internal("server: server spawned child $child with ".$self->{num_children}." total children");
344 } else { # child process
346 $SIG{$_} = 'DEFAULT' for (qw/INT TERM QUIT HUP/);
352 OpenSRF::Transport::PeerHandle->retrieve->disconnect;
354 $logger->error("server: child process died: $@") if $@;
359 # ----------------------------------------------------------------
360 # Sends the register command to the configured routers
361 # ----------------------------------------------------------------
362 sub register_routers {
365 my $conf = OpenSRF::Utils::Config->current;
366 my $routers = $conf->bootstrap->routers;
367 my $router_name = $conf->bootstrap->router_name;
370 for my $router (@$routers) {
373 if( !$router->{services} ||
374 !$router->{services}->{service} ||
376 ref($router->{services}->{service}) eq 'ARRAY' and
377 grep { $_ eq $self->{service} } @{$router->{services}->{service}}
378 ) || $router->{services}->{service} eq $self->{service}) {
380 my $name = $router->{name};
381 my $domain = $router->{domain};
382 push(@targets, "$name\@$domain/router");
386 push(@targets, "$router_name\@$router/router");
391 $logger->info("server: registering with router $_");
392 $self->{osrf_handle}->send(
394 body => 'registering',
395 router_command => 'register',
396 router_class => $self->{service}
400 $self->{routers} = \@targets;
403 # ----------------------------------------------------------------
404 # Sends the unregister command to any routers we have registered
406 # ----------------------------------------------------------------
407 sub unregister_routers {
409 return unless $self->{osrf_handle}->tcp_connected;
411 for my $router (@{$self->{routers}}) {
412 $logger->info("server: disconnecting from router $router");
413 $self->{osrf_handle}->send(
415 body => "unregistering",
416 router_command => "unregister",
417 router_class => $self->{service}
423 package OpenSRF::Server::Child;
426 use OpenSRF::Transport;
427 use OpenSRF::Application;
428 use OpenSRF::Transport::PeerHandle;
429 use OpenSRF::Transport::SlimJabber::XMPPMessage;
430 use OpenSRF::Utils::Logger qw($logger);
431 use OpenSRF::DomainObject::oilsResponse qw/:status/;
432 use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
433 use Time::HiRes qw(time);
434 use POSIX qw/:sys_wait_h :errno_h/;
436 use overload '""' => sub { return '[' . shift()->{pid} . ']'; };
439 my($class, $parent) = @_;
440 my $self = bless({}, $class);
441 $self->{pid} = 0; # my process ID
442 $self->{parent} = $parent; # Controller parent process
443 $self->{num_requests} = 0; # total serviced requests
449 my $flags = fcntl($fh, F_GETFL, 0);
450 fcntl($fh, F_SETFL, $flags | O_NONBLOCK);
455 my $flags = fcntl($fh, F_GETFL, 0);
456 $flags &= ~O_NONBLOCK;
457 fcntl($fh, F_SETFL, $flags);
460 # ----------------------------------------------------------------
461 # Connects to Jabber and runs the application child_init
462 # ----------------------------------------------------------------
465 my $service = $self->{parent}->{service};
466 $0 = "OpenSRF Drone [$service]";
467 OpenSRF::Transport::PeerHandle->construct($service);
468 OpenSRF::Application->application_implementation->child_init
469 if (OpenSRF::Application->application_implementation->can('child_init'));
472 # ----------------------------------------------------------------
473 # Waits for messages from the parent process, handles the message,
474 # then goes into the keepalive loop if this is a stateful session.
475 # When max_requests is hit, the process exits.
476 # ----------------------------------------------------------------
479 my $network = OpenSRF::Transport::PeerHandle->retrieve;
481 # main child run loop. Ends when this child hits max requests.
484 my $data = $self->wait_for_request or next;
486 # Update process name to show activity
490 # Discard extraneous data from the jabber socket
491 if(!$network->flush_socket()) {
492 $logger->error("server: network disconnected! child dropping request and exiting: $data");
496 my $session = OpenSRF::Transport->handler(
497 $self->{parent}->{service},
498 OpenSRF::Transport::SlimJabber::XMPPMessage->new(xml => $data)
501 $self->keepalive_loop($session);
503 last if ++$self->{num_requests} == $self->{parent}->{max_requests};
505 # Tell the parent process we are available to process requests
508 # Repair process name
512 $chatty and $logger->internal("server: child process shutting down after reaching max_requests");
514 OpenSRF::Application->application_implementation->child_exit
515 if (OpenSRF::Application->application_implementation->can('child_exit'));
518 # ----------------------------------------------------------------
519 # waits for a request data on the parent pipe and returns it.
520 # ----------------------------------------------------------------
521 sub wait_for_request {
525 my $read_size = 1024;
529 # Start out blocking, when data is available, read it all
532 my $n = sysread($self->{pipe_to_parent}, $buf, $read_size);
535 $logger->error("server: error reading data pipe: $!") unless EAGAIN == $!;
539 last if $n <= 0; # no data left to read
543 last if $n < $read_size; # done reading all data
545 $self->set_nonblock($self->{pipe_to_parent}) unless $nonblock;
549 $self->set_block($self->{pipe_to_parent}) if $nonblock;
554 # ----------------------------------------------------------------
555 # If this is a stateful opensrf session, wait up to $keepalive
556 # seconds for subsequent requests from the client
557 # ----------------------------------------------------------------
559 my($self, $session) = @_;
560 my $keepalive = $self->{parent}->{keepalive};
562 while($session->state and $session->state == $session->CONNECTED) {
564 unless( $session->queue_wait($keepalive) ) {
566 # client failed to disconnect before timeout
567 $logger->info("server: no request was received in $keepalive seconds, exiting stateful session");
569 my $res = OpenSRF::DomainObject::oilsConnectStatus->new(
570 status => "Disconnected on timeout",
571 statusCode => STATUS_TIMEOUT
574 $session->status($res);
575 $session->state($session->DISCONNECTED);
580 $chatty and $logger->internal("server: child done with request(s)");
584 # ----------------------------------------------------------------
585 # Report our availability to our parent process
586 # ----------------------------------------------------------------
590 $self->{pipe_to_parent},
591 sprintf("%*s", OpenSRF::Server::STATUS_PIPE_DATA_SIZE, $self->{pid})