blib/lib/XAS/Lib/Modules/Spool.pm

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