| 1 | =head1 NAME | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | Mail::Action::Role::Purge - purge expired objects from Mail::Action::Storage | ||||||||||||||||||||||||||||
| 4 | collections. | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | use Mail::TempAddress::Addresses; | ||||||||||||||||||||||||||||
| 9 | use Mail::Action::Role::Purge; | ||||||||||||||||||||||||||||
| 10 | use Class::Roles | ||||||||||||||||||||||||||||
| 11 | apply => { | ||||||||||||||||||||||||||||
| 12 | role => 'Purge', | ||||||||||||||||||||||||||||
| 13 | to => 'Mail::TempAddress::Addresses', | ||||||||||||||||||||||||||||
| 14 | }; | ||||||||||||||||||||||||||||
| 15 | my $addrs = Mail::TempAddress::Addresses->new( '/foo' ); | ||||||||||||||||||||||||||||
| 16 | for my $addr( $addrs->object_names ) { | ||||||||||||||||||||||||||||
| 17 | print "the address for ", $addr->owner, " expires at ", | ||||||||||||||||||||||||||||
| 18 | scalar localtime($addr->expires), "\n"; | ||||||||||||||||||||||||||||
| 19 | if( $addr->expires < time() ) { | ||||||||||||||||||||||||||||
| 20 | $addrs->delete_from_storage($addr); | ||||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | Mail::Action::Role::Purge provides a role to allow subclasses of | ||||||||||||||||||||||||||||
| 27 | Mail::Action::Storage to retrieve the names of all object in the collection | ||||||||||||||||||||||||||||
| 28 | and remove the file on disk underlying a named object. | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =cut | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | package Mail::Action::Role::Purge; | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | 7 | ▶ | use Carp 'croak'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 7 | ▶ | use Mail::Action::Storage; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | use Class::Roles | ||||||||||||||||||||||||||||
| 39 | 7 | ▶ | multi => { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | 'Purge' => [ qw| | ||||||||||||||||||||||||||||
| 41 | object_names | ||||||||||||||||||||||||||||
| 42 | num_objects | ||||||||||||||||||||||||||||
| 43 | delete_from_storage | ||||||||||||||||||||||||||||
| 44 | purge | ||||||||||||||||||||||||||||
| 45 | |], | ||||||||||||||||||||||||||||
| 46 | 7 | }; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 7 | ▶ | use vars '$VERSION'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 7 | $VERSION = '0.11'; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 7 | ▶ | use File::Basename; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 53 | 7 | ▶ | use Fcntl ':flock'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | sub object_names | ||||||||||||||||||||||||||||
| 56 | 30 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
object_names: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 30 | my $self = shift; | |||||||||||||||||||||||||||
| 59 | 30 | my $dir = $self->storage_dir; | |||||||||||||||||||||||||||
| 60 | 30 | my $extension = $self->storage_extension(); | |||||||||||||||||||||||||||
| 61 | my @files = map { | ||||||||||||||||||||||||||||
| 62 | 30 | scalar( s/\.$extension//, basename $_) | |||||||||||||||||||||||||||
| 63 | 7 | ▶ | } glob("$dir/*.$extension"); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 64 | 30 | ▶ | return wantarray ? @files : \@files; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | sub num_objects | ||||||||||||||||||||||||||||
| 69 | 19 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
num_objects: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 19 | my $self = shift; | |||||||||||||||||||||||||||
| 72 | 19 | ▶ | return scalar @{ $self->object_names } || 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
scalar @{$self->object_names;} || 0
MC/DC100%1/1
scalar @{$self->object_names;} || 0
scalar @{$self->object_names;}
Truth table100%2/2
A
A= scalar @{$self->object_names;}
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | sub delete_from_storage | ||||||||||||||||||||||||||||
| 77 | 25 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
delete_from_storage: called CC=5 SCAR=18.7
Pod
documented
| |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 25 | my $self = shift; | |||||||||||||||||||||||||||
| 80 | 25 | my $name = shift; | |||||||||||||||||||||||||||
| 81 | 25 | my $file = $self->storage_file($name); | |||||||||||||||||||||||||||
| 82 | 25 | ▶ | unless( $file ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 0 | $! = "no object by that name"; | |||||||||||||||||||||||||||
| 84 | 0 | return undef; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | 25 | ▶ | open( OUT, '+< ' . $file ) or croak "cannot open $file: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 25 | ▶ | flock OUT, LOCK_EX or croak "cannot lock $file: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 25 | ▶ | unlink($file) && return 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 0 | return undef; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | sub purge | ||||||||||||||||||||||||||||
| 94 | 7 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
purge: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 7 | my $self = shift; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 7 | my $min_ts; | |||||||||||||||||||||||||||
| 99 | 7 | ▶ | if( my $min_age = shift ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 2 | my $subtract = Mail::Action::Address->process_time( $min_age ); | |||||||||||||||||||||||||||
| 101 | 2 | $min_ts = time() - $subtract; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | else { | ||||||||||||||||||||||||||||
| 104 | 5 | $min_ts = time(); | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 7 | my $purged = 0; | |||||||||||||||||||||||||||
| 108 | 7 | for( $self->object_names ) { | |||||||||||||||||||||||||||
| 109 | 75 | my $addy = $self->fetch($_); | |||||||||||||||||||||||||||
| 110 | 75 | ▶ | next unless $addy; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 75 | my $expires = $addy->expires(); | |||||||||||||||||||||||||||
| 112 | 75 | ▶ | if( $expires && $expires < $min_ts ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$expires and $expires < $min_ts
MC/DC100%2/2
$expires and $expires < $min_ts
$expires
$expires < $min_ts
Truth table100%3/3
A and B
A= $expiresB= $expires < $min_ts
| |||||||||||||||||||||||||||||
| 113 | 25 | ▶ | $self->delete_from_storage($_) && $purged++; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 7 | return $purged; | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | # keep require happy | ||||||||||||||||||||||||||||
| 122 | 7 | 1; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | __END__ | ||||||||||||||||||||||||||||