… | |
… | |
38 | Use AnyEvent::Watchdog to automatically restart the program |
38 | Use AnyEvent::Watchdog to automatically restart the program |
39 | when it fails to handle events for longer than 5 minutes: |
39 | when 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 | |
45 | The module supports the following functions: |
45 | The 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 | |
56 | use Carp (); |
56 | use Carp (); |
57 | |
57 | |
58 | our $VERSION = '0.9'; |
58 | our $VERSION = '0.9'; |
59 | |
59 | |
|
|
60 | =item $AnyEvent::Watchdog::ENABLED |
|
|
61 | |
|
|
62 | This is true when the program is running under the regime of |
|
|
63 | AnyEvent::Watchdog. Semi-obviously, you should I<NOT> C<use> or C<require> |
|
|
64 | this module before looking at this variable, and neither should you try |
|
|
65 | to load this module unless in the main program, rather use an idiom like |
|
|
66 | this: |
|
|
67 | |
|
|
68 | $AnyEvent::Watchdog::ENABLED |
|
|
69 | or die "watchdog not enabled..."; |
|
|
70 | AnyEvent::Watchdog::restart (60); # MUST use () |
|
|
71 | |
|
|
72 | Note that if this variable is defined, but false, then AnyEvent::Watchdog |
|
|
73 | is running, but you are in the watchdog process - you probably did |
|
|
74 | something very wrong in this case. |
|
|
75 | |
|
|
76 | =cut |
|
|
77 | |
60 | our $PID; # child pid |
78 | our $PID; # child pid |
61 | our $ENABLED = 1; |
79 | our $ENABLED = 0; |
62 | our $AUTORESTART; # actually exit |
80 | our $AUTORESTART; # actually exit |
63 | our $HEARTBEAT; |
81 | our $HEARTBEAT; |
64 | our ($P, $C); |
82 | our ($P, $C); |
65 | |
83 | |
66 | sub poll($) { |
84 | sub poll($) { |
… | |
… | |
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 | } |