| 1 | package Proc::Daemontools::Service; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 4 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 4 | ▶ | use Config; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 4 | my (%SIGNUM, %SIGMETH); | |||||||||||||||||||
| 8 | 4 | ▶ | BEGIN { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 9 | 4 | my $i = 0; | |||||||||||||||||||
| 10 | 4 | for my $name (split ' ', $Config{sig_name}) { | |||||||||||||||||||
| 11 | 272 | $SIGNUM{$name} = $i++; | |||||||||||||||||||
| 12 | } | ||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | %SIGMETH = ( | ||||||||||||||||||||
| 15 | 4 | INT => 'svc_interrupt', | |||||||||||||||||||
| 16 | HUP => 'svc_hangup', | ||||||||||||||||||||
| 17 | TERM => 'svc_terminate', | ||||||||||||||||||||
| 18 | ALRM => 'svc_alarm', | ||||||||||||||||||||
| 19 | ); | ||||||||||||||||||||
| 20 | } | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | =head1 NAME | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | Proc::Daemontools::Service - services that play nicely with daemontools | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | =head1 VERSION | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 0.02 | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | =cut | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | 4 | our $VERSION = '0.02'; | |||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | =head1 SYNOPSIS | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | package Foo::Service; | ||||||||||||||||||||
| 37 | use base qw(Proc::Daemontools::Service); | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | sub svc_up { ... } | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | # In other code... | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | my $serv = Foo::Service->new; | ||||||||||||||||||||
| 44 | $serv->run; | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | =head1 DESCRIPTION | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | See the daemontools page, at | ||||||||||||||||||||
| 49 | L<http://cr.yp.to/daemontools.html>, and particularly the | ||||||||||||||||||||
| 50 | svc page, at L<http://cr.yp.to/daemontools/svc.html>. | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | =head1 METHODS | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | =head2 C<< new >> | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | Takes no arguments (yet). | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | =cut | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 2 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||
| 61 | 2 | my $class = shift; | |||||||||||||||||||
| 62 | 2 | ▶ | die "no arguments to new" if @_; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 63 | 2 | return bless {} => $class; | |||||||||||||||||||
| 64 | } | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | =head2 C<< run >> | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | Install signal handlers and call C<< svc_run >>, which | ||||||||||||||||||||
| 69 | may continue indefinitely. | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | If C<< svc_run >> ever finishes, calls C<< exit >>. | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | =cut | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 1 | ▶ | sub run { | ||||||||||||||||||
|
Subroutine
run: called CC=1 SCAR=0.2
Pod
documented
| |||||||||||||||||||||
| 76 | 1 | my $self = shift; | |||||||||||||||||||
| 77 | 1 | $self->install_handlers; | |||||||||||||||||||
| 78 | 1 | $self->svc_run; | |||||||||||||||||||
| 79 | 0 | $self->exit(0); | |||||||||||||||||||
| 80 | } | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | =head2 C<< exit >> | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | $serv->exit($exit_status); | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | Exit, calling C<< svc_exit >> first if it exists. Default | ||||||||||||||||||||
| 87 | signal handlers call this. | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | =cut | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | 1 | ▶ | sub exit { | ||||||||||||||||||
|
Subroutine
exit: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 92 | 1 | my $self = shift; | |||||||||||||||||||
| 93 | 1 | ▶ | if ($self->can('svc_exit')) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 94 | 1 | $self->svc_exit; | |||||||||||||||||||
| 95 | } | ||||||||||||||||||||
| 96 | 1 | exit(shift); | |||||||||||||||||||
| 97 | } | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | =head2 C<< install_handlers >> | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | Install signal handlers to queue signals for processing by | ||||||||||||||||||||
| 102 | C<< svc_* >> methods, below. | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | NOTE: signal handlers are global. This means that two | ||||||||||||||||||||
| 105 | instances of Proc::Daemontools::Service will fight with each | ||||||||||||||||||||
| 106 | other. Don't do that. | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | =cut | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | 1 | ▶ | sub install_handlers { | ||||||||||||||||||
|
Subroutine
install_handlers: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 111 | 1 | my $self = shift; | |||||||||||||||||||
| 112 | 1 | require sigtrap; | |||||||||||||||||||
| 113 | 1 | my @args; | |||||||||||||||||||
| 114 | 1 | for my $sig (qw(HUP INT TERM ALRM)) { | |||||||||||||||||||
| 115 | 4 | ▶ | push @args, handler => sub { $self->_handle_signal($sig) } => $sig; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 116 | } | ||||||||||||||||||||
| 117 | 1 | sigtrap->import(@args); | |||||||||||||||||||
| 118 | } | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | 2 | ▶ | sub _handle_signal { | ||||||||||||||||||
|
Subroutine
_handle_signal: called CC=2 SCAR=7.0
| |||||||||||||||||||||
| 121 | 2 | my ($self, $signame) = @_; | |||||||||||||||||||
| 122 | my $arg = { | ||||||||||||||||||||
| 123 | signame => $signame, | ||||||||||||||||||||
| 124 | 2 | signum => $SIGNUM{$signame}, | |||||||||||||||||||
| 125 | }; | ||||||||||||||||||||
| 126 | 2 | ▶ | my $meth = $self->can($SIGMETH{$signame}) || $self->can('svc_default'); | ||||||||||||||||||
|
Condition66%2/3
$self->can($SIGMETH{$signame}) || $self->can('svc_default')
MC/DC0%0/2
$self->can($SIGMETH{$signame}) || $self->can('svc_default')
$self->can($SIGMETH{$signame})
$self->can('svc_default')
Truth table66%2/3
A || B
A= $self->can($SIGMETH{$signame})B= $self->can('svc_default')
| |||||||||||||||||||||
| 127 | 2 | $self->$meth($arg); | |||||||||||||||||||
| 128 | } | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | =head1 HOOKS | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | =head2 C<< svc_run >> | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | Called by C<< run >>. Your main program body should be here. | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | =head2 C<< svc_exit >> | ||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | Called by C<< exit >>. Any cleanup should be here. (optional) | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | =head1 SIGNALS | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | Subclasses should define their own copy of each of these | ||||||||||||||||||||
| 143 | methods. They will be called by Proc::Daemontools::Service | ||||||||||||||||||||
| 144 | as signals are caught. | ||||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | Names are taken from the full names of svc options. | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | When called, these methods will be passed a hashref | ||||||||||||||||||||
| 149 | indicating state. | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | =over 4 | ||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | =item B<signal> | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | the name of the signal (e.g. TERM) | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | =item B<signum> | ||||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | the number of the signal (e.g. 15) | ||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | =back | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | =head2 C<< svc_hangup >> | ||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | =head2 C<< svc_alarm >> | ||||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | =head2 C<< svc_interrupt >> | ||||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | =head2 C<< svc_terminate >> | ||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | =head1 DEFAULT HANDLERS | ||||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | Uncaught signals will cause your program to exit. If your | ||||||||||||||||||||
| 174 | package defines a C<< svc_exit >> method, it will be called | ||||||||||||||||||||
| 175 | before exiting (see L</exit>). | ||||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | The exit value will be the number of the signal that caused | ||||||||||||||||||||
| 178 | program exit. | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | =head2 C<< svc_default >> | ||||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | Override this method to provide your own default for the | ||||||||||||||||||||
| 183 | signals listed above. | ||||||||||||||||||||
| 184 | |||||||||||||||||||||
| 185 | =cut | ||||||||||||||||||||
| 186 | |||||||||||||||||||||
| 187 | 1 | ▶ | sub svc_default { | ||||||||||||||||||
|
Subroutine
svc_default: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 188 | 1 | my ($self, $arg) = @_; | |||||||||||||||||||
| 189 | 1 | $self->exit($arg->{signum}); | |||||||||||||||||||
| 190 | } | ||||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | =head1 UNCATCHABLE SIGNALS | ||||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | =head2 C<< KILL >> | ||||||||||||||||||||
| 195 | |||||||||||||||||||||
| 196 | =head2 C<< STOP >> | ||||||||||||||||||||
| 197 | |||||||||||||||||||||
| 198 | =head2 C<< CONT >> | ||||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | Technically CONT isn't uncatchable; however, given that you | ||||||||||||||||||||
| 201 | can't catch STOP, you probably don't want to catch CONT | ||||||||||||||||||||
| 202 | either. | ||||||||||||||||||||
| 203 | |||||||||||||||||||||
| 204 | =head1 AUTHOR | ||||||||||||||||||||
| 205 | |||||||||||||||||||||
| 206 | Hans Dieter Pearcey, C<< <hdp@cpan.org> >> | ||||||||||||||||||||
| 207 | |||||||||||||||||||||
| 208 | =head1 BUGS | ||||||||||||||||||||
| 209 | |||||||||||||||||||||
| 210 | Please report any bugs or feature requests to | ||||||||||||||||||||
| 211 | C<bug-proc-daemontools-service@rt.cpan.org>, or through the web interface at | ||||||||||||||||||||
| 212 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Proc-Daemontools-Service>. | ||||||||||||||||||||
| 213 | I will be notified, and then you'll automatically be notified of progress on | ||||||||||||||||||||
| 214 | your bug as I make changes. | ||||||||||||||||||||
| 215 | |||||||||||||||||||||
| 216 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||
| 217 | |||||||||||||||||||||
| 218 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||
| 219 | |||||||||||||||||||||
| 220 | Copyright 2006 Hans Dieter Pearcey, all rights reserved. | ||||||||||||||||||||
| 221 | |||||||||||||||||||||
| 222 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||
| 223 | under the same terms as Perl itself. | ||||||||||||||||||||
| 224 | |||||||||||||||||||||
| 225 | =cut | ||||||||||||||||||||
| 226 | |||||||||||||||||||||
| 227 | 4 | 1; # End of Proc::Daemontools::Service | |||||||||||||||||||