| 1 | package PkgForge::Queue; # -*-perl-*- | ||
| 2 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | # $Id: Queue.pm.in 15409 2011-01-12 17:25:17Z squinney@INF.ED.AC.UK $ | ||
| 6 | # $Source:$ | ||
| 7 | # $Revision: 15409 $ | ||
| 8 | # $HeadURL: https://svn.lcfg.org/svn/source/tags/PkgForge-Server/PkgForge_Server_1_1_10/lib/PkgForge/Queue.pm.in $ | ||
| 9 | # $Date: 2011-01-12 17:25:17 +0000 (Wed, 12 Jan 2011) $ | ||
| 10 | |||
| 11 | our $VERSION = '1.1.10'; | ||
| 12 | |||
| 13 | 2 | ▶ | use File::Spec (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | 2 | ▶ | use IO::Dir (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 15 | 0 | ▶ | use PkgForge::Queue::Entry (); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 16 | |||
| 17 | use Moose; | ||
| 18 | use PkgForge::Types qw(AbsolutePathDirectory); | ||
| 19 | use MooseX::Types::Moose qw(Bool Str); | ||
| 20 | |||
| 21 | use overload q{""} => sub { shift->stringify }; | ||
| 22 | |||
| 23 | has 'logger' => ( | ||
| 24 | is => 'ro', | ||
| 25 | isa => 'Log::Dispatch::Config', | ||
| 26 | predicate => 'has_logger', | ||
| 27 | documentation => 'Optional logging object', | ||
| 28 | ); | ||
| 29 | |||
| 30 | has 'directory' => ( | ||
| 31 | is => 'ro', | ||
| 32 | isa => AbsolutePathDirectory, | ||
| 33 | required => 1, | ||
| 34 | default => sub { File::Spec->curdir() }, | ||
| 35 | documentation => 'The directory where the queue is stored', | ||
| 36 | ); | ||
| 37 | |||
| 38 | has 'allow_symlinks' => ( | ||
| 39 | is => 'ro', | ||
| 40 | isa => Bool, | ||
| 41 | default => 0, | ||
| 42 | documentation => 'Allow symbolic links within a build queue directory', | ||
| 43 | ); | ||
| 44 | |||
| 45 | has 'cruft' => ( | ||
| 46 | traits => ['Array'], | ||
| 47 | is => 'rw', | ||
| 48 | isa => 'ArrayRef[Str]', | ||
| 49 | default => sub { [] }, | ||
| 50 | auto_deref => 1, | ||
| 51 | handles => { | ||
| 52 | clear_cruft => 'clear', | ||
| 53 | add_cruft => 'push', | ||
| 54 | count_cruft => 'count', | ||
| 55 | }, | ||
| 56 | documentation => 'Any cruft found', | ||
| 57 | ); | ||
| 58 | |||
| 59 | has 'entries' => ( | ||
| 60 | traits => ['Array'], | ||
| 61 | is => 'rw', | ||
| 62 | isa => 'ArrayRef[PkgForge::Queue::Entry]', | ||
| 63 | default => sub { [] }, | ||
| 64 | auto_deref => 1, | ||
| 65 | handles => { | ||
| 66 | clear_entries => 'clear', | ||
| 67 | add_entries => 'push', | ||
| 68 | count_entries => 'count', | ||
| 69 | }, | ||
| 70 | documentation => 'The list of entries found', | ||
| 71 | ); | ||
| 72 | |||
| 73 | around 'BUILDARGS' => sub { | ||
| 74 | my ( $orig, $class, @args ) = @_; | ||
| 75 | |||
| 76 | if ( @args == 1 && !ref $args[0] ) { | ||
| 77 | return $class->$orig( directory => $args[0] ); | ||
| 78 | } else { | ||
| 79 | return $class->$orig(@args); | ||
| 80 | } | ||
| 81 | }; | ||
| 82 | |||
| 83 | no Moose; | ||
| 84 | __PACKAGE__->meta->make_immutable; | ||
| 85 | |||
| 86 | sub stringify { | ||
| 87 | my ($self) = @_; | ||
| 88 | return $self->directory; | ||
| 89 | } | ||
| 90 | |||
| 91 | sub rescan { | ||
| 92 | my ($self) = @_; | ||
| 93 | |||
| 94 | my $dir = $self->directory; | ||
| 95 | |||
| 96 | my $dh = IO::Dir->new($dir) | ||
| 97 | or die "Could not open $dir: $!\n"; | ||
| 98 | |||
| 99 | my @cruft; | ||
| 100 | my @entries; | ||
| 101 | |||
| 102 | while ( defined( my $entry = $dh->read ) ) { | ||
| 103 | if ( $entry eq q{.} || $entry eq q{..} ) { | ||
| 104 | next; | ||
| 105 | } | ||
| 106 | my $path = File::Spec->catdir( $dir, $entry ); | ||
| 107 | if ( !-d $path || ( -l $path && !$self->allow_symlinks ) ) { | ||
| 108 | push @cruft, $path; | ||
| 109 | } else { | ||
| 110 | my $entry = PkgForge::Queue::Entry->new( path => $path ); | ||
| 111 | push @entries, $entry; | ||
| 112 | } | ||
| 113 | } | ||
| 114 | |||
| 115 | my @sorted = $self->sorted_entries(@entries); | ||
| 116 | $self->cruft( \@cruft ); | ||
| 117 | $self->entries( \@sorted ); | ||
| 118 | |||
| 119 | return; | ||
| 120 | } | ||
| 121 | |||
| 122 | sub erase_cruft { | ||
| 123 | my ($self) = @_; | ||
| 124 | |||
| 125 | for my $cruft ( $self->cruft ) { | ||
| 126 | if ( $self->has_logger ) { | ||
| 127 | $self->logger->notice("Removing $cruft, not a job directory"); | ||
| 128 | } | ||
| 129 | |||
| 130 | my $ok = unlink $cruft; # Will not be a directory | ||
| 131 | |||
| 132 | if ( !$ok ) { | ||
| 133 | my $msg = "Could not unlink $cruft: $!"; | ||
| 134 | if ( $self->has_logger ) { | ||
| 135 | $self->logger->error($msg); | ||
| 136 | } else { | ||
| 137 | warn "$msg\n"; | ||
| 138 | } | ||
| 139 | } | ||
| 140 | } | ||
| 141 | |||
| 142 | return; | ||
| 143 | } | ||
| 144 | |||
| 145 | sub sorted_entries { | ||
| 146 | my ( $self, @entries ) = @_; | ||
| 147 | |||
| 148 | my @sorted = sort { $a->timestamp <=> $b->timestamp } @entries; | ||
| 149 | return @sorted; | ||
| 150 | } | ||
| 151 | |||
| 152 | sub BUILD { | ||
| 153 | my ($self) = @_; | ||
| 154 | |||
| 155 | $self->rescan(); | ||
| 156 | |||
| 157 | return; | ||
| 158 | } | ||
| 159 | |||
| 160 | 1; | ||
| 161 | __END__ | ||