blib/lib/XAS/Lib/Pidfile.pm

filter: Statementstmt 22.9% 11 / 48 Branchbran 6.2% 1 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 37.5% 3 / 8 Podpod 100% 3 / 3 totaltotal 24.0% 18 / 75 CC 9 SCAR 20.8
CC
9
Cov
19%
write
12.0
remove
12.0
_get_pid
12.0
CRAP 8.0
« blib/lib/XAS/Lib/POE/Session.pm ↑ Summary blib/lib/XAS/Lib/XML.pm »
1 package XAS::Lib::Pidfile;
2  
3 1 our $VERSION = '0.01';
4  
5 1 my $mixin;
6  
7 BEGIN {
8 1 ▶ $mixin = 'XAS::Lib::Pidfile::Unix';
Subroutine BEGIN: called CC=2 SCAR=7.2
9 1 ▶ $mixin = 'XAS::Lib::Pidfile::Win32' if ($^O eq 'MSWin32');
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "MSWin32" 0 1 1
10 }
11  
12 1 ▶use XAS::Lib::Modules::Locking;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 use XAS::Class
15 1 debug => 0,
16 version => $VERSION,
17 base => 'XAS::Base',
18 mixin => $mixin,
19 utils => 'trim dotid',
20 accessors => 'lockmgr',
21 filesystem => 'Dir',
22 vars => {
23 PARAMS => {
24 pid => 1,
25 file => { optional => 1, default => undef, isa => 'Badger::Filesystem::File' },
26 }
27 }
28 1 ▶;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 #use Data::Dumper;
31  
32 # ----------------------------------------------------------------------
33 # Public Methods
34 # ----------------------------------------------------------------------
35  
36 sub write {
37 0 ▶ my $self = shift;
Subroutine write: not called CC=3 SCAR=24.8
Pod documented
38  
39 0 my $stat = 0;
40 0 my $lock = Dir($self->file->volume, $self->file->directory);
41 my $output = sub {
42  
43 0 ▶ my $fh = $self->file->open('w');
Subroutine __ANON__: not called CC=1 SCAR=6.9
44 0 $fh->printf("%s\n", $self->pid);
45 0 $fh->close;
46  
47 0 };
48  
49 0 ▶ if ($self->lockmgr->lock_directory($lock)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->lockmgr->lock_directory($lock)) 0 0 0
50  
51 0 ▶ if ($self->file->exists) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->file->exists) { } 0 0 0
52  
53 0 $output->();
54  
55 } else {
56  
57 0 $self->file->create();
58 0 $output->();
59  
60 }
61  
62 0 $stat = 1;
63 0 $self->lockmgr->unlock_directory($lock);
64  
65 }
66  
67 0 return $stat;
68  
69 }
70  
71 sub remove {
72 0 ▶ my $self = shift;
Subroutine remove: not called CC=3 SCAR=24.8
Pod documented
73  
74 0 my $lock = Dir($self->file->volume, $self->file->directory);
75  
76 0 ▶ if ($self->lockmgr->lock_directory($lock)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->lockmgr->lock_directory($lock)) 0 0 0
77  
78 0 ▶ $self->file->delete() if ($self->file->exists);
Branch: 1 branch
BranchTrueFalseTotal
if $self->file->exists 0 0 0
79 0 $self->lockmgr->unlock_directory($lock);
80  
81 }
82  
83 }
84  
85  
86 # ----------------------------------------------------------------------
87 # Private Methods
88 # ----------------------------------------------------------------------
89  
90 sub _get_pid {
91 0 ▶ my $self = shift;
Subroutine _get_pid: not called CC=3 SCAR=24.8
92  
93 0 my $pid = undef;
94 0 my $lock = Dir($self->file->volume, $self->file->directory);
95  
96 0 ▶ if ($self->lockmgr->lock_directory($lock)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->lockmgr->lock_directory($lock)) 0 0 0
97  
98 0 ▶ if ($self->file->exists) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->file->exists) 0 0 0
99  
100 0 my $fh = $self->file->open();
101 0 $pid = $fh->getline();
102 0 $pid = trim($pid);
103 0 $fh->close();
104  
105 }
106  
107 0 $self->lockmgr->unlock_directory($lock);
108  
109 }
110  
111 0 return $pid
112  
113 }
114  
115 sub init {
116 0 ▶ my $class = shift;
Subroutine init: not called CC=2 SCAR=17.9
Pod documented
117  
118 0 my $self = $class->SUPER::init(@_);
119  
120 0 $self->{lockmgr} = XAS::Lib::Modules::Locking->new();
121  
122 0 ▶ unless (defined($self->{file})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"file"}) 0 0 0
123  
124 0 $self->{file} = $self->env->pidfile;
125  
126 }
127  
128 0 return $self;
129  
130 }
131  
132 1 1;
133  
134 __END__
135  
136 =head1 NAME
137  
138 XAS::Lib::PidFile - A class to manage pid files within XAS
139  
140 =head1 SYNOPSIS
141  
142 use XAS::Lib::PidFile;
143  
144 my $pid = XAS::Lib::PidFile->new(
145 -pid => $$,
146 -file => File('/', 'var', 'run', 'xas', 'process.pid')
147 );
148  
149 if ($pid->is_running) {
150  
151 printf("already running\n");
152 exit 2;
153  
154 }
155  
156 $pid->write();
157
158 ...
159
160 $pid->remove();
161
162 =head1 DESCRIPTION
163  
164 This class will manage pid files for XAS. It loads mixins for individual
165 platforms to help with determining if a process is already running. It
166 uses discretionary directory locking to control access to the pid files.
167  
168 =head1 METHODS
169  
170 =head2 new
171  
172 This method initialize the module and takes this optional parameters.
173  
174 =over 4
175  
176 =item B<-file>
177  
178 Specifiy a pid file to use. This defaults to the pid file defined by
179 L<XAS::Lib::Modules::Environment> for the current procedure.
180  
181 =item B<-pid>
182  
183 Define a pid number. This must be supplied
184  
185 =back
186  
187 =head2 is_running
188  
189 This method is loaded thru a mixin. It will attempt to load a currently
190 existing pid file and check to see if that pid is active and if that
191 running process is the same as the current procedure.
192  
193 If it is, then it will return true. If not then it will return false.
194  
195 =head2 write
196  
197 Write the current pid to the pid file.
198  
199 =head2 remove
200  
201 Remove the current pid file.
202  
203 =head1 SEE ALSO
204  
205 =over 4
206  
207 =item L<XAS|XAS>
208  
209 =back
210  
211 =head1 AUTHOR
212  
213 Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt>
214  
215 =head1 COPYRIGHT AND LICENSE
216  
217 Copyright (C) 2014 Kevin L. Esteb
218  
219 TThis is free software; you can redistribute it and/or modify it under
220 the terms of the Artistic License 2.0. For details, see the full text
221 of the license at http://www.perlfoundation.org/artistic_license_2_0.
222  
223 =cut
« blib/lib/XAS/Lib/POE/Session.pm ↑ Summary blib/lib/XAS/Lib/XML.pm »