| 1 | package XAS::Lib::Modules::Spool; | ||
| 2 | |||
| 3 | our $VERSION = '0.03'; | ||
| 4 | |||
| 5 | use Try::Tiny; | ||
| 6 | use XAS::Factory; | ||
| 7 | |||
| 8 | use XAS::Class | ||
| 9 | debug => 0, | ||
| 10 | version => $VERSION, | ||
| 11 | base => 'XAS::Base', | ||
| 12 | mixin => 'XAS::Lib::Mixins::Handlers', | ||
| 13 | accessors => 'lockmgr', | ||
| 14 | utils => 'dotid', | ||
| 15 | vars => { | ||
| 16 | PARAMS => { | ||
| 17 | -directory => { isa => 'Badger::Filesystem::Directory' }, | ||
| 18 | -mask => { optional => 1, default => 0664 }, | ||
| 19 | -extension => { optional => 1, default => '.pkt' }, | ||
| 20 | -seqfile => { optional => 1, default => '.SEQ' }, | ||
| 21 | -lockfile => { optional => 1, default => 'spool' }, | ||
| 22 | } | ||
| 23 | } | ||
| 24 | ; | ||
| 25 | |||
| 26 | # ------------------------------------------------------------------------ | ||
| 27 | # Public Methods | ||
| 28 | # ------------------------------------------------------------------------ | ||
| 29 | |||
| 30 | sub read { | ||
| 31 | my $self = shift; | ||
| 32 | |||
| 33 | my ($filename) = $self->validate_params(\@_, [ | ||
| 34 | { isa => 'Badger::Filesystem::File' }, | ||
| 35 | ]); | ||
| 36 | |||
| 37 | my $packet; | ||
| 38 | |||
| 39 | if ($self->lockmgr->lock_directory($self->directory)) { | ||
| 40 | |||
| 41 | $packet = $self->read_packet($filename); | ||
| 42 | $self->lockmgr->unlock_directory($self->directory); | ||
| 43 | |||
| 44 | } else { | ||
| 45 | |||
| 46 | $self->throw_msg( | ||
| 47 | dotid($self->class) . '.read.lock_error', | ||
| 48 | 'lock_error', | ||
| 49 | $self->directory->path | ||
| 50 | ); | ||
| 51 | |||
| 52 | } | ||
| 53 | |||
| 54 | return $packet; | ||
| 55 | |||
| 56 | } | ||
| 57 | |||
| 58 | sub write { | ||
| 59 | my $self = shift; | ||
| 60 | |||
| 61 | my ($packet) = $self->validate_params(\@_, [ 1 ]); | ||
| 62 | |||
| 63 | my $seqnum; | ||
| 64 | |||
| 65 | if ($self->lockmgr->lock_directory($self->directory)) { | ||
| 66 | |||
| 67 | $seqnum = $self->sequence(); | ||
| 68 | |||
| 69 | $self->write_packet($packet, $seqnum); | ||
| 70 | $self->lockmgr->unlock_directory($self->directory); | ||
| 71 | |||
| 72 | } else { | ||
| 73 | |||
| 74 | $self->throw_msg( | ||
| 75 | dotid($self->class) . '.write.lock_error', | ||
| 76 | 'lock_error', | ||
| 77 | $self->directory->path | ||
| 78 | ); | ||
| 79 | |||
| 80 | } | ||
| 81 | |||
| 82 | return 1; | ||
| 83 | |||
| 84 | } | ||
| 85 | |||
| 86 | sub scan { | ||
| 87 | my $self = shift; | ||
| 88 | |||
| 89 | my @files; | ||
| 90 | my $pattern = qr/$self->extension/i; | ||
| 91 | |||
| 92 | if ($self->lockmgr->lock_directory($self->directory)) { | ||
| 93 | |||
| 94 | @files = sort(grep( $_->path =~ /$pattern/, $self->directory->files() )); | ||
| 95 | $self->lockmgr->unlock_directory($self->directory); | ||
| 96 | |||
| 97 | } else { | ||
| 98 | |||
| 99 | $self->throw_msg( | ||
| 100 | dotid($self->class) . '.scan.lock_error', | ||
| 101 | 'lock_error', | ||
| 102 | $self->directory->path | ||
| 103 | ); | ||
| 104 | |||
| 105 | } | ||
| 106 | |||
| 107 | return @files; | ||
| 108 | |||
| 109 | } | ||
| 110 | |||
| 111 | sub delete { | ||
| 112 | my $self = shift; | ||
| 113 | |||
| 114 | my ($file) = $self->validate_params(\@_, [ | ||
| 115 | { isa => 'Badger::Filesystem::File' }, | ||
| 116 | ]); | ||
| 117 | |||
| 118 | if ($self->lockmgr->lock_directory($self->directory)) { | ||
| 119 | |||
| 120 | try { | ||
| 121 | |||
| 122 | $file->delete; | ||
| 123 | |||
| 124 | } catch { | ||
| 125 | |||
| 126 | my $ex = $_; | ||
| 127 | |||
| 128 | $self->exception_handler($ex); | ||
| 129 | |||
| 130 | }; | ||
| 131 | |||
| 132 | $self->lockmgr->unlock_directory($self->directory); | ||
| 133 | |||
| 134 | } else { | ||
| 135 | |||
| 136 | $self->throw_msg( | ||
| 137 | dotid($self->class) . '.delete.lock_error', | ||
| 138 | 'lock_error', | ||
| 139 | $self->directory->path | ||
| 140 | ); | ||
| 141 | |||
| 142 | } | ||
| 143 | |||
| 144 | } | ||
| 145 | |||
| 146 | sub count { | ||
| 147 | my $self = shift; | ||
| 148 | |||
| 149 | my @files; | ||
| 150 | my $count; | ||
| 151 | my $pattern = qr/$self->extension/i; | ||
| 152 | |||
| 153 | if ($self->lockmgr->lock_directory($self->directory)) { | ||
| 154 | |||
| 155 | @files = grep( $_->path =~ /$pattern/, $self->directory->files() ); | ||
| 156 | $count = scalar(@files); | ||
| 157 | |||
| 158 | $self->lockmgr->unlock_directory($self->directory); | ||
| 159 | |||
| 160 | } else { | ||
| 161 | |||
| 162 | $self->throw_msg( | ||
| 163 | dotid($self->class) . '.count.lock_error', | ||
| 164 | 'lock_error', | ||
| 165 | $self->directory->path | ||
| 166 | ); | ||
| 167 | |||
| 168 | } | ||
| 169 | |||
| 170 | return $count; | ||
| 171 | |||
| 172 | } | ||
| 173 | |||
| 174 | sub get { | ||
| 175 | my $self = shift; | ||
| 176 | |||
| 177 | my @files; | ||
| 178 | my $filename; | ||
| 179 | my $pattern = qr/$self->extension/i; | ||
| 180 | |||
| 181 | if ($self->lockmgr->lock_directory($self->directory)) { | ||
| 182 | |||
| 183 | @files = sort(grep( $_->path =~ /$pattern/, $self->directory->files() )); | ||
| 184 | $filename = $files[0]; | ||
| 185 | |||
| 186 | $self->lockmgr->unlock_directory($self->directory); | ||
| 187 | |||
| 188 | } else { | ||
| 189 | |||
| 190 | $self->throw_msg( | ||
| 191 | dotid($self->class) . '.get.lock_error', | ||
| 192 | 'lock_error', | ||
| 193 | $self->directory->path | ||
| 194 | ); | ||
| 195 | |||
| 196 | } | ||
| 197 | |||
| 198 | return $filename; | ||
| 199 | |||
| 200 | } | ||
| 201 | |||
| 202 | # ------------------------------------------------------------------------ | ||
| 203 | # Private Methods | ||
| 204 | # ------------------------------------------------------------------------ | ||
| 205 | |||
| 206 | sub init { | ||
| 207 | my $class = shift; | ||
| 208 | |||
| 209 | my $self = $class->SUPER::init(@_); | ||
| 210 | |||
| 211 | $self->{lockmgr} = XAS::Factory->module('lockmgr', { | ||
| 212 | -lockfile => $self->lockfile | ||
| 213 | }); | ||
| 214 | |||
| 215 | return $self; | ||
| 216 | |||
| 217 | } | ||
| 218 | |||
| 219 | sub sequence { | ||
| 220 | my $self = shift; | ||
| 221 | |||
| 222 | my $fh; | ||
| 223 | my $cnt; | ||
| 224 | my $seqnum; | ||
| 225 | my $mask = $self->mask + 0; | ||
| 226 | my $file = File($self->directory, $self->seqfile); | ||
| 227 | |||
| 228 | try { | ||
| 229 | |||
| 230 | if ($file->exists) { | ||
| 231 | |||
| 232 | $fh = $file->open("r+"); | ||
| 233 | $seqnum = $fh->getline; | ||
| 234 | $seqnum++; | ||
| 235 | $fh->seek(0, 0); | ||
| 236 | $fh->print($seqnum); | ||
| 237 | $fh->close; | ||
| 238 | |||
| 239 | } else { | ||
| 240 | |||
| 241 | $fh = $file->open("w"); | ||
| 242 | $fh->print("1"); | ||
| 243 | $fh->close; | ||
| 244 | |||
| 245 | $cnt = chmod($mask, $file); | ||
| 246 | $self->throw_msg( | ||
| 247 | dotid($self->class) . '.sequence.invperms', | ||
| 248 | 'invperms', | ||
| 249 | $file | ||
| 250 | ) if ($cnt < 1); | ||
| 251 | |||
| 252 | $seqnum = 1; | ||
| 253 | |||
| 254 | } | ||
| 255 | |||
| 256 | } catch { | ||
| 257 | |||
| 258 | my $ex = $_; | ||
| 259 | |||
| 260 | $self->throw_msg( | ||
| 261 | dotid($self->class) . '.sequence', | ||
| 262 | 'sequence', | ||
| 263 | $file | ||
| 264 | ); | ||
| 265 | |||
| 266 | }; | ||
| 267 | |||
| 268 | return $seqnum; | ||
| 269 | |||
| 270 | } | ||
| 271 | |||
| 272 | sub write_packet { | ||
| 273 | my $self = shift; | ||
| 274 | |||
| 275 | my ($packet, $seqnum) = $self->validate_params(\@_, [1,1]); | ||
| 276 | |||
| 277 | my $fh; | ||
| 278 | my $cnt; | ||
| 279 | my $mask = $self->mask + 0; | ||
| 280 | my $file = File($self->directory, $seqnum . $self->extension); | ||
| 281 | |||
| 282 | try { | ||
| 283 | |||
| 284 | $fh = $file->open("w"); | ||
| 285 | $fh->print($packet); | ||
| 286 | $fh->close; | ||
| 287 | $cnt = chmod($mask, $file->path) or | ||
| 288 | $self->throw_msg( | ||
| 289 | dotid($self->class) . '.write_packet.invperms', | ||
| 290 | 'invperms', | ||
| 291 | $file->path | ||
| 292 | ); | ||
| 293 | |||
| 294 | } catch { | ||
| 295 | |||
| 296 | my $ex = $_; | ||
| 297 | |||
| 298 | $self->throw_msg( | ||
| 299 | dotid($self->class) . '.write_packet', | ||
| 300 | 'write_packet', | ||
| 301 | $file->path | ||
| 302 | ); | ||
| 303 | |||
| 304 | }; | ||
| 305 | |||
| 306 | } | ||
| 307 | |||
| 308 | sub read_packet { | ||
| 309 | my $self = shift; | ||
| 310 | |||
| 311 | my ($file) = $self->validate_params(\@_, [ | ||
| 312 | { isa => 'Badger::Filesystem::File' } | ||
| 313 | ]); | ||
| 314 | |||
| 315 | my $fh; | ||
| 316 | my $packet; | ||
| 317 | |||
| 318 | try { | ||
| 319 | |||
| 320 | $fh = $file->open("r"); | ||
| 321 | $packet = $fh->getline; | ||
| 322 | $fh->close; | ||
| 323 | |||
| 324 | } catch { | ||
| 325 | |||
| 326 | my $ex = $_; | ||
| 327 | |||
| 328 | $self->throw_msg( | ||
| 329 | dotid($self->class) . '.read_packet', | ||
| 330 | 'read_packet', | ||
| 331 | $file->path | ||
| 332 | ); | ||
| 333 | |||
| 334 | }; | ||
| 335 | |||
| 336 | return $packet; | ||
| 337 | |||
| 338 | } | ||
| 339 | |||
| 340 | 1; | ||
| 341 | |||
| 342 | __END__ | ||
| 343 | |||
| 344 | =head1 NAME | ||
| 345 | |||
| 346 | XAS::Lib::Modules::Spool - A Perl extension for the XAS environment | ||
| 347 | |||
| 348 | =head1 SYNOPSIS | ||
| 349 | |||
| 350 | use XAS::Factory; | ||
| 351 | |||
| 352 | my $spl = XAS::Factory->module( | ||
| 353 | spool => { | ||
| 354 | -directory => 'spool' | ||
| 355 | } | ||
| 356 | ); | ||
| 357 | |||
| 358 | $spl->write('this is some data'); | ||
| 359 | $spl->write("This is some other data"); | ||
| 360 | |||
| 361 | my @files = $spl->scan(); | ||
| 362 | |||
| 363 | foreach my $file (@files) { | ||
| 364 | |||
| 365 | my $packet = $spl->read($file); | ||
| 366 | print $packet; | ||
| 367 | $spl->delete($file); | ||
| 368 | |||
| 369 | } | ||
| 370 | |||
| 371 | =head1 DESCRIPTION | ||
| 372 | |||
| 373 | This module provides the basic handling of spool files. This module | ||
| 374 | provides basic read, write, scan and delete functionality for those files. | ||
| 375 | |||
| 376 | This functionality is designed to be overridden with more specific methods | ||
| 377 | for each type of spool file required. | ||
| 378 | |||
| 379 | Individual spool files are stored in sub directories. Since multiple | ||
| 380 | processes may be accessing those directories, lock files are being used to | ||
| 381 | control access. This is an important requirement to prevent possible race | ||
| 382 | conditions between those processes. | ||
| 383 | |||
| 384 | A sequence number is stored in the .SEQ file within each sub directory. Each | ||
| 385 | spool file will use the ever increasing sequence number as the file name with | ||
| 386 | a .pkt extension. To reset the sequence number, just delete the .SEQ file. A | ||
| 387 | new file will automatically be created. | ||
| 388 | |||
| 389 | =head1 METHODS | ||
| 390 | |||
| 391 | =head2 new | ||
| 392 | |||
| 393 | This will initialize the base object. It takes the following parameters: | ||
| 394 | |||
| 395 | =over 4 | ||
| 396 | |||
| 397 | =item B<-directory> | ||
| 398 | |||
| 399 | This is the directory to use for spool files. | ||
| 400 | |||
| 401 | =item B<-lockfile> | ||
| 402 | |||
| 403 | The name of the lock file to use. Defaults to 'spool'. | ||
| 404 | |||
| 405 | =item B<-extension> | ||
| 406 | |||
| 407 | The extension to use on the spool file. Defaults to '.pkt'. | ||
| 408 | |||
| 409 | =item B<-seqfile> | ||
| 410 | |||
| 411 | The name of the sequence file to use. Defaults to '.SEQ'. | ||
| 412 | |||
| 413 | =item B<-mask> | ||
| 414 | |||
| 415 | The file permissions for any created file. Default 0664. | ||
| 416 | |||
| 417 | =back | ||
| 418 | |||
| 419 | =head2 write($packet) | ||
| 420 | |||
| 421 | This will write a new spool file using the supplied "packet". Each | ||
| 422 | evocation of write() will create a new spool file. This method should be | ||
| 423 | overridden by the more specific needs of sub classes. | ||
| 424 | |||
| 425 | =over 4 | ||
| 426 | |||
| 427 | =item B<$packet> | ||
| 428 | |||
| 429 | The data that will be written to the spool file. | ||
| 430 | |||
| 431 | =back | ||
| 432 | |||
| 433 | =head2 read($filename) | ||
| 434 | |||
| 435 | This will read the contents of spool file and return a data structure. This | ||
| 436 | method should be overridden by the more specific needs of sub classes. | ||
| 437 | |||
| 438 | Example | ||
| 439 | |||
| 440 | $packet = $spl->read($file); | ||
| 441 | |||
| 442 | =head2 scan | ||
| 443 | |||
| 444 | This will scan the spool directory looking for items to process. It returns | ||
| 445 | and array of files to process. | ||
| 446 | |||
| 447 | =head2 delete($filename) | ||
| 448 | |||
| 449 | This method will delete the file from the spool directory. | ||
| 450 | |||
| 451 | =head2 count | ||
| 452 | |||
| 453 | This method will return a count of the items in the spool directory. | ||
| 454 | |||
| 455 | =head2 get | ||
| 456 | |||
| 457 | This method will retrieve a file name from the spool directory. | ||
| 458 | |||
| 459 | =head1 ACCESORS | ||
| 460 | |||
| 461 | =head2 extension | ||
| 462 | |||
| 463 | This method will get the current file extension. | ||
| 464 | |||
| 465 | =head2 lockfile | ||
| 466 | |||
| 467 | This method will get the current lock file name. | ||
| 468 | |||
| 469 | =head2 segfile | ||
| 470 | |||
| 471 | This method will get the current sequence file name. | ||
| 472 | |||
| 473 | =head1 SEE ALSO | ||
| 474 | |||
| 475 | =over 4 | ||
| 476 | |||
| 477 | =item L<XAS|XAS> | ||
| 478 | |||
| 479 | =back | ||
| 480 | |||
| 481 | =head1 AUTHOR | ||
| 482 | |||
| 483 | Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt> | ||
| 484 | |||
| 485 | =head1 COPYRIGHT AND LICENSE | ||
| 486 | |||
| 487 | Copyright (C) 2014 Kevin L. Esteb | ||
| 488 | |||
| 489 | This library is free software; you can redistribute it and/or modify | ||
| 490 | it under the same terms as Perl itself, either Perl version 5.8.8 or, | ||
| 491 | at your option, any later version of Perl 5 you may have available. | ||
| 492 | |||
| 493 | See L<http://dev.perl.org/licenses/> for more information. | ||
| 494 | |||
| 495 | =cut |