]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/perl/lib/OpenSRF/Server.pm
LP1204123 SIGTERM causes graceful shutdown (Perl)
[OpenSRF.git] / src / perl / lib / OpenSRF / Server.pm
1 # ----------------------------------------------------------------
2 # Copyright (C) 2010 Equinox Software, Inc.
3 # Bill Erickson <erickson@esilibrary.com>
4 #
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.
9 #
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;
16 use strict;
17 use warnings;
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 Encode;
26 use POSIX qw/:sys_wait_h :errno_h/;
27 use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
28 use Time::HiRes qw/usleep/;
29 use IO::Select;
30 use Socket;
31 our $chatty = 1; # disable for production
32
33 use constant STATUS_PIPE_DATA_SIZE => 12;
34 use constant WRITE_PIPE_DATA_SIZE  => 12;
35
36 sub new {
37     my($class, $service, %args) = @_;
38     my $self = bless(\%args, $class);
39
40     $self->{service}        = $service; # service name
41     $self->{num_children}   = 0; # number of child processes
42     $self->{osrf_handle}    = undef; # xmpp handle
43     $self->{routers}        = []; # list of registered routers
44     $self->{active_list}    = []; # list of active children
45     $self->{idle_list}      = []; # list of idle children
46     $self->{sighup_pending} = [];
47     $self->{pid_map}        = {}; # map of child pid to child for cleaner access
48     $self->{sig_pipe}       = 0;  # true if last syswrite failed
49
50     $self->{stderr_log} = $self->{stderr_log_path} . "/${service}_stderr.log" 
51         if $self->{stderr_log_path};
52
53     $self->{min_spare_children} ||= 0;
54
55     $self->{max_spare_children} = $self->{min_spare_children} + 1 if
56         $self->{max_spare_children} and
57         $self->{max_spare_children} <= $self->{min_spare_children};
58
59     return $self;
60 }
61
62 # ----------------------------------------------------------------
63 # Disconnects from routers and waits for child processes to exit.
64 # ----------------------------------------------------------------
65 sub cleanup {
66     my $self = shift;
67     my $no_exit = shift;
68     my $graceful = shift;
69
70     $logger->info("server: shutting down and cleaning up...");
71
72     # de-register routers
73     $self->unregister_routers;
74
75     if ($graceful) {
76         # graceful shutdown waits for all active 
77         # children to complete their in-process tasks.
78
79         while (@{$self->{active_list}}) {
80             $logger->info("server: graceful shutdown with ".
81                 @{$self->{active_list}}." active children...");
82
83             # block until a child is becomes available
84             $self->check_status(1);
85         }
86         $logger->info("server: all clear for graceful shutdown");
87     }
88
89     # don't get sidetracked by signals while we're cleaning up.
90     # it could result in unexpected behavior with list traversal
91     $SIG{CHLD} = 'IGNORE';
92
93     # terminate the child processes
94     $self->kill_child($_) for
95         (@{$self->{idle_list}}, @{$self->{active_list}});
96
97     $self->{osrf_handle}->disconnect;
98
99     # clean up our dead children
100     $self->reap_children(1);
101
102     exit(0) unless $no_exit;
103 }
104
105 # ----------------------------------------------------------------
106 # SIGHUP handler.  Kill all idle children.  Copy list of active
107 # children into sighup_pending list for later cleanup.
108 # ----------------------------------------------------------------
109 sub handle_sighup {
110     my $self = shift;
111     $logger->info("server: caught SIGHUP; reloading children");
112
113     # reload the opensrf config
114     # note: calling ::Config->load() results in ever-growing
115     # package names, which eventually causes an exception
116     OpenSRF::Utils::Config->current->_load(
117         force => 1,
118         config_file => OpenSRF::Utils::Config->current->FILE
119     );
120
121     # force-reload the logger config
122     OpenSRF::Utils::Logger::set_config(1);
123
124     # copy active list into pending list for later cleanup
125     $self->{sighup_pending} = [ @{$self->{active_list}} ];
126
127     # idle_list will be modified as children are reaped.
128     my @idle = @{$self->{idle_list}};
129
130     # idle children are the reaper's plaything
131     $self->kill_child($_) for @idle;
132 }
133
134 # ----------------------------------------------------------------
135 # Waits on the jabber socket for inbound data from the router.
136 # Each new message is passed off to a child process for handling.
137 # At regular intervals, wake up for min/max spare child maintenance
138 # ----------------------------------------------------------------
139 sub run {
140     my $self = shift;
141
142     $logger->set_service($self->{service});
143
144     $SIG{$_} = sub { $self->cleanup; } for (qw/INT QUIT/);
145     $SIG{TERM} = sub { $self->cleanup(0, 1); };
146     $SIG{CHLD} = sub { $self->reap_children(); };
147     $SIG{HUP} = sub { $self->handle_sighup(); };
148
149     $self->spawn_children;
150     $self->build_osrf_handle;
151     $self->register_routers;
152     my $wait_time = 1;
153
154     # main server loop
155     while(1) {
156
157         $self->check_status;
158         $self->{child_died} = 0;
159
160         my $msg = $self->{osrf_handle}->process($wait_time);
161
162         # we woke up for any reason, reset the wait time to allow
163         # for idle maintenance as necessary
164         $wait_time = 1;
165
166         if($msg) {
167
168             if(my $child = pop(@{$self->{idle_list}})) {
169
170                 # we have an idle child to handle the request
171                 $chatty and $logger->internal("server: passing request to idle child $child");
172                 push(@{$self->{active_list}}, $child);
173                 $self->write_child($child, $msg);
174
175             } elsif($self->{num_children} < $self->{max_children}) {
176
177                 # spawning a child to handle the request
178                 $chatty and $logger->internal("server: spawning child to handle request");
179                 $self->write_child($self->spawn_child(1), $msg);
180
181             } else {
182                 $logger->warn("server: no children available, waiting... consider increasing " .
183                     "max_children for this application higher than $self->{max_children} ".
184                     "in the OpenSRF configuration if this message occurs frequently");
185                 $self->check_status(1); # block until child is available
186
187                 my $child = pop(@{$self->{idle_list}});
188                 push(@{$self->{active_list}}, $child);
189                 $self->write_child($child, $msg);
190             }
191
192         } else {
193
194             # don't perform idle maint immediately when woken by SIGCHLD
195             unless($self->{child_died}) {
196
197                 # when we hit equilibrium, there's no need for regular
198                 # maintenance, so set wait_time to 'forever'
199                 $wait_time = -1 if 
200                     !$self->perform_idle_maintenance and # no maintenance performed this time
201                     @{$self->{active_list}} == 0; # no active children 
202             }
203         }
204     }
205 }
206
207 # ----------------------------------------------------------------
208 # Launch a new spare child or kill an extra spare child.  To
209 # prevent large-scale spawning or die-offs, spawn or kill only
210 # 1 process per idle maintenance loop.
211 # Returns true if any idle maintenance occurred, 0 otherwise
212 # ----------------------------------------------------------------
213 sub perform_idle_maintenance {
214     my $self = shift;
215
216     $chatty and $logger->internal(
217         sprintf(
218             "server: %d idle, %d active, %d min_spare, %d max_spare in idle maintenance",
219             scalar(@{$self->{idle_list}}), 
220             scalar(@{$self->{active_list}}),
221             $self->{min_spare_children},
222             $self->{max_spare_children}
223         )
224     );
225
226     # spawn 1 spare child per maintenance loop if necessary
227     if( $self->{min_spare_children} and
228         $self->{num_children} < $self->{max_children} and
229         scalar(@{$self->{idle_list}}) < $self->{min_spare_children} ) {
230
231         $chatty and $logger->internal("server: spawning spare child");
232         $self->spawn_child;
233         return 1;
234
235     # kill 1 excess spare child per maintenance loop if necessary
236     } elsif($self->{max_spare_children} and
237             $self->{num_children} > $self->{min_children} and
238             scalar(@{$self->{idle_list}}) > $self->{max_spare_children} ) {
239
240         $chatty and $logger->internal("server: killing spare child");
241         $self->kill_child;
242         return 1;
243     }
244
245     return 0;
246 }
247
248 sub kill_child {
249     my $self = shift;
250     my $child = shift || pop(@{$self->{idle_list}}) or return;
251     $chatty and $logger->internal("server: killing child $child");
252     kill('TERM', $child->{pid});
253 }
254
255 # ----------------------------------------------------------------
256 # Jabber connection inbound message arrive on.
257 # ----------------------------------------------------------------
258 sub build_osrf_handle {
259     my $self = shift;
260
261     my $conf = OpenSRF::Utils::Config->current;
262     my $username = $conf->bootstrap->username;
263     my $password = $conf->bootstrap->passwd;
264     my $domain = $conf->bootstrap->domain;
265     my $port = $conf->bootstrap->port;
266     my $resource = $self->{service} . '_listener_' . $conf->env->hostname;
267
268     $logger->debug("server: inbound connecting as $username\@$domain/$resource on port $port");
269
270     $self->{osrf_handle} =
271         OpenSRF::Transport::SlimJabber::Client->new(
272             username => $username,
273             resource => $resource,
274             password => $password,
275             host => $domain,
276             port => $port,
277         );
278
279     $self->{osrf_handle}->initialize;
280 }
281
282
283 # ----------------------------------------------------------------
284 # Sends request data to a child process
285 # ----------------------------------------------------------------
286 sub write_child {
287     my($self, $child, $msg) = @_;
288     my $xml = encode_utf8(decode_utf8($msg->to_xml));
289
290     # tell the child how much data to expect, minus the header
291     my $write_size;
292     {use bytes; $write_size = length($xml)}
293     $write_size = sprintf("%*s", WRITE_PIPE_DATA_SIZE, $write_size);
294
295     for (0..2) {
296
297         $self->{sig_pipe} = 0;
298         local $SIG{'PIPE'} = sub { $self->{sig_pipe} = 1; };
299
300         # send message to child data pipe
301         syswrite($child->{pipe_to_child}, $write_size . $xml);
302
303         last unless $self->{sig_pipe};
304         $logger->error("server: got SIGPIPE writing to $child, retrying...");
305         usleep(50000); # 50 msec
306     }
307
308     $logger->error("server: unable to send request message to child $child") if $self->{sig_pipe};
309 }
310
311 # ----------------------------------------------------------------
312 # Checks to see if any child process has reported its availability
313 # In blocking mode, blocks until a child has reported.
314 # ----------------------------------------------------------------
315 sub check_status {
316     my($self, $block) = @_;
317
318     return unless @{$self->{active_list}};
319
320     my @pids;
321
322     while (1) {
323
324         # if can_read or sysread is interrupted while bloking, go back and 
325         # wait again until we have at least 1 free child
326
327         # refresh the read_set handles in case we lost a child in the previous iteration
328         my $read_set = IO::Select->new;
329         $read_set->add($_->{pipe_to_child}) for @{$self->{active_list}};
330
331         if(my @handles = $read_set->can_read(($block) ? undef : 0)) {
332             my $pid = '';
333             for my $pipe (@handles) {
334                 sysread($pipe, $pid, STATUS_PIPE_DATA_SIZE) or next;
335                 push(@pids, int($pid));
336             }
337         }
338
339         last unless $block and !@pids;
340     }
341
342     return unless @pids;
343
344     $chatty and $logger->internal("server: ".scalar(@pids)." children reporting for duty: (@pids)");
345
346     my $child;
347     my @new_actives;
348
349     # move the children from the active list to the idle list
350     for my $proc (@{$self->{active_list}}) {
351         if(grep { $_ == $proc->{pid} } @pids) {
352             push(@{$self->{idle_list}}, $proc);
353         } else {
354             push(@new_actives, $proc);
355         }
356     }
357
358     $self->{active_list} = [@new_actives];
359
360     $chatty and $logger->internal(sprintf(
361         "server: %d idle and %d active children after status update",
362             scalar(@{$self->{idle_list}}), scalar(@{$self->{active_list}})));
363
364     # some children just went from active to idle. let's see 
365     # if any of them need to be killed from a previous sighup.
366
367     for my $child (@{$self->{sighup_pending}}) {
368         if (grep {$_ == $child->{pid}} @pids) {
369
370             $chatty and $logger->internal(
371                 "server: killing previously-active ".
372                 "child after receiving SIGHUP: $child");
373
374             # remove the pending child
375             $self->{sighup_pending} = [
376                 grep {$_->{pid} != $child->{pid}} 
377                     @{$self->{sighup_pending}}
378             ];
379
380             # kill the pending child
381             $self->kill_child($child)
382         }
383     }
384 }
385
386 # ----------------------------------------------------------------
387 # Cleans up any child processes that have exited.
388 # In shutdown mode, block until all children have washed ashore
389 # ----------------------------------------------------------------
390 sub reap_children {
391     my($self, $shutdown) = @_;
392     $self->{child_died} = 1;
393
394     while(1) {
395
396         my $pid = waitpid(-1, ($shutdown) ? 0 : WNOHANG);
397         last if $pid <= 0;
398
399         $chatty and $logger->internal("server: reaping child $pid");
400
401         my $child = $self->{pid_map}->{$pid};
402
403         close($child->{pipe_to_parent});
404         close($child->{pipe_to_child});
405
406         $self->{active_list} = [ grep { $_->{pid} != $pid } @{$self->{active_list}} ];
407         $self->{idle_list} = [ grep { $_->{pid} != $pid } @{$self->{idle_list}} ];
408
409         $self->{num_children}--;
410         delete $self->{pid_map}->{$pid};
411         delete $child->{$_} for keys %$child; # destroy with a vengeance
412     }
413
414     $self->spawn_children unless $shutdown;
415
416     $chatty and $logger->internal(sprintf(
417         "server: %d idle and %d active children after reap_children",
418             scalar(@{$self->{idle_list}}), scalar(@{$self->{active_list}})));
419 }
420
421 # ----------------------------------------------------------------
422 # Spawn up to max_children processes
423 # ----------------------------------------------------------------
424 sub spawn_children {
425     my $self = shift;
426     $self->spawn_child while $self->{num_children} < $self->{min_children};
427 }
428
429 # ----------------------------------------------------------------
430 # Spawns a new child.  If $active is set, the child goes directly
431 # into the active_list.
432 # ----------------------------------------------------------------
433 sub spawn_child {
434     my($self, $active) = @_;
435
436     my $child = OpenSRF::Server::Child->new($self);
437
438     # socket for sending message data to the child
439     if(!socketpair(
440         $child->{pipe_to_child},
441         $child->{pipe_to_parent},
442         AF_UNIX, SOCK_STREAM, PF_UNSPEC)) {
443             $logger->error("server: error creating data socketpair: $!");
444             return undef;
445     }
446
447     $child->{pipe_to_child}->autoflush(1);
448     $child->{pipe_to_parent}->autoflush(1);
449
450     $child->{pid} = fork();
451
452     if($child->{pid}) { # parent process
453         $self->{num_children}++;
454         $self->{pid_map}->{$child->{pid}} = $child;
455
456         if($active) {
457             push(@{$self->{active_list}}, $child);
458         } else {
459             push(@{$self->{idle_list}}, $child);
460         }
461
462         $chatty and $logger->internal("server: server spawned child $child with ".$self->{num_children}." total children");
463
464         return $child;
465
466     } else { # child process
467
468         $SIG{$_} = 'DEFAULT' for (qw/INT TERM QUIT HUP CHLD/);
469
470         if($self->{stderr_log}) {
471
472             $chatty and $logger->internal("server: redirecting STDERR to " . $self->{stderr_log});
473
474             close STDERR;
475             unless( open(STDERR, '>>' . $self->{stderr_log}) ) {
476                 $logger->error("server: unable to open STDERR log file: " . $self->{stderr_log} . " : $@");
477                 open STDERR, '>/dev/null'; # send it back to /dev/null
478             }
479         }
480
481         $child->{pid} = $$;
482         eval {
483             $child->init;
484             $child->run;
485             OpenSRF::Transport::PeerHandle->retrieve->disconnect;
486         };
487         $logger->error("server: child process died: $@") if $@;
488         exit(0);
489     }
490 }
491
492 # ----------------------------------------------------------------
493 # Sends the register command to the configured routers
494 # ----------------------------------------------------------------
495 sub register_routers {
496     my $self = shift;
497
498     my $conf = OpenSRF::Utils::Config->current;
499     my $routers = $conf->bootstrap->routers;
500     my $router_name = $conf->bootstrap->router_name;
501     my @targets;
502
503     for my $router (@$routers) {
504         if(ref $router) {
505
506             if( !$router->{services} ||
507                 !$router->{services}->{service} ||
508                 (
509                     ref($router->{services}->{service}) eq 'ARRAY' and
510                     grep { $_ eq $self->{service} } @{$router->{services}->{service}}
511                 )  || $router->{services}->{service} eq $self->{service}) {
512
513                 my $name = $router->{name};
514                 my $domain = $router->{domain};
515                 push(@targets, "$name\@$domain/router");
516             }
517
518         } else {
519             push(@targets, "$router_name\@$router/router");
520         }
521     }
522
523     foreach (@targets) {
524         $logger->info("server: registering with router $_");
525         $self->{osrf_handle}->send(
526             to => $_,
527             body => 'registering',
528             router_command => 'register',
529             router_class => $self->{service}
530         );
531     }
532
533     $self->{routers} = \@targets;
534 }
535
536 # ----------------------------------------------------------------
537 # Sends the unregister command to any routers we have registered
538 # with.
539 # ----------------------------------------------------------------
540 sub unregister_routers {
541     my $self = shift;
542     return unless $self->{osrf_handle}->tcp_connected;
543
544     for my $router (@{$self->{routers}}) {
545         $logger->info("server: disconnecting from router $router");
546         $self->{osrf_handle}->send(
547             to => $router,
548             body => "unregistering",
549             router_command => "unregister",
550             router_class => $self->{service}
551         );
552     }
553 }
554
555
556 package OpenSRF::Server::Child;
557 use strict;
558 use warnings;
559 use OpenSRF::Transport;
560 use OpenSRF::Application;
561 use OpenSRF::Transport::PeerHandle;
562 use OpenSRF::Transport::SlimJabber::XMPPMessage;
563 use OpenSRF::Utils::Logger qw($logger);
564 use OpenSRF::DomainObject::oilsResponse qw/:status/;
565 use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
566 use Time::HiRes qw(time usleep);
567 use POSIX qw/:sys_wait_h :errno_h/;
568
569 use overload '""' => sub { return '[' . shift()->{pid} . ']'; };
570
571 sub new {
572     my($class, $parent) = @_;
573     my $self = bless({}, $class);
574     $self->{pid} = 0; # my process ID
575     $self->{parent} = $parent; # Controller parent process
576     $self->{num_requests} = 0; # total serviced requests
577     $self->{sig_pipe} = 0;  # true if last syswrite failed
578     return $self;
579 }
580
581 sub set_nonblock {
582     my($self, $fh) = @_;
583     my  $flags = fcntl($fh, F_GETFL, 0);
584     fcntl($fh, F_SETFL, $flags | O_NONBLOCK);
585 }
586
587 sub set_block {
588     my($self, $fh) = @_;
589     my  $flags = fcntl($fh, F_GETFL, 0);
590     $flags &= ~O_NONBLOCK;
591     fcntl($fh, F_SETFL, $flags);
592 }
593
594 # ----------------------------------------------------------------
595 # Connects to Jabber and runs the application child_init
596 # ----------------------------------------------------------------
597 sub init {
598     my $self = shift;
599     my $service = $self->{parent}->{service};
600     $0 = "OpenSRF Drone [$service]";
601     OpenSRF::Transport::PeerHandle->construct($service);
602     OpenSRF::Application->application_implementation->child_init
603         if (OpenSRF::Application->application_implementation->can('child_init'));
604 }
605
606 # ----------------------------------------------------------------
607 # Waits for messages from the parent process, handles the message,
608 # then goes into the keepalive loop if this is a stateful session.
609 # When max_requests is hit, the process exits.
610 # ----------------------------------------------------------------
611 sub run {
612     my $self = shift;
613     my $network = OpenSRF::Transport::PeerHandle->retrieve;
614
615     # main child run loop.  Ends when this child hits max requests.
616     while(1) {
617
618         my $data = $self->wait_for_request or next;
619
620         # Update process name to show activity
621         my $orig_name = $0;
622         $0 = "$0*";
623
624         # Discard extraneous data from the jabber socket
625         if(!$network->flush_socket()) {
626             $logger->error("server: network disconnected!  child dropping request and exiting: $data");
627             exit;
628         }
629
630         my $session = OpenSRF::Transport->handler(
631             $self->{parent}->{service},
632             OpenSRF::Transport::SlimJabber::XMPPMessage->new(xml => $data)
633         );
634
635         $self->keepalive_loop($session);
636
637         last if ++$self->{num_requests} == $self->{parent}->{max_requests};
638
639         # Tell the parent process we are available to process requests
640         $self->send_status;
641
642         # Repair process name
643         $0 = $orig_name;
644     }
645
646     $chatty and $logger->internal("server: child process shutting down after reaching max_requests");
647
648     OpenSRF::Application->application_implementation->child_exit
649         if (OpenSRF::Application->application_implementation->can('child_exit'));
650 }
651
652 # ----------------------------------------------------------------
653 # waits for a request data on the parent pipe and returns it.
654 # ----------------------------------------------------------------
655 sub wait_for_request {
656     my $self = shift;
657
658     my $data = ''; # final request data
659     my $buf_size = 4096; # default linux pipe_buf (atomic window, not total size)
660     my $read_pipe = $self->{pipe_to_parent};
661     my $bytes_needed; # size of the data we are about to receive
662     my $bytes_recvd; # number of bytes read so far
663     my $first_read = 1; # true for first loop iteration
664     my $read_error;
665
666     while (1) {
667
668         # wait for some data to start arriving
669         my $read_set = IO::Select->new;
670         $read_set->add($read_pipe);
671     
672         while (1) {
673             # if can_read is interrupted while blocking, 
674             # go back and wait again until it succeeds.
675             last if $read_set->can_read;
676         }
677
678         # parent started writing, let's start reading
679         $self->set_nonblock($read_pipe);
680
681         while (1) {
682             # read all of the available data
683
684             my $buf = '';
685             my $nbytes = sysread($self->{pipe_to_parent}, $buf, $buf_size);
686
687             unless(defined $nbytes) {
688                 if ($! != EAGAIN) {
689                     $logger->error("server: error reading data from parent: $!.  ".
690                         "bytes_needed=$bytes_needed; bytes_recvd=$bytes_recvd; data=$data");
691                     $read_error = 1;
692                 }
693                 last;
694             }
695
696             last if $nbytes <= 0; # no more data available for reading
697
698             $bytes_recvd += $nbytes;
699             $data .= $buf;
700         }
701
702         $self->set_block($self->{pipe_to_parent});
703         return undef if $read_error;
704
705         # extract the data size and remove the header from the final data
706         if ($first_read) {
707             my $wps_size = OpenSRF::Server::WRITE_PIPE_DATA_SIZE;
708             $bytes_needed = int(substr($data, 0, $wps_size)) + $wps_size;
709             $data = substr($data, $wps_size);
710             $first_read = 0;
711         }
712
713
714         if ($bytes_recvd == $bytes_needed) {
715             # we've read all the data. Nothing left to do
716             last;
717         }
718
719         $logger->info("server: child process read all available pipe data.  ".
720             "waiting for more data from parent.  bytes_needed=$bytes_needed; bytes_recvd=$bytes_recvd");
721     }
722
723     return $data;
724 }
725
726
727 # ----------------------------------------------------------------
728 # If this is a stateful opensrf session, wait up to $keepalive
729 # seconds for subsequent requests from the client
730 # ----------------------------------------------------------------
731 sub keepalive_loop {
732     my($self, $session) = @_;
733     my $keepalive = $self->{parent}->{keepalive};
734
735     while($session->state and $session->state == $session->CONNECTED) {
736
737         unless( $session->queue_wait($keepalive) ) {
738
739             # client failed to disconnect before timeout
740             $logger->info("server: no request was received in $keepalive seconds, exiting stateful session");
741
742             my $res = OpenSRF::DomainObject::oilsConnectStatus->new(
743                 status => "Disconnected on timeout",
744                 statusCode => STATUS_TIMEOUT
745             );
746
747             $session->status($res);
748             $session->state($session->DISCONNECTED);
749             last;
750         }
751     }
752
753     $chatty and $logger->internal("server: child done with request(s)");
754     $session->kill_me;
755 }
756
757 # ----------------------------------------------------------------
758 # Report our availability to our parent process
759 # ----------------------------------------------------------------
760 sub send_status {
761     my $self = shift;
762
763     for (0..2) {
764
765         $self->{sig_pipe} = 0;
766         local $SIG{'PIPE'} = sub { $self->{sig_pipe} = 1; };
767
768         syswrite(
769             $self->{pipe_to_parent},
770             sprintf("%*s", OpenSRF::Server::STATUS_PIPE_DATA_SIZE, $self->{pid})
771         );
772
773         last unless $self->{sig_pipe};
774         $logger->error("server: $self got SIGPIPE writing status to parent, retrying...");
775         usleep(50000); # 50 msec
776     }
777
778     $logger->error("server: $self unable to send status to parent") if $self->{sig_pipe};
779 }
780
781
782 1;