blib/lib/XAS/Lib/Modules/Locking.pm untested

filter: Statementstmt 0.0% 0 / 48 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 67 CC 3 SCAR 15.5
CC
3
Cov
0%
lock_directories
12.0
lock_directory
2.0
unlock_directory
2.0
CRAP 4.7
1 package XAS::Lib::Modules::Locking;
2  
3 our $VERSION = '0.02';
4  
5 use XAS::Factory;
6 use LockFile::Simple;
7  
8 use XAS::Class
9 debug => 0,
10 version => $VERSION,
11 base => 'XAS::Base',
12 accessors => 'lockmgr',
13 mutators => 'lockfile max delay hold',
14 filesystem => 'File',
15 vars => {
16 PARAMS => {
17 -lockfile => { optional => 1, default => 'locked' },
18 -max => { optional => 1, default => 20 },
19 -delay => { optional => 1, default => 1 },
20 -hold => { optional => 1, default => 900 },
21 }
22 }
23 ;
24  
25 # ----------------------------------------------------------------------
26 # Public Methods
27 # ----------------------------------------------------------------------
28  
29 sub lock_directory {
30 my $self = shift;
31  
32 my ($path) = $self->validate_params(\@_, [
33 { isa => 'Badger::Filesystem::Directory' },
34 ]);
35  
36 my $lock = $self->lock_file_name($path);
37  
38 return $self->lockmgr->lock($lock);
39  
40 }
41  
42 sub unlock_directory {
43 my $self = shift;
44  
45 my ($path) = $self->validate_parms(\@_, [
46 { isa => 'Badger::Filesystem::Directory' },
47 ]);
48  
49 my $lock = $self->lock_file_name($path);
50  
51 $self->lockmgr->unlock($lock);
52  
53 }
54  
55 sub lock_directories {
56 my $self = shift;
57  
58 my ($sdir, $ddir) = $self->validate_params(\@_, [
59 { isa => 'Badger::Filesystem::Directory' },
60 { isa => 'Badger::Filesystem::Directory' },
61 ]);
62  
63 my $stat = 0;
64  
65 if ($self->lock_directory($sdir)) {
66  
67 if ($self->lock_directory($ddir)){
68  
69 $stat = 1;
70  
71 } else {
72  
73 $self->unlock_directory($sdir);
74  
75 }
76  
77 }
78  
79 return $stat;
80  
81 }
82  
83 sub unlock_directories {
84 my $self = shift;
85  
86 my ($sdir, $ddir) = $self->validate_params(\@_, [
87 { isa => 'Badger::Filesystem::Directory' },
88 { isa => 'Badger::Filesystem::Directory' },
89 ]);
90  
91 $self->unlock_directory($sdir);
92 $self->unlock_directory($ddir);
93  
94 }
95  
96 sub lock_file_name {
97 my $self = shift;
98  
99 my ($directory) = $self->validate_params(\@_, [
100 { isa => 'Badger::Filesystem::Directory' },
101 ]);
102  
103 my $lock = File($directory->canonical, $self->lockfile);
104  
105 return $lock->path;
106  
107 }
108  
109 # ----------------------------------------------------------------------
110 # Private Methods
111 # ----------------------------------------------------------------------
112  
113 sub init {
114 my $class = shift;
115  
116 my $self = $class->SUPER::init(@_);
117  
118 $self->{lockmgr} = LockFile::Simple->make(
119 -stale => 1,
120 -nfs => 1,
121 -format => '%f.lck',
122 -max => $self->max,
123 -hold => $self->hold,
124 -delay => $self->delay,
125 -wfunc => sub { my $msg = shift; $self->log->warn($msg); },
126 -efunc => sub { my $msg = shift; $self->log->error($msg); }
127 );
128  
129 return $self;
130  
131 }
132  
133 1;
134  
135 __END__
136  
137 =head1 NAME
138  
139 XAS::Lib::Modules::Locking - A module to provide discretionary directory locking.
140  
141 =head1 SYNOPSIS
142  
143 package My::App;
144  
145 use XAS::Class
146 version => '0.01',
147 base => 'XAS::Lib::App',
148 ;
149  
150 sub main {
151  
152 if ($self->lockmgr->lock_directories('here', 'there')) {
153  
154 $self->lockmgr->unlock_directories('here', 'there);
155  
156 }
157  
158 }
159
160 1;
161  
162 =head1 DESCRIPTION
163  
164 This module provides discretionary directory locking. This is used to
165 coordinate access to shared resources. It is implemented as a singleton.
166 This module will also auto-load if "lockmgr" is used as a method invocation.
167  
168 =head1 METHODS
169  
170 =head2 new($parameters)
171  
172 This method initializes the module. It takes these parameters:
173  
174 =over 4
175  
176 =item B<-lockfile>
177  
178 The optional name of the lock file. It defaults to "locked".
179  
180 =item B<-max>
181  
182 The optional number of retries. This defaults to 20.
183  
184 =item B<-delay>
185  
186 The number of seconds to delay before retrying to acquire the lock. This
187 defaults to 1 second.
188  
189 =item B<-hold>
190  
191 The amount of time to hold a lock. After this time the lock is considered
192 "stale". This defaults to 900 seconds.
193  
194 =back
195  
196 =head2 lock_directory($directory)
197  
198 This method will lock a single directory. It takes these parameters:
199  
200 =over 4
201  
202 =item B<$directory>
203  
204 The directory to use when locking.
205  
206 =back
207  
208 =head2 unlock_directory($directory)
209  
210 This method will unlock a single directory. It takes these parameters:
211  
212 =over 4
213  
214 =item B<$directory>
215  
216 The directory to use when unlocking.
217  
218 =back
219  
220 =head2 lock_directories($source, $destination)
221  
222 This method will attempt to lock the source and destination directories.
223 It takes these parameters:
224  
225 =over 4
226  
227 =item B<$source>
228  
229 The source directory.
230  
231 =item B<$destination>
232  
233 The destination directory.
234  
235 =back
236  
237 =head2 unlock_directories($source, $destination)
238  
239 This method will unlock the source and destination directories. It takes
240 these parameters:
241  
242 =over 4
243  
244 =item B<$source>
245  
246 The source directory.
247  
248 =item B<$destination>
249  
250 The destination directory.
251  
252 =back
253  
254 =head2 lock_file_name($directory)
255  
256 This method returns the locks file name. It can be overridden if needed.
257 The default name is 'locked'. It takes the following parameters:
258  
259 =over 4
260  
261 =item B<$directory>
262  
263 The directory the lock file resides in.
264  
265 =back
266  
267 =head1 SEE ALSO
268  
269 =over 4
270  
271 =item L<XAS|XAS>
272  
273 =back
274  
275 =head1 AUTHOR
276  
277 Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt>
278  
279 =head1 COPYRIGHT AND LICENSE
280  
281 Copyright (C) 2014 Kevin L. Esteb
282  
283 This library is free software; you can redistribute it and/or modify
284 it under the same terms as Perl itself, either Perl version 5.8.8 or,
285 at your option, any later version of Perl 5 you may have available.
286  
287 See L<http://dev.perl.org/licenses/> for more information.
288  
289 =cut