File Coverage

blib/lib/XAS/Lib/Pidfile.pm
Criterion Covered Total %
statement 11 53 20.7
branch 1 18 5.5
condition n/a
subroutine 4 9 44.4
pod 3 3 100.0
total 19 83 22.8


line stmt bran cond sub pod time code
1             package XAS::Lib::Pidfile;
2              
3             our $VERSION = '0.01';
4             my $mixin;
5              
6             BEGIN {
7 1     1   3 $mixin = 'XAS::Lib::Pidfile::Unix';
8 1 50       24 $mixin = 'XAS::Lib::Pidfile::Win32' if ($^O eq 'MSWin32');
9             }
10              
11 1     1   4 use XAS::Factory;
  1         1  
  1         8  
12 1     1   71 use File::Basename;
  1         1  
  1         85  
13              
14             use XAS::Class
15 1         19 debug => 0,
16             version => $VERSION,
17             base => 'XAS::Base',
18             mixin => $mixin,
19             utils => 'trim dotid',
20             accessors => 'lockmgr lock',
21             filesystem => 'Dir',
22             vars => {
23             PARAMS => {
24             pid => 1,
25             file => { optional => 1, default => undef, isa => 'Badger::Filesystem::File' },
26             }
27             }
28 1     1   4 ;
  1         1  
29              
30             #use Data::Dumper;
31              
32             # ----------------------------------------------------------------------
33             # Public Methods
34             # ----------------------------------------------------------------------
35              
36             sub write {
37 0     0 1   my $self = shift;
38              
39 0           my $stat = 0;
40 0           my $lock = $self->lock;
41              
42             my $output = sub {
43              
44 0     0     my $fh = $self->file->open('w');
45 0           $fh->printf("%s\n", $self->pid);
46 0           $fh->close;
47              
48 0           };
49              
50 0 0         if ($self->lockmgr->lock($lock)) {
51              
52 0 0         if ($self->file->exists) {
53              
54 0           $output->();
55              
56             } else {
57              
58 0           $self->file->create();
59 0           $output->();
60              
61             }
62              
63 0           $stat = 1;
64 0           $self->lockmgr->unlock($lock);
65              
66             }
67              
68 0           return $stat;
69              
70             }
71              
72             sub remove {
73 0     0 1   my $self = shift;
74              
75 0           my $lock = $self->lock;
76 0           my $pid = $self->_get_pid;
77              
78 0 0         if ($pid == $$) {
79              
80 0 0         if ($self->lockmgr->lock($lock)) {
81              
82 0 0         $self->file->delete() if ($self->file->exists);
83 0           $self->lockmgr->unlock($lock);
84              
85             }
86              
87             }
88              
89             }
90              
91             # ----------------------------------------------------------------------
92             # Private Methods
93             # ----------------------------------------------------------------------
94              
95             sub _get_pid {
96 0     0     my $self = shift;
97              
98 0           my $pid = undef;
99 0           my $lock = $self->lock;
100              
101 0 0         if ($self->lockmgr->lock($lock)) {
102              
103 0 0         if ($self->file->exists) {
104              
105 0           my $fh = $self->file->open();
106 0           $pid = $fh->getline();
107 0           $pid = trim($pid);
108 0           $fh->close();
109              
110             }
111              
112 0           $self->lockmgr->unlock($lock);
113              
114             }
115              
116 0           return $pid
117              
118             }
119              
120             sub init {
121 0     0 1   my $class = shift;
122              
123 0           my $self = $class->SUPER::init(@_);
124              
125 0 0         unless (defined($self->{'file'})) {
126              
127 0           $self->{'file'} = $self->env->pid_file;
128              
129             }
130              
131 0           my $basename = fileparse($self->env->script, qr/\.[^.]*/);
132              
133 0           $self->{'lock'} = Dir($self->file->volume, $self->file->directory, $basename)->path;
134 0           $self->{'lockmgr'} = XAS::Factory->module('lockmgr');
135              
136 0           $self->lockmgr->add(
137             -key => $self->lock,
138             -driver => 'Filesystem',
139             );
140              
141 0           return $self;
142              
143             }
144              
145             1;
146              
147             __END__