| 1 | package App::CLI::Plugin::Proc::PID::File; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =pod | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | App::CLI::Plugin::Proc::PID::File - for App::CLI::Extension pidfile plugin module | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 VERSION | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1.3 | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | # MyApp.pm | ||||||||||||||||||||||||||||
| 16 | package MyApp; | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | use strict; | ||||||||||||||||||||||||||||
| 19 | use base qw(App::CLI::Extension); | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | # extension method | ||||||||||||||||||||||||||||
| 22 | __PACKAGE__->load_plugins(qw(Proc::PID::File)); | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | # extension method | ||||||||||||||||||||||||||||
| 25 | __PACKAGE__->config( proc_pid_file => { verify => 1, dir => "/var/run", name => "myapp" } ); | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 1; | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | # MyApp/Hello.pm | ||||||||||||||||||||||||||||
| 30 | package MyApp::Hello; | ||||||||||||||||||||||||||||
| 31 | use strict; | ||||||||||||||||||||||||||||
| 32 | use feature ":5.10.0"; | ||||||||||||||||||||||||||||
| 33 | use base qw(App::CLI::Command); | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | sub run { | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | my($self, @args) = @_; | ||||||||||||||||||||||||||||
| 38 | # make pid file (/var/run/myapp.pid) | ||||||||||||||||||||||||||||
| 39 | # /var/run/myapp.pid is automatically deleted (by Proc::PID::File::DESTROY) | ||||||||||||||||||||||||||||
| 40 | $self->pf->touch; | ||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | App::CLI::Extension pidfile plugin module | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | pf method setting | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | __PACKAGE__->config( proc_pid_file => {%proc_pid_file_option} ); | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | Proc::PID::File option is L<Proc::PID::File> please refer to | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =cut | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 56 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 57 | 3 | ▶ | use base qw(Class::Accessor::Grouped); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 58 | 3 | ▶ | use Fcntl qw(:DEFAULT :flock); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 59 | 3 | ▶ | use File::Basename; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 60 | 3 | ▶ | use File::Path; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 61 | 3 | ▶ | use Proc::PID::File; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 3 | __PACKAGE__->mk_group_accessors(inherited => "pf"); | |||||||||||||||||||||||||||
| 64 | 3 | our $VERSION = '1.3'; | |||||||||||||||||||||||||||
| 65 | 3 | our $PROC_PID_FILE_RECOMENDED_VERSION = '1.37'; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | =pod | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | =head1 EXTENDED METHOD | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | =head2 Proc::PID::File::path | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | return pidfile path | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | Example: | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | # MyApp::Hello(App::CLI::Command base package) | ||||||||||||||||||||||||||||
| 78 | sub run { | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | my($self, @args) = @_; | ||||||||||||||||||||||||||||
| 81 | say $self->pf->path; | ||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | =cut | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 3 | *Proc::PID::File::path = \&_path; | |||||||||||||||||||||||||||
| 87 | 3 | ▶ | if ($Proc::PID::File::VERSION < $PROC_PID_FILE_RECOMENDED_VERSION) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | { | ||||||||||||||||||||||||||||
| 89 | 3 | ▶ | no warnings "redefine"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 90 | 3 | *Proc::PID::File::alive = \&_alive; | |||||||||||||||||||||||||||
| 91 | 3 | *Proc::PID::File::read = \&_read; | |||||||||||||||||||||||||||
| 92 | 3 | *Proc::PID::File::touch = \&_touch; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | =pod | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | =head1 METHOD | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | =head2 pf | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | return Proc::PID::File object. | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | Specify the process ID of the file that describes the default Proc::PID::File in the specified or default values are applied, dir, name, but you may specify a combination of an optional extension to specify the pidfile possible. | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | --pidfile command line option if you also have to be defined by the specified module, --pidfile file path specified in the process ID can also be used as a file that describes the | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | Example1. Proc::PID::File pidfile config | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | # in MyApp.pm | ||||||||||||||||||||||||||||
| 111 | __PACKAGE__->config( | ||||||||||||||||||||||||||||
| 112 | proc_pid_file => { | ||||||||||||||||||||||||||||
| 113 | pidfile => "/tmp/myapp.pid", | ||||||||||||||||||||||||||||
| 114 | ############################### | ||||||||||||||||||||||||||||
| 115 | # Following equivalent | ||||||||||||||||||||||||||||
| 116 | ############################### | ||||||||||||||||||||||||||||
| 117 | # dir => "/tmp", | ||||||||||||||||||||||||||||
| 118 | # name => "myapp" | ||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | ); | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | Example2. pidfile option | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | myapp --pidfile=/tmp/myapp.pid | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =cut | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 0 | ▶ | sub setup { | ||||||||||||||||||||||||||
|
Subroutine
setup: not called CC=9 SCAR=45.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 0 | my($self, @argv) = @_; | |||||||||||||||||||||||||||
| 131 | 0 | my $pidfile; | |||||||||||||||||||||||||||
| 132 | 0 | ▶ | my %option = (exists $self->config->{proc_pid_file}) ? %{$self->config->{proc_pid_file}} : (); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 0 | ▶ | if (exists $option{pidfile} && defined $option{pidfile}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
exists $option{"pidfile"} and defined $option{"pidfile"}
MC/DC0%0/2
exists $option{"pidfile"} and defined $option{"pidfile"}
exists $option{"pidfile"}
defined $option{"pidfile"}
Truth table0%0/3
A and B
A= exists $option{"pidfile"}B= defined $option{"pidfile"}
| |||||||||||||||||||||||||||||
| 134 | 0 | $pidfile = $option{pidfile}; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | 0 | ▶ | if (exists $self->{pidfile} && defined $self->{pidfile}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
exists $self->{"pidfile"} and defined $self->{"pidfile"}
MC/DC0%0/2
exists $self->{"pidfile"} and defined $self->{"pidfile"}
exists $self->{"pidfile"}
defined $self->{"pidfile"}
Truth table0%0/3
A and B
A= exists $self->{"pidfile"}B= defined $self->{"pidfile"}
| |||||||||||||||||||||||||||||
| 137 | 0 | $pidfile = $self->{pidfile}; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 0 | ▶ | if (defined $pidfile) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | # get name and path. fileparse is File::Basename function | ||||||||||||||||||||||||||||
| 142 | 0 | my($name, $path) = fileparse($pidfile, qr/\.[^.]*$/); | |||||||||||||||||||||||||||
| 143 | 0 | $option{name} = $name; | |||||||||||||||||||||||||||
| 144 | 0 | $option{dir} = $path; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | # make directory. mkpath is File::Path function | ||||||||||||||||||||||||||||
| 148 | 0 | ▶ | if (exists $option{dir} && !-d $option{dir}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
exists $option{"dir"} and not -d $option{"dir"}
MC/DC0%0/2
exists $option{"dir"} and not -d $option{"dir"}
exists $option{"dir"}
not -d $option{"dir"}
Truth table0%0/3
A and not(B)
A= exists $option{"dir"}B= not -d $option{"dir"}
| |||||||||||||||||||||||||||||
| 149 | 0 | mkpath($option{dir}); | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 0 | $self->pf(Proc::PID::File->new(%option)); | |||||||||||||||||||||||||||
| 153 | 0 | $self->maybe::next::method(@argv); | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | #################################### | ||||||||||||||||||||||||||||
| 157 | # Proc::PID::File extended method | ||||||||||||||||||||||||||||
| 158 | #################################### | ||||||||||||||||||||||||||||
| 159 | 0 | ▶ | sub _path { | ||||||||||||||||||||||||||
|
Subroutine
_path: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 162 | 0 | return $self->{path}; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 0 | ▶ | sub _alive { | ||||||||||||||||||||||||||
|
Subroutine
_alive: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 168 | 0 | $self->debug("alive(): for A::C::P::Proc::PID::File compat method"); | |||||||||||||||||||||||||||
| 169 | 0 | my $pid = $self->read; | |||||||||||||||||||||||||||
| 170 | 0 | ▶ | if (defined $pid) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | 0 | $self->debug("alive(): $pid"); | |||||||||||||||||||||||||||
| 172 | } else { | ||||||||||||||||||||||||||||
| 173 | 0 | $self->debug("alive(): not living my process"); | |||||||||||||||||||||||||||
| 174 | 0 | return 0; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 0 | ▶ | if ($pid != $$ && kill(0, $pid)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$pid != $$ and kill 0, $pid
MC/DC0%0/2
$pid != $$ and kill 0, $pid
$pid != $$
kill 0, $pid
Truth table0%0/3
A and B
A= $pid != $$B= kill 0, $pid
| |||||||||||||||||||||||||||||
| 178 | 0 | ▶ | return $self->verify($pid) ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | 0 | return 0; | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 0 | ▶ | sub _read { | ||||||||||||||||||||||||||
|
Subroutine
_read: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 186 | 0 | $self->debug("read(): for A::C::P::Proc::PID::File compat method"); | |||||||||||||||||||||||||||
| 187 | 0 | ▶ | if (!-e $self->path) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | 0 | return; | |||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | 0 | ▶ | open my $fh, "<", $self->path or die "can not open file ". $self->path . ": $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 191 | 0 | ▶ | flock $fh, LOCK_EX | LOCK_NB or die "can not flock file " . $self->path . ": $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 192 | 0 | my($pid) = <$fh> =~ /^(\d{1,})$/; | |||||||||||||||||||||||||||
| 193 | 0 | ▶ | close $fh or die "can not close file " . $self->path . ": $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 0 | $self->debug(sprintf "read(%s) = $pid", $self->path); | |||||||||||||||||||||||||||
| 196 | 0 | return $pid; | |||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 0 | ▶ | sub _touch { | ||||||||||||||||||||||||||
|
Subroutine
_touch: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 202 | 0 | $self->debug("touch(): for A::C::P::Proc::PID::File compat method"); | |||||||||||||||||||||||||||
| 203 | 0 | $self->debug("write($$)"); | |||||||||||||||||||||||||||
| 204 | 0 | ▶ | open my $fh, ">", $self->path or die "can not open file ". $self->path . ": $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 205 | 0 | ▶ | flock $fh, LOCK_EX | LOCK_NB or die "can not flock file " . $self->path . ": $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 206 | 0 | print $fh "$$\n"; | |||||||||||||||||||||||||||
| 207 | 0 | ▶ | close $fh or die "can not close file " . $self->path . ": $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | 3 | 1; | |||||||||||||||||||||||||||
| 212 | __END__ | ||||||||||||||||||||||||||||