ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-Watchdog/Watchdog.pm
(Generate patch)

Comparing AnyEvent-Watchdog/Watchdog.pm (file contents):
Revision 1.4 by root, Fri Aug 14 23:07:09 2009 UTC vs.
Revision 1.6 by root, Sun Aug 30 17:20:27 2009 UTC

38Use AnyEvent::Watchdog to automatically restart the program 38Use AnyEvent::Watchdog to automatically restart the program
39when it fails to handle events for longer than 5 minutes: 39when it fails to handle events for longer than 5 minutes:
40 40
41 use AnyEvent::Watchdog qw(autorestart heartbeat=300); 41 use AnyEvent::Watchdog qw(autorestart heartbeat=300);
42 42
43=head1 FUNCTIONS 43=head1 VARIABLES/FUNCTIONS
44 44
45The module supports the following functions: 45The module supports the following variables and functions:
46 46
47=over 4 47=over 4
48 48
49=cut 49=cut
50 50
55 55
56use Carp (); 56use Carp ();
57 57
58our $VERSION = '0.9'; 58our $VERSION = '0.9';
59 59
60=item $AnyEvent::Watchdog::ENABLED
61
62This is true when the program is running under the regime of
63AnyEvent::Watchdog. Semi-obviously, you should I<NOT> C<use> or C<require>
64this module before looking at this variable, and neither should you try
65to load this module unless in the main program, rather use an idiom like
66this:
67
68 $AnyEvent::Watchdog::ENABLED
69 or die "watchdog not enabled...";
70 AnyEvent::Watchdog::restart (60); # MUST use ()
71
72Note that if this variable is defined, but false, then AnyEvent::Watchdog
73is running, but you are in the watchdog process - you probably did
74something very wrong in this case.
75
76=cut
77
60our $PID; # child pid 78our $PID; # child pid
61our $ENABLED = 1; 79our $ENABLED = 0;
62our $AUTORESTART; # actually exit 80our $AUTORESTART; # actually exit
63our $HEARTBEAT; 81our $HEARTBEAT;
64our ($P, $C); 82our ($P, $C);
65 83
66sub poll($) { 84sub poll($) {
164 182
165our %SEEKPOS; 183our %SEEKPOS;
166# due to bugs in perl, try to remember file offsets for all fds, and restore them later 184# due to bugs in perl, try to remember file offsets for all fds, and restore them later
167# (the parser otherwise exhausts the input files) 185# (the parser otherwise exhausts the input files)
168 186
169# this causes perlio to flush it's handles internally, so 187# this causes perlio to flush its handles internally, so
170# seek offsets become correct. 188# seek offsets become correct.
171exec "."; # toi toi toi 189exec "."; # toi toi toi
172#{ 190#{
173# local $SIG{CHLD} = 'DEFAULT'; 191# local $SIG{CHLD} = 'DEFAULT';
174# my $pid = fork; 192# my $pid = fork;
178# } else { 196# } else {
179# kill 9, $$; 197# kill 9, $$;
180# } 198# }
181#} 199#}
182 200
183# now records all fd positions 201# now record "all" fd positions, assuming 1023 is more than enough.
184for (0 .. 1023) { 202for (0 .. 1023) {
185 open my $fh, "<&$_" or next; 203 open my $fh, "<&$_" or next;
186 $SEEKPOS{$_} = (sysseek $fh, 0, 1 or next); 204 $SEEKPOS{$_} = (sysseek $fh, 0, 1 or next);
187} 205}
188 206
203 221
204 unless (defined $PID) { 222 unless (defined $PID) {
205 warn "AnyEvent::Watchdog: '$!', retrying in one second...\n"; 223 warn "AnyEvent::Watchdog: '$!', retrying in one second...\n";
206 sleep 1; 224 sleep 1;
207 } elsif ($PID) { 225 } elsif ($PID) {
226 # parent code
208 close $C; 227 close $C;
209 server; 228 server;
210 } else { 229 } else {
230 # child code
231 $ENABLED = 1;
232
211 # restore seek offsets 233 # restore seek offsets
212 while (my ($k, $v) = each %SEEKPOS) { 234 while (my ($k, $v) = each %SEEKPOS) {
213 open my $fh, "<&$k" or next; 235 open my $fh, "<&$k" or next;
214 sysseek $fh, $v, 0; 236 sysseek $fh, $v, 0;
215 } 237 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines