| 1 | ## -*- Mode: CPerl -*- | ||
| 2 | ## | ||
| 3 | ## File: DTA::CAB::Queue::File.pm | ||
| 4 | ## Author: Bryan Jurish <moocow@cpan.org> | ||
| 5 | ## Description: file-based queue | ||
| 6 | |||
| 7 | package DTA::CAB::Queue::File; | ||
| 8 | use DTA::CAB::Utils ':temp'; | ||
| 9 | use File::Queue; | ||
| 10 | use Carp; | ||
| 11 | use strict; | ||
| 12 | |||
| 13 | ##============================================================================== | ||
| 14 | ## Globals | ||
| 15 | ##============================================================================== | ||
| 16 | |||
| 17 | our @ISA = qw(DTA::CAB::Logger File::Queue); | ||
| 18 | |||
| 19 | ##============================================================================== | ||
| 20 | ## Constructors etc. | ||
| 21 | ##============================================================================== | ||
| 22 | |||
| 23 | ## $q = DTA::CAB::Queue::File->new(%args) | ||
| 24 | ## + %$q, %args: | ||
| 25 | ## ( | ||
| 26 | ## file => $filename, ##-- basename of queue file (will have .dat,.idx suffixes); default=tmpfsfile('qXXXXXXX') | ||
| 27 | ## mode => $mode, ##-- creation mode (default=0660) | ||
| 28 | ## seperator => $str, ##-- item separator string (default=$/) [typo in name is (sic): bummer] | ||
| 29 | ## ) | ||
| 30 | sub new { | ||
| 31 | my ($that,%args) = @_; | ||
| 32 | if (exists($args{separator})) { | ||
| 33 | ##-- annoying typo in File::Queue | ||
| 34 | $args{seperator} = $args{separator}; | ||
| 35 | CORE::delete($args{separator}); | ||
| 36 | } | ||
| 37 | $args{file} = tmpfsfile('qXXXXXXX') if (!defined($args{file})); | ||
| 38 | my $q = $that->SUPER::new(seperator=>$/,mode=>0600,%args); | ||
| 39 | @$q{keys %args} = values %args; ##-- save args | ||
| 40 | return $q; | ||
| 41 | } | ||
| 42 | |||
| 43 | ## $q = $q->reopen() | ||
| 44 | ## + re-open the queue (e.g. in a sub-process) | ||
| 45 | ## + hack just calls new() | ||
| 46 | BEGIN { | ||
| 47 | *refresh = \&reopen; | ||
| 48 | } | ||
| 49 | sub reopen { | ||
| 50 | my $q = shift; | ||
| 51 | %$q = %{ref($q)->new(file=>$q->{file},mode=>$q->{mode},seperator=>$q->{seperator})}; | ||
| 52 | return $q; | ||
| 53 | } | ||
| 54 | |||
| 55 | ## undef = $q->enq($item) | ||
| 56 | ## + enqueue a simple item | ||
| 57 | |||
| 58 | ## $item_or_undef = $q->deq() | ||
| 59 | ## + de-queue a single item; undef means end-of-queue | ||
| 60 | |||
| 61 | ## @items = $q->peek($count) | ||
| 62 | ## + peek at the top of the queue | ||
| 63 | |||
| 64 | ## undef = $q->reset() | ||
| 65 | ## + clear the queue | ||
| 66 | sub clear { $_[0]->reset(); } | ||
| 67 | |||
| 68 | ## undef = $q->close() | ||
| 69 | ## + close the queue filehandles | ||
| 70 | |||
| 71 | ## undef = $q->delete() | ||
| 72 | ## + delete queue file(s) | ||
| 73 | sub unlink { $_[0]->delete(); } | ||
| 74 | |||
| 75 | 1; | ||
| 76 | |||
| 77 | ##============================================================================== | ||
| 78 | ## Package DTA::CAB::Queue::File::Locked | ||
| 79 | package DTA::CAB::Queue::File::Locked; | ||
| 80 | use Fcntl qw(:flock); | ||
| 81 | our @ISA = qw(DTA::CAB::Queue::File); | ||
| 82 | |||
| 83 | sub _locked { | ||
| 84 | my $subname = shift; | ||
| 85 | my $supersub = DTA::CAB::Queue::File->can($subname); | ||
| 86 | die(__PACKAGE__, "::_locked(): no superclass subroutine for '$subname'") if (!$supersub); | ||
| 87 | return sub { | ||
| 88 | my (@rc); | ||
| 89 | flock($_[0]{queue},LOCK_EX) if ($_[0]{queue}); | ||
| 90 | if (wantarray) { | ||
| 91 | @rc = $supersub->(@_); | ||
| 92 | } else { | ||
| 93 | $rc[0] = $supersub->(@_); | ||
| 94 | } | ||
| 95 | flock($_[0]{queue},LOCK_UN) if ($_[0]{queue}); | ||
| 96 | return wantarray ? @rc : $rc[0]; | ||
| 97 | }; | ||
| 98 | } | ||
| 99 | |||
| 100 | *enq = _locked('enq'); | ||
| 101 | *deq = _locked('deq'); | ||
| 102 | *peek = _locked('peek'); | ||
| 103 | *reset = _locked('reset'); | ||
| 104 | #*close = _locked('close'); | ||
| 105 | |||
| 106 | 1; ##-- be happy | ||
| 107 | |||
| 108 | __END__ |