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;
26 use POSIX qw/:sys_wait_h :errno_h/;
27 use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
30 our $chatty = 1; # disable for production
32 use constant STATUS_PIPE_DATA_SIZE => 12;
35 my($class, $service, %args) = @_;
36 my $self = bless(\%args, $class);
38 $self->{service} = $service; # service name
39 $self->{num_children} = 0; # number of child processes
40 $self->{osrf_handle} = undef; # xmpp handle
41 $self->{routers} = []; # list of registered routers
42 $self->{active_list} = []; # list of active children
43 $self->{idle_list} = []; # list of idle children
44 $self->{pid_map} = {}; # map of child pid to child for cleaner access
46 $self->{stderr_log} = $self->{stderr_log_path} . "/${service}_stderr.log"
47 if $self->{stderr_log_path};
49 $self->{min_spare_children} ||= 0;
51 $self->{max_spare_children} = $self->{min_spare_children} + 1 if
52 $self->{max_spare_children} and
53 $self->{max_spare_children} <= $self->{min_spare_children};
58 # ----------------------------------------------------------------
59 # Disconnects from routers and waits for child processes to exit.
60 # ----------------------------------------------------------------
65 $logger->info("server: shutting down and cleaning up...");
67 # don't get sidetracked by signals while we're cleaning up.
68 # it could result in unexpected behavior with list traversal
69 $SIG{CHLD} = 'IGNORE';
71 # terminate the child processes
72 $self->kill_child($_) for
73 (@{$self->{idle_list}}, @{$self->{active_list}});
76 $self->unregister_routers;
78 $self->{osrf_handle}->disconnect;
80 # clean up our dead children
81 $self->reap_children(1);
83 exit(0) unless $no_exit;
87 # ----------------------------------------------------------------
88 # Waits on the jabber socket for inbound data from the router.
89 # Each new message is passed off to a child process for handling.
90 # At regular intervals, wake up for min/max spare child maintenance
91 # ----------------------------------------------------------------
95 $logger->set_service($self->{service});
97 $SIG{$_} = sub { $self->cleanup; } for (qw/INT TERM QUIT/);
98 $SIG{CHLD} = sub { $self->reap_children(); };
100 $self->spawn_children;
101 $self->build_osrf_handle;
102 $self->register_routers;
109 $self->{child_died} = 0;
111 my $msg = $self->{osrf_handle}->process($wait_time);
113 # we woke up for any reason, reset the wait time to allow
114 # for idle maintenance as necessary
119 if(my $child = pop(@{$self->{idle_list}})) {
121 # we have an idle child to handle the request
122 $chatty and $logger->internal("server: passing request to idle child $child");
123 push(@{$self->{active_list}}, $child);
124 $self->write_child($child, $msg);
126 } elsif($self->{num_children} < $self->{max_children}) {
128 # spawning a child to handle the request
129 $chatty and $logger->internal("server: spawning child to handle request");
130 $self->write_child($self->spawn_child(1), $msg);
133 $logger->warn("server: no children available, waiting... consider increasing " .
134 "max_children for this application higher than $self->{max_children} ".
135 "in the OpenSRF configuration if this message occurs frequently");
136 $self->check_status(1); # block until child is available
138 my $child = pop(@{$self->{idle_list}});
139 push(@{$self->{active_list}}, $child);
140 $self->write_child($child, $msg);
145 # don't perform idle maint immediately when woken by SIGCHLD
146 unless($self->{child_died}) {
148 # when we hit equilibrium, there's no need for regular
149 # maintenance, so set wait_time to 'forever'
151 !$self->perform_idle_maintenance and # no maintenance performed this time
152 @{$self->{active_list}} == 0; # no active children
158 # ----------------------------------------------------------------
159 # Launch a new spare child or kill an extra spare child. To
160 # prevent large-scale spawning or die-offs, spawn or kill only
161 # 1 process per idle maintenance loop.
162 # Returns true if any idle maintenance occurred, 0 otherwise
163 # ----------------------------------------------------------------
164 sub perform_idle_maintenance {
167 $chatty and $logger->internal(
169 "server: %d idle, %d active, %d min_spare, %d max_spare in idle maintenance",
170 scalar(@{$self->{idle_list}}),
171 scalar(@{$self->{active_list}}),
172 $self->{min_spare_children},
173 $self->{max_spare_children}
177 # spawn 1 spare child per maintenance loop if necessary
178 if( $self->{min_spare_children} and
179 $self->{num_children} < $self->{max_children} and
180 scalar(@{$self->{idle_list}}) < $self->{min_spare_children} ) {
182 $chatty and $logger->internal("server: spawning spare child");
186 # kill 1 excess spare child per maintenance loop if necessary
187 } elsif($self->{max_spare_children} and
188 $self->{num_children} > $self->{min_children} and
189 scalar(@{$self->{idle_list}}) > $self->{max_spare_children} ) {
191 $chatty and $logger->internal("server: killing spare child");
201 my $child = shift || pop(@{$self->{idle_list}}) or return;
202 $chatty and $logger->internal("server: killing child $child");
203 kill('TERM', $child->{pid});
206 # ----------------------------------------------------------------
207 # Jabber connection inbound message arrive on.
208 # ----------------------------------------------------------------
209 sub build_osrf_handle {
212 my $conf = OpenSRF::Utils::Config->current;
213 my $username = $conf->bootstrap->username;
214 my $password = $conf->bootstrap->passwd;
215 my $domain = $conf->bootstrap->domain;
216 my $port = $conf->bootstrap->port;
217 my $resource = $self->{service} . '_listener_' . $conf->env->hostname;
219 $logger->debug("server: inbound connecting as $username\@$domain/$resource on port $port");
221 $self->{osrf_handle} =
222 OpenSRF::Transport::SlimJabber::Client->new(
223 username => $username,
224 resource => $resource,
225 password => $password,
230 $self->{osrf_handle}->initialize;
234 # ----------------------------------------------------------------
235 # Sends request data to a child process
236 # ----------------------------------------------------------------
238 my($self, $child, $msg) = @_;
239 my $xml = decode_utf8($msg->to_xml);
240 syswrite($child->{pipe_to_child}, encode_utf8($xml));
243 # ----------------------------------------------------------------
244 # Checks to see if any child process has reported its availability
245 # In blocking mode, blocks until a child has reported.
246 # ----------------------------------------------------------------
248 my($self, $block) = @_;
250 return unless @{$self->{active_list}};
252 my $read_set = IO::Select->new;
253 $read_set->add($_->{pipe_to_child}) for @{$self->{active_list}};
259 # if can_read or sysread is interrupted while bloking, go back and
260 # wait again until we have at least 1 free child
262 if(my @handles = $read_set->can_read(($block) ? undef : 0)) {
264 for my $pipe (@handles) {
265 sysread($pipe, $pid, STATUS_PIPE_DATA_SIZE) or next;
266 push(@pids, int($pid));
270 last unless $block and !@pids;
275 $chatty and $logger->internal("server: ".scalar(@pids)." children reporting for duty: (@pids)");
280 # move the children from the active list to the idle list
281 for my $proc (@{$self->{active_list}}) {
282 if(grep { $_ == $proc->{pid} } @pids) {
283 push(@{$self->{idle_list}}, $proc);
285 push(@new_actives, $proc);
289 $self->{active_list} = [@new_actives];
291 $chatty and $logger->internal(sprintf(
292 "server: %d idle and %d active children after status update",
293 scalar(@{$self->{idle_list}}), scalar(@{$self->{active_list}})));
296 # ----------------------------------------------------------------
297 # Cleans up any child processes that have exited.
298 # In shutdown mode, block until all children have washed ashore
299 # ----------------------------------------------------------------
301 my($self, $shutdown) = @_;
302 $self->{child_died} = 1;
306 my $pid = waitpid(-1, ($shutdown) ? 0 : WNOHANG);
309 $chatty and $logger->internal("server: reaping child $pid");
311 my $child = $self->{pid_map}->{$pid};
313 close($child->{pipe_to_parent});
314 close($child->{pipe_to_child});
316 $self->{active_list} = [ grep { $_->{pid} != $pid } @{$self->{active_list}} ];
317 $self->{idle_list} = [ grep { $_->{pid} != $pid } @{$self->{idle_list}} ];
319 $self->{num_children}--;
320 delete $self->{pid_map}->{$pid};
321 delete $child->{$_} for keys %$child; # destroy with a vengeance
324 $self->spawn_children unless $shutdown;
326 $chatty and $logger->internal(sprintf(
327 "server: %d idle and %d active children after reap_children",
328 scalar(@{$self->{idle_list}}), scalar(@{$self->{active_list}})));
332 # ----------------------------------------------------------------
333 # Spawn up to max_children processes
334 # ----------------------------------------------------------------
337 $self->spawn_child while $self->{num_children} < $self->{min_children};
340 # ----------------------------------------------------------------
341 # Spawns a new child. If $active is set, the child goes directly
342 # into the active_list.
343 # ----------------------------------------------------------------
345 my($self, $active) = @_;
347 my $child = OpenSRF::Server::Child->new($self);
349 # socket for sending message data to the child
351 $child->{pipe_to_child},
352 $child->{pipe_to_parent},
353 AF_UNIX, SOCK_STREAM, PF_UNSPEC)) {
354 $logger->error("server: error creating data socketpair: $!");
358 $child->{pipe_to_child}->autoflush(1);
359 $child->{pipe_to_parent}->autoflush(1);
361 $child->{pid} = fork();
363 if($child->{pid}) { # parent process
364 $self->{num_children}++;
365 $self->{pid_map}->{$child->{pid}} = $child;
368 push(@{$self->{active_list}}, $child);
370 push(@{$self->{idle_list}}, $child);
373 $chatty and $logger->internal("server: server spawned child $child with ".$self->{num_children}." total children");
377 } else { # child process
379 $SIG{$_} = 'DEFAULT' for (qw/INT TERM QUIT HUP/);
381 if($self->{stderr_log}) {
383 $chatty and $logger->internal("server: redirecting STDERR to " . $self->{stderr_log});
386 unless( open(STDERR, '>>' . $self->{stderr_log}) ) {
387 $logger->error("server: unable to open STDERR log file: " . $self->{stderr_log} . " : $@");
388 open STDERR, '>/dev/null'; # send it back to /dev/null
396 OpenSRF::Transport::PeerHandle->retrieve->disconnect;
398 $logger->error("server: child process died: $@") if $@;
403 # ----------------------------------------------------------------
404 # Sends the register command to the configured routers
405 # ----------------------------------------------------------------
406 sub register_routers {
409 my $conf = OpenSRF::Utils::Config->current;
410 my $routers = $conf->bootstrap->routers;
411 my $router_name = $conf->bootstrap->router_name;
414 for my $router (@$routers) {
417 if( !$router->{services} ||
418 !$router->{services}->{service} ||
420 ref($router->{services}->{service}) eq 'ARRAY' and
421 grep { $_ eq $self->{service} } @{$router->{services}->{service}}
422 ) || $router->{services}->{service} eq $self->{service}) {
424 my $name = $router->{name};
425 my $domain = $router->{domain};
426 push(@targets, "$name\@$domain/router");
430 push(@targets, "$router_name\@$router/router");
435 $logger->info("server: registering with router $_");
436 $self->{osrf_handle}->send(
438 body => 'registering',
439 router_command => 'register',
440 router_class => $self->{service}
444 $self->{routers} = \@targets;
447 # ----------------------------------------------------------------
448 # Sends the unregister command to any routers we have registered
450 # ----------------------------------------------------------------
451 sub unregister_routers {
453 return unless $self->{osrf_handle}->tcp_connected;
455 for my $router (@{$self->{routers}}) {
456 $logger->info("server: disconnecting from router $router");
457 $self->{osrf_handle}->send(
459 body => "unregistering",
460 router_command => "unregister",
461 router_class => $self->{service}
467 package OpenSRF::Server::Child;
470 use OpenSRF::Transport;
471 use OpenSRF::Application;
472 use OpenSRF::Transport::PeerHandle;
473 use OpenSRF::Transport::SlimJabber::XMPPMessage;
474 use OpenSRF::Utils::Logger qw($logger);
475 use OpenSRF::DomainObject::oilsResponse qw/:status/;
476 use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
477 use Time::HiRes qw(time);
478 use POSIX qw/:sys_wait_h :errno_h/;
480 use overload '""' => sub { return '[' . shift()->{pid} . ']'; };
483 my($class, $parent) = @_;
484 my $self = bless({}, $class);
485 $self->{pid} = 0; # my process ID
486 $self->{parent} = $parent; # Controller parent process
487 $self->{num_requests} = 0; # total serviced requests
493 my $flags = fcntl($fh, F_GETFL, 0);
494 fcntl($fh, F_SETFL, $flags | O_NONBLOCK);
499 my $flags = fcntl($fh, F_GETFL, 0);
500 $flags &= ~O_NONBLOCK;
501 fcntl($fh, F_SETFL, $flags);
504 # ----------------------------------------------------------------
505 # Connects to Jabber and runs the application child_init
506 # ----------------------------------------------------------------
509 my $service = $self->{parent}->{service};
510 $0 = "OpenSRF Drone [$service]";
511 OpenSRF::Transport::PeerHandle->construct($service);
512 OpenSRF::Application->application_implementation->child_init
513 if (OpenSRF::Application->application_implementation->can('child_init'));
516 # ----------------------------------------------------------------
517 # Waits for messages from the parent process, handles the message,
518 # then goes into the keepalive loop if this is a stateful session.
519 # When max_requests is hit, the process exits.
520 # ----------------------------------------------------------------
523 my $network = OpenSRF::Transport::PeerHandle->retrieve;
525 # main child run loop. Ends when this child hits max requests.
528 my $data = $self->wait_for_request or next;
530 # Update process name to show activity
534 # Discard extraneous data from the jabber socket
535 if(!$network->flush_socket()) {
536 $logger->error("server: network disconnected! child dropping request and exiting: $data");
540 my $session = OpenSRF::Transport->handler(
541 $self->{parent}->{service},
542 OpenSRF::Transport::SlimJabber::XMPPMessage->new(xml => $data)
545 $self->keepalive_loop($session);
547 last if ++$self->{num_requests} == $self->{parent}->{max_requests};
549 # Tell the parent process we are available to process requests
552 # Repair process name
556 $chatty and $logger->internal("server: child process shutting down after reaching max_requests");
558 OpenSRF::Application->application_implementation->child_exit
559 if (OpenSRF::Application->application_implementation->can('child_exit'));
562 # ----------------------------------------------------------------
563 # waits for a request data on the parent pipe and returns it.
564 # ----------------------------------------------------------------
565 sub wait_for_request {
569 my $read_size = 1024;
573 # Start out blocking, when data is available, read it all
576 my $n = sysread($self->{pipe_to_parent}, $buf, $read_size);
579 $logger->error("server: error reading data pipe: $!") unless EAGAIN == $!;
583 last if $n <= 0; # no data left to read
587 last if $n < $read_size; # done reading all data
589 $self->set_nonblock($self->{pipe_to_parent}) unless $nonblock;
593 $self->set_block($self->{pipe_to_parent}) if $nonblock;
598 # ----------------------------------------------------------------
599 # If this is a stateful opensrf session, wait up to $keepalive
600 # seconds for subsequent requests from the client
601 # ----------------------------------------------------------------
603 my($self, $session) = @_;
604 my $keepalive = $self->{parent}->{keepalive};
606 while($session->state and $session->state == $session->CONNECTED) {
608 unless( $session->queue_wait($keepalive) ) {
610 # client failed to disconnect before timeout
611 $logger->info("server: no request was received in $keepalive seconds, exiting stateful session");
613 my $res = OpenSRF::DomainObject::oilsConnectStatus->new(
614 status => "Disconnected on timeout",
615 statusCode => STATUS_TIMEOUT
618 $session->status($res);
619 $session->state($session->DISCONNECTED);
624 $chatty and $logger->internal("server: child done with request(s)");
628 # ----------------------------------------------------------------
629 # Report our availability to our parent process
630 # ----------------------------------------------------------------
634 $self->{pipe_to_parent},
635 sprintf("%*s", OpenSRF::Server::STATUS_PIPE_DATA_SIZE, $self->{pid})