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