blib/lib/POE/Wheel/Run/DaemonHelper.pm

filter: Statementstmt 81.8% 9 / 11 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 86.6% 13 / 15 CC 1 SCAR 0.7
CC
1
Cov
82%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package POE::Wheel::Run::DaemonHelper;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 0 ▶use POE qw( Wheel::Run );
Subroutine BEGIN: called CC=1 SCAR=2.6
7 use base 'Error::Helper';
8 use Algorithm::Backoff::Exponential;
9 use Sys::Syslog;
10 use File::Slurp qw(append_file read_file);
11  
12 =head1 NAME
13  
14 POE::Wheel::Run::DaemonHelper - Helper for the POE::Wheel::Run for easy controlling logging of stdout/err as well as restarting with backoff.
15  
16 =head1 VERSION
17  
18 Version 0.1.0
19  
20 =cut
21  
22 our $VERSION = '0.1.0';
23  
24 =head1 SYNOPSIS
25  
26 use strict;
27 use warnings;
28 use POE::Wheel::Run::DaemonHelper;
29 use POE;
30  
31 my $program = 'sleep 1; echo test; derp derp derp';
32  
33 my $dh = POE::Wheel::Run::DaemonHelper->new(
34 program => $program,
35 status_syslog => 1,
36 restart_ctl => 1,
37 status_print => 1,
38 status_print_warn => 1,
39 # this one will be ignored as it will already be warning for print
40 status_syslog_warn => 1,
41 );
42  
43 $dh->create_session;
44  
45 POE::Kernel->run();
46  
47 =head1 METHODS
48  
49 =head2 new
50  
51 Required args as below.
52  
53 - program :: The program to execute. Either a string or array.
54 Default :: undef
55  
56 - restart_ctl :: Control if it will be restarted if it dies.
57 Default :: 1
58  
59 Optional args are as below.
60  
61 - syslog_name :: The name to use when sending stuff to syslog.
62 Default :: DaemonHelper
63  
64 - pid_file :: The file to check for additional PIDs. Used for for
65 with the $dh->pids and $dh->pid_from_pid_file.
66 Default :: undef
67  
68 - default_kill_signal :: The default signal to use for kill.
69 Default :: TERM
70  
71 The following optional args control the backoff. Backoff is handled by
72 L<Algorithm::Backoff::Exponential> with consider_actual_delay and delay_on_success
73 set to true. The following are passed to it.
74  
75 - max_delay :: Max backoff delay in seconds when a program exits quickly.
76 Default :: 90
77  
78 - initial_delay :: Initial backoff amount.
79 Default :: 2
80  
81 The following optional args control the how the log_message method behaves.
82  
83 - syslog_facility :: The syslog facility to log to.
84 Default :: daemon
85  
86 - stdout_prepend :: What to prepend to STDOUT lines sent for status logging.
87 Default :: Out:
88  
89 - stderr_prepend :: What to prepend to STDERR lines sent to status logging.
90 Default :: Err:
91  
92 - status_print :: Print statuses messages to stdout.
93 Default :: 0
94  
95 - status_print_warn :: For when error is true, use warn.
96 Default :: 0
97  
98 - status_syslog :: Send status messages to syslog
99 Default :: 1
100  
101 - status_syslog_warn :: Warn for error messages going to syslog. Warn will only be used once.
102 Default :: 0
103  
104 =cut
105  
106 sub new {
107 my ( $blank, %opts ) = @_;
108  
109 my $self = {
110 perror => undef,
111 error => undef,
112 errorLine => undef,
113 errorFilename => undef,
114 errorString => "",
115 errorExtra => {
116 all_errors_fatal => 1,
117 flags => {
118 1 => 'invalidProgram',
119 2 => 'optsBadRef',
120 3 => 'optsNotInt',
121 4 => 'readPidFileFailed',
122 5 => 'killFailed',
123 },
124 fatal_flags => {},
125 perror_not_fatal => 0,
126 },
127 args => {
128 ints => {
129 'max_delay' => 1,
130 'initial_delay' => 1,
131 },
132 args => [
133 'syslog_name', 'syslog_facility', 'stdout_prepend', 'stderr_prepend',
134 'max_delay', 'initial_delay', 'status_syslog', 'status_print',
135 'status_print_warn', 'status_syslog_warn', 'restart_ctl', 'pid_file',
136 'default_kill_signal',
137 ],
138 },
139 program => undef,
140 syslog_name => 'DaemonHelper',
141 syslog_facility => 'daemon',
142 stdout_prepend => 'Out: ',
143 stderr_prepend => 'Err: ',
144 max_delay => 90,
145 initial_delay => 2,
146 session_created => 0,
147 started => undef,
148 started_at => undef,
149 restart_ctl => 1,
150 backoff => undef,
151 pid => undef,
152 status_syslog => 1,
153 status_syslog_warn => 0,
154 status_print => 0,
155 status_print_warn => 0,
156 append_pid => 0,
157 pid_prepend => 1,
158 pid_file => undef,
159 default_kill_signal => 'TERM',
160 };
161 bless $self;
162  
163 if ( !defined( $opts{program} ) ) {
164 $self->{perror} = 1;
165 $self->{error} = 1;
166 $self->{errorString} = 'program is defined';
167 $self->warn;
168 return;
169 } elsif ( ref( $opts{program} ) ne '' && ref( $opts{program} ) ne 'ARRAY' ) {
170 $self->{perror} = 1;
171 $self->{error} = 1;
172 $self->{errorString} = 'ref for program is ' . ref( $opts{program} ) . ', but should be either "" or ARRAY';
173 $self->warn;
174 return;
175 }
176 $self->{program} = $opts{program};
177  
178 foreach my $arg ( @{ $self->{args}{args} } ) {
179 if ( defined( $opts{$arg} ) ) {
180 if ( ref( $opts{$arg} ) ne '' ) {
181 $self->{perror} = 1;
182 $self->{error} = 2;
183 $self->{errorString} = 'ref for ' . $arg . ' is ' . ref( $opts{$arg} ) . ', but should be ""';
184 $self->warn;
185 return;
186 }
187  
188 if ( $self->{args}{ints}{$arg} && $opts{$arg} !~ /^[0-9]+$/ ) {
189 $self->{perror} = 1;
190 $self->{error} = 3;
191 $self->{errorString} = $arg . ' is "' . $opts{$arg} . '" and does not match /^[0-9]+$/';
192 $self->warn;
193 return;
194 }
195  
196 $self->{$arg} = $opts{$arg};
197 } ## end if ( defined( $opts{$arg} ) )
198 } ## end foreach my $arg ( @{ $self->{args}{args} } )
199  
200 eval {
201 $self->{backoff} = Algorithm::Backoff::Exponential->new(
202 initial_delay => $self->{initial_delay},
203 max_delay => $self->{max_delay},
204 consider_actual_delay => 1,
205 delay_on_success => 1,
206 );
207 };
208 if ($@) {
209 die($@);
210 }
211  
212 return $self;
213 } ## end sub new
214  
215 =head2 create_session
216  
217 This creates the new POE session that will handle this.
218  
219 $dh->create_session;
220  
221 =cut
222  
223 sub create_session {
224 my ( $self, %opts ) = @_;
225  
226 $self->errorblank;
227  
228 POE::Session->create(
229 inline_states => {
230 _start => \&on_start,
231 got_child_stdout => \&on_child_stdout,
232 got_child_stderr => \&on_child_stderr,
233 got_child_close => \&on_child_close,
234 got_child_signal => \&on_child_signal,
235 },
236 heap => { self => $self },
237 );
238  
239 return;
240 } ## end sub create_session
241  
242 =head2 log_message
243  
244 Logs a message. Printing to stdout or sending to syslog is controlled via
245 the status_syslog and status_print values passed to new.
246  
247  
248  
249 - status :: What to log.
250 Default :: undef
251  
252 - error :: If true, this will set the log level from info to err.
253 Default :: 0
254  
255 =cut
256  
257 sub log_message {
258 my ( $self, %opts ) = @_;
259  
260 $self->errorblank;
261  
262 if ( !defined( $opts{status} ) ) {
263 return;
264 }
265  
266 my $level = 'info';
267 if ( $opts{error} ) {
268 $level = 'err';
269 }
270  
271 # used for making sure we only use warn once.
272 my $warned = 0;
273  
274 if ( $self->{status_print} ) {
275 if ( $self->{status_print_warn} && $opts{error} ) {
276 warn( $self->{syslog_name} . '[' . $$ . '] ' . $opts{status} );
277 $warned = 1;
278 } else {
279 print $self->{syslog_name} . '[' . $$ . '] ' . $opts{status} . "\n";
280 }
281 }
282  
283 if ( $self->{status_syslog} ) {
284 if ( $self->{status_syslog_warn} && $opts{error} && !$warned ) {
285 warn( $self->{syslog_name} . '[' . $$ . '] ' . $opts{status} );
286 }
287 eval {
288 openlog( $self->{syslog_name}, '', $self->{syslog_facility} );
289 syslog( $level, $opts{status} );
290 closelog();
291 };
292 if ($@) {
293 warn( 'Errored logging message... ' . $@ );
294 }
295 } ## end if ( $self->{status_syslog} )
296 } ## end sub log_message
297  
298 =head2 kill
299  
300 Sends the specified signal to the PIDs.
301  
302 Returns undef if there are no PIDs, meaning it is not running.
303  
304 If the signal is not supported, the error 5, killFailed, is set.
305  
306 For understanding the return value, see the docs for the Perl
307 function kill.
308  
309 If you want to see the available signals,
310 check L<Config> and $Config{sig_name}.
311  
312 - signal :: The signal to send. The default is conntrolled
313 by the setting of the default_kill_signal setting.
314  
315 # send the default signal
316 my $count=$dh->kill;
317  
318 # send the KILL signal
319 my $count;
320 eval{ $count=$dh->kill(signal=>'KILL'); };
321 if ($@ && $Error::Helper::errorFlag eq 'killFailed') {
322 die('Unkown kill signal used');
323 } elsif ($@) {
324 die($@);
325 } elsif ( $count < 1 ) {
326 die('Failed to kill any of the procs');
327 }
328 print $count . " procs signaled\n";
329  
330 =cut
331  
332 sub kill {
333 my ( $self, %opts ) = @_;
334  
335 $self->errorblank;
336  
337 if ( !defined( $opts{signal} ) ) {
338 $opts{signal} = $self->{default_kill_signal};
339 }
340  
341 my @pids = $self->pids;
342  
343 if ( !defined( $pids[0] ) ) {
344 return undef;
345 }
346  
347 my $count;
348 eval { $count = kill $opts{signal}, @pids; };
349 if ($@) {
350 $self->{error} = 5;
351 $self->{errorString}
352 = 'Died trying to send kill signal "' . $opts{signal} . '" to pids ' . join( ',', @pids ) . ' ... ' . $@;
353 $self->warn;
354 return undef;
355 }
356  
357 return $count;
358 } ## end sub kill
359  
360 =head2 pid
361  
362 Returns the PID of the process or undef if it
363 has not been started.
364  
365 This just return the child PID. Will not return
366 the PID from the PID file if one is set.
367  
368 my $pid = $dh->pid;
369 if ($pid){
370 print 'PID is '.$started_at."\n";
371 }
372  
373 =cut
374  
375 sub pid {
376 my ( $self, %opts ) = @_;
377  
378 $self->errorblank;
379  
380 return $self->{pid};
381 }
382  
383 =head2 pids
384  
385 Returns the child PID and PID from the PID file if one is specified.
386  
387 This calls pid_from_pid_file via eval and ignores if it fails. If you
388 want to check to see if that errored or not, check to see if error 4,
389 readPidFileFailed, was set or use both pid and pid_from_pid_file.
390  
391 my @pids = $dh->pids;
392 print 'PIDs are ' . join(', ', @pids) . "\n";
393  
394 =cut
395  
396 sub pids {
397 my ($self) = @_;
398  
399 $self->errorblank;
400  
401 my @pids;
402  
403 if ( defined( $self->{pid} ) ) {
404 push( @pids, $self->{pid} );
405 }
406  
407 my $pid_from_pid_file;
408 eval { $pid_from_pid_file = $self->pid_from_pid_file; };
409 if ( defined($pid_from_pid_file) ) {
410 push( @pids, $pid_from_pid_file );
411 }
412  
413 return @pids;
414 } ## end sub pids
415  
416 =head2 pid_from_pid_file
417  
418 Reads the PID from the PID file.
419  
420 If one was not specified or the file does not exist, it returns undef.
421  
422 Will throw error 4, readPidFileFailed, if it could not read it.
423  
424 After reading it, it will return the first integer.
425  
426 my $pid;
427 eval{ $pid = $dh->pid_from_pid_file; };
428 if ($@) {
429 print "Could not read PID file\n";
430 } elsif (defined ($pid)) {
431 print 'PID: ' . $pid . "\n";
432 }
433  
434 =cut
435  
436 sub pid_from_pid_file {
437 my ($self) = @_;
438  
439 $self->errorblank;
440  
441 if ( !defined( $self->{pid_file} ) ) {
442 return undef;
443 } elsif ( !-f $self->{pid_file} ) {
444 return undef;
445 }
446  
447 my $raw_pid_file;
448 eval { $raw_pid_file = read_file( $self->{pid_file} ); };
449 if ($@) {
450 $self->{error} = 4;
451 $self->{errorString} = 'Failed to read PID file, "' . $self->{pid_file} . '" ... ' . $@;
452 $self->warn;
453 return;
454 }
455  
456 my @raw_pid_file_split = split( /\n/, $raw_pid_file );
457 foreach my $line (@raw_pid_file_split) {
458 if ( $line =~ /^[0-9]+$/ ) {
459 return $line;
460 }
461 }
462  
463 return undef;
464 } ## end sub pid_from_pid_file
465  
466 =head2 restart_ctl
467  
468 Controls if the process will be restarted when it exits or not.
469  
470 - restart_ctl :: A Perl boolean that if true the process will
471 be restarted when it exits.
472 Default :: undef
473  
474 # next time it exits, it won't be restarted
475 $dh->restart_ctl(restart_ctl=>0);
476  
477 If restart_ctl is undef, the current value is returned.
478  
479 my $restart_ctl = $dh->restart_ctl;
480 if ($restart_ctl) {
481 print "Will be restarted when it dies.\n";
482 } else {
483 print "Will NOT be restarted when it dies.\n";
484 }
485  
486 =cut
487  
488 sub restart_ctl {
489 my ( $self, %opts ) = @_;
490  
491 $self->errorblank;
492  
493 if ( !defined( $opts{restart_ctl} ) ) {
494 return $self->{restart_ctl};
495 }
496  
497 $self->{restart_ctl} = $opts{restart_ctl};
498 } ## end sub restart_ctl
499  
500 =head2 started
501  
502 Returns a Perl boolean for if it has been started or not.
503  
504 my $started=$dh->started;
505 if ($started){
506 print 'started as '.$dh->pid."\n";
507 }
508  
509 =cut
510  
511 sub started {
512 my ( $self, %opts ) = @_;
513  
514 $self->errorblank;
515  
516 return $self->{started};
517 }
518  
519 =head2 started_at
520  
521 Returns the unix time it was (re)started at or undef if it has not
522 been started.
523  
524 my $started_at = $dh->started;
525 if ($started_at){
526 print 'started at '.$started_at."\n";
527 }
528  
529 =cut
530  
531 sub started_at {
532 my ( $self, %opts ) = @_;
533  
534 $self->errorblank;
535  
536 return $self->{started_at};
537 }
538  
539 sub on_start {
540  
541 my $child = POE::Wheel::Run->new(
542 StdioFilter => POE::Filter::Line->new(),
543 StderrFilter => POE::Filter::Line->new(),
544 Program => $_[HEAP]{self}->{program},
545 StdoutEvent => "got_child_stdout",
546 StderrEvent => "got_child_stderr",
547 CloseEvent => "got_child_close",
548 );
549  
550 $_[KERNEL]->sig_child( $child->PID, "got_child_signal" );
551  
552 # Wheel events include the wheel's ID.
553 $_[HEAP]{children_by_wid}{ $child->ID } = $child;
554  
555 # Signal events include the process ID.
556 $_[HEAP]{children_by_pid}{ $child->PID } = $child;
557  
558 $_[HEAP]{self}->log_message( status => 'Starting... ' . $_[HEAP]{self}->{program} );
559  
560 $_[HEAP]{self}->log_message( status => 'Child pid ' . $child->PID . ' started' );
561  
562 $_[HEAP]{self}{started} = 1;
563 $_[HEAP]{self}{pid} = $child->PID;
564 $_[HEAP]{self}{started_at} = time;
565 } ## end sub on_start
566  
567 sub on_child_stdout {
568 my ( $stdout_line, $wheel_id ) = @_[ ARG0, ARG1 ];
569 my $child = $_[HEAP]{children_by_wid}{$wheel_id};
570  
571 my $prepend = $_[HEAP]{self}->{stdout_prepend};
572 if ( $_[HEAP]{self}->{pid_prepend} ) {
573 $prepend = $_[HEAP]{self}->{pid} . ' ' . $prepend;
574 }
575  
576 $_[HEAP]{self}->log_message( status => $prepend . $stdout_line );
577 } ## end sub on_child_stdout
578  
579 sub on_child_stderr {
580 my ( $stderr_line, $wheel_id ) = @_[ ARG0, ARG1 ];
581 my $child = $_[HEAP]{children_by_wid}{$wheel_id};
582  
583 my $prepend = $_[HEAP]{self}->{stderr_prepend};
584 if ( $_[HEAP]{self}->{pid_prepend} ) {
585 $prepend = $_[HEAP]{self}->{pid} . ' ' . $prepend;
586 }
587  
588 $_[HEAP]{self}->log_message( error => 1, status => $prepend . $stderr_line );
589 } ## end sub on_child_stderr
590  
591 sub on_child_close {
592 my $wheel_id = $_[ARG0];
593 my $child = delete $_[HEAP]{children_by_wid}{$wheel_id};
594  
595 # May have been reaped by on_child_signal().
596 unless ( defined $child ) {
597 return;
598 }
599 $_[HEAP]{self}->log_message( status => $child->PID . ' closed all pipes.' );
600 delete $_[HEAP]{children_by_pid}{ $child->PID };
601 } ## end sub on_child_close
602  
603 sub on_child_signal {
604 my $error = 0;
605 if ( $_[ARG2] ne '0' ) {
606 $error = 1,;
607 }
608  
609 my $child = delete $_[HEAP]{children_by_pid}{ $_[ARG1] };
610  
611 $_[HEAP]{self}->log_message( error => $error, status => $_[ARG1] . ' exited with ' . $_[ARG2] );
612  
613 if ( defined($child) ) {
614 delete $_[HEAP]{children_by_wid}{ $child->ID };
615 }
616  
617 my $secs;
618 if ( !$error ) {
619 $secs = $_[HEAP]{self}{backoff}->success;
620 } else {
621 $secs = $_[HEAP]{self}{backoff}->failure;
622 }
623  
624 if ( $_[HEAP]{self}->{restart_ctl} ) {
625 $_[HEAP]{self}->log_message( status => 'restarting in ' . $secs . ' seconds' );
626  
627 $_[KERNEL]->delay( _start => 3 );
628 } else {
629 $_[HEAP]{self}->log_message( status => 'restart_ctl false... not restarting' );
630 }
631 } ## end sub on_child_signal
632  
633 =head1 ERROR CODES / FLAGS
634  
635 =head2 1, invalidProgram
636  
637 No program is specified.
638  
639 =head2 2, optsBadRef
640  
641 The opts has a invlaid ref.
642  
643 =head2 3, optsNotInt
644  
645 The opts in question should be a int.
646  
647 =head2 4, readPidFileFailed
648  
649 Failed to read the PID file.
650  
651 =head2 5, killFailed
652  
653 Failed to run kill. This in general means a improper signal was specified.
654  
655 If you want to see the available signals, check L<Config> and $Config{sig_name}.
656  
657 =head1 AUTHOR
658  
659 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
660  
661 =head1 BUGS
662  
663 Please report any bugs or feature requests to C<bug-poe-wheel-run-daemonhelper at rt.cpan.org>, or through
664 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=POE-Wheel-Run-DaemonHelper>. I will be notified, and then you'll
665 automatically be notified of progress on your bug as I make changes.
666  
667  
668  
669  
670 =head1 SUPPORT
671  
672 You can find documentation for this module with the perldoc command.
673  
674 perldoc POE::Wheel::Run::DaemonHelper
675  
676  
677 You can also look for information at:
678  
679 =over 4
680  
681 =item * RT: CPAN's request tracker (report bugs here)
682  
683 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=POE-Wheel-Run-DaemonHelper>
684  
685 =item * Search CPAN
686  
687 L<https://metacpan.org/release/POE-Wheel-Run-DaemonHelper>
688  
689 =back
690  
691  
692 =head1 ACKNOWLEDGEMENTS
693  
694  
695 =head1 LICENSE AND COPYRIGHT
696  
697 This software is Copyright (c) 2024 by Zane C. Bowers-Hadley.
698  
699 This is free software, licensed under:
700  
701 The GNU Lesser General Public License, Version 2.1, February 1999
702  
703  
704 =cut
705  
706 1; # End of POE::Wheel::Run::DaemonHelper
↑ Summary