| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
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 please refer to |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=cut |
|
54
|
|
|
|
|
|
|
|
|
55
|
3
|
|
|
3
|
|
164139
|
use strict; |
|
|
3
|
|
|
|
|
6
|
|
|
|
3
|
|
|
|
|
124
|
|
|
56
|
3
|
|
|
3
|
|
20
|
use warnings; |
|
|
3
|
|
|
|
|
15
|
|
|
|
3
|
|
|
|
|
122
|
|
|
57
|
3
|
|
|
3
|
|
17
|
use base qw(Class::Accessor::Grouped); |
|
|
3
|
|
|
|
|
7
|
|
|
|
3
|
|
|
|
|
1576
|
|
|
58
|
3
|
|
|
3
|
|
19392
|
use Fcntl qw(:DEFAULT :flock); |
|
|
3
|
|
|
|
|
8
|
|
|
|
3
|
|
|
|
|
1956
|
|
|
59
|
3
|
|
|
3
|
|
22
|
use File::Basename; |
|
|
3
|
|
|
|
|
6
|
|
|
|
3
|
|
|
|
|
455
|
|
|
60
|
3
|
|
|
3
|
|
19
|
use File::Path; |
|
|
3
|
|
|
|
|
5
|
|
|
|
3
|
|
|
|
|
253
|
|
|
61
|
3
|
|
|
3
|
|
1632
|
use Proc::PID::File; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
__PACKAGE__->mk_group_accessors(inherited => "pf"); |
|
64
|
|
|
|
|
|
|
our $VERSION = '1.3'; |
|
65
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
*Proc::PID::File::path = \&_path; |
|
87
|
|
|
|
|
|
|
if ($Proc::PID::File::VERSION < $PROC_PID_FILE_RECOMENDED_VERSION) { |
|
88
|
|
|
|
|
|
|
{ |
|
89
|
|
|
|
|
|
|
no warnings "redefine"; |
|
90
|
|
|
|
|
|
|
*Proc::PID::File::alive = \&_alive; |
|
91
|
|
|
|
|
|
|
*Proc::PID::File::read = \&_read; |
|
92
|
|
|
|
|
|
|
*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
|
|
|
|
|
|
|
sub setup { |
|
129
|
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
my($self, @argv) = @_; |
|
131
|
|
|
|
|
|
|
my $pidfile; |
|
132
|
|
|
|
|
|
|
my %option = (exists $self->config->{proc_pid_file}) ? %{$self->config->{proc_pid_file}} : (); |
|
133
|
|
|
|
|
|
|
if (exists $option{pidfile} && defined $option{pidfile}) { |
|
134
|
|
|
|
|
|
|
$pidfile = $option{pidfile}; |
|
135
|
|
|
|
|
|
|
} |
|
136
|
|
|
|
|
|
|
if (exists $self->{pidfile} && defined $self->{pidfile}) { |
|
137
|
|
|
|
|
|
|
$pidfile = $self->{pidfile}; |
|
138
|
|
|
|
|
|
|
} |
|
139
|
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
if (defined $pidfile) { |
|
141
|
|
|
|
|
|
|
# get name and path. fileparse is File::Basename function |
|
142
|
|
|
|
|
|
|
my($name, $path) = fileparse($pidfile, qr/\.[^.]*$/); |
|
143
|
|
|
|
|
|
|
$option{name} = $name; |
|
144
|
|
|
|
|
|
|
$option{dir} = $path; |
|
145
|
|
|
|
|
|
|
} |
|
146
|
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
# make directory. mkpath is File::Path function |
|
148
|
|
|
|
|
|
|
if (exists $option{dir} && !-d $option{dir}) { |
|
149
|
|
|
|
|
|
|
mkpath($option{dir}); |
|
150
|
|
|
|
|
|
|
} |
|
151
|
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
$self->pf(Proc::PID::File->new(%option)); |
|
153
|
|
|
|
|
|
|
$self->maybe::next::method(@argv); |
|
154
|
|
|
|
|
|
|
} |
|
155
|
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
#################################### |
|
157
|
|
|
|
|
|
|
# Proc::PID::File extended method |
|
158
|
|
|
|
|
|
|
#################################### |
|
159
|
|
|
|
|
|
|
sub _path { |
|
160
|
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
my $self = shift; |
|
162
|
|
|
|
|
|
|
return $self->{path}; |
|
163
|
|
|
|
|
|
|
} |
|
164
|
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
sub _alive { |
|
166
|
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
my $self = shift; |
|
168
|
|
|
|
|
|
|
$self->debug("alive(): for A::C::P::Proc::PID::File compat method"); |
|
169
|
|
|
|
|
|
|
my $pid = $self->read; |
|
170
|
|
|
|
|
|
|
if (defined $pid) { |
|
171
|
|
|
|
|
|
|
$self->debug("alive(): $pid"); |
|
172
|
|
|
|
|
|
|
} else { |
|
173
|
|
|
|
|
|
|
$self->debug("alive(): not living my process"); |
|
174
|
|
|
|
|
|
|
return 0; |
|
175
|
|
|
|
|
|
|
} |
|
176
|
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
if ($pid != $$ && kill(0, $pid)) { |
|
178
|
|
|
|
|
|
|
return $self->verify($pid) ? 1 : 0; |
|
179
|
|
|
|
|
|
|
} |
|
180
|
|
|
|
|
|
|
return 0; |
|
181
|
|
|
|
|
|
|
} |
|
182
|
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
sub _read { |
|
184
|
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
my $self = shift; |
|
186
|
|
|
|
|
|
|
$self->debug("read(): for A::C::P::Proc::PID::File compat method"); |
|
187
|
|
|
|
|
|
|
if (!-e $self->path) { |
|
188
|
|
|
|
|
|
|
return; |
|
189
|
|
|
|
|
|
|
} |
|
190
|
|
|
|
|
|
|
open my $fh, "<", $self->path or die "can not open file ". $self->path . ": $!"; |
|
191
|
|
|
|
|
|
|
flock $fh, LOCK_EX | LOCK_NB or die "can not flock file " . $self->path . ": $!"; |
|
192
|
|
|
|
|
|
|
my($pid) = <$fh> =~ /^(\d{1,})$/; |
|
193
|
|
|
|
|
|
|
close $fh or die "can not close file " . $self->path . ": $!"; |
|
194
|
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
$self->debug(sprintf "read(%s) = $pid", $self->path); |
|
196
|
|
|
|
|
|
|
return $pid; |
|
197
|
|
|
|
|
|
|
} |
|
198
|
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
sub _touch { |
|
200
|
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
my $self = shift; |
|
202
|
|
|
|
|
|
|
$self->debug("touch(): for A::C::P::Proc::PID::File compat method"); |
|
203
|
|
|
|
|
|
|
$self->debug("write($$)"); |
|
204
|
|
|
|
|
|
|
open my $fh, ">", $self->path or die "can not open file ". $self->path . ": $!"; |
|
205
|
|
|
|
|
|
|
flock $fh, LOCK_EX | LOCK_NB or die "can not flock file " . $self->path . ": $!"; |
|
206
|
|
|
|
|
|
|
print $fh "$$\n"; |
|
207
|
|
|
|
|
|
|
close $fh or die "can not close file " . $self->path . ": $!"; |
|
208
|
|
|
|
|
|
|
} |
|
209
|
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
1; |
|
212
|
|
|
|
|
|
|
__END__ |