File Coverage

blib/lib/PkgForge/Queue.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             package PkgForge::Queue; # -*-perl-*-
2 2     2   69758 use strict;
  2         5  
  2         61  
3 2     2   10 use warnings;
  2         4  
  2         83  
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     2   10 use File::Spec ();
  2         6  
  2         25  
14 2     2   1577 use IO::Dir ();
  2         15029  
  2         44  
15 2     2   792 use PkgForge::Queue::Entry ();
  0            
  0            
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__
162              
163             =head1 NAME
164              
165             PkgForge::Queue - Represents a build queue for the LCFG Package Forge
166              
167             =head1 VERSION
168              
169             This documentation refers to PkgForge::Queue version 1.1.10
170              
171             =head1 SYNOPSIS
172              
173             use PkgForge::Queue;
174             use PkgForge::Job;
175              
176             my $queue = PkgForge::Queue->new( directory => "/tmp/incoming" );
177              
178             for my $entry ($queue->entries) {
179             my $job = PkgForge::Job->new_from_qentry($qentry);
180              
181             $job->validate();
182             }
183              
184             =head1 DESCRIPTION
185              
186             In the LCFG Package Forge a build queue is represented by a
187             directory. The jobs in a queue are each represented by separate
188             sub-directories within that build queue directory.
189              
190             This module is used as a lightweight representation of a queue. It is
191             basically a means of finding all the build queue entry
192             sub-directories.
193              
194             =head1 ATTRIBUTES
195              
196             These attributes are all only settable when the Queue object is
197             created. After that point they are all read-only.
198              
199             =over 4
200              
201             =item directory
202              
203             The directory in which the queue is stored.
204              
205             =item allow_symlinks
206              
207             This is a boolean value which controls whether or not queue items in
208             the directory can be symbolic links. By default this option is false.
209              
210             =item cruft
211              
212             A list of anything found in the queue directory which is not a valid
213             queue item. This is anything which is not a directory and, depending
214             on the setting of the C<allow_symlinks> attribute, might also contain
215             symbolic links to directories.
216              
217             =item entries
218              
219             This is a list of L<PkgForge::Queue::Entry> items representing
220             the sub-directories found in the queue directory.
221              
222             =back
223              
224             =head1 SUBROUTINES/METHODS
225              
226             =over 4
227              
228             =item new
229              
230             This creates a new Queue object. The directory attribute must be specified.
231              
232             =item rescan
233              
234             This forces the queue object to rescan the directory and resets the
235             cruft and entry lists.
236              
237             =item sorted_entries
238              
239             Returns the list of entries sorted by the timestamp.
240              
241             =item clear_entries
242              
243             Empties the list of entries
244              
245             =item add_entries
246              
247             Adds L<Queue::Entry> objects to the entries list.
248              
249             =item count_entries
250              
251             Returns the size of the list of entries.
252              
253             =item clear_cruft
254              
255             Empties the list of cruft
256              
257             =item add_cruft
258              
259             Adds items to the list of cruft.
260              
261             =item count_cruft
262              
263             Returns the size of the list of cruft.
264              
265             =back
266              
267             =head1 DEPENDENCIES
268              
269             This module is powered by L<Moose> and uses L<MooseX::Types>
270              
271             =head1 SEE ALSO
272              
273             L<PkgForge>, L<PkgForge::Queue::Entry>, L<PkgForge::Utils>
274              
275             =head1 PLATFORMS
276              
277             This is the list of platforms on which we have tested this
278             software. We expect this software to work on any Unix-like platform
279             which is supported by Perl.
280              
281             ScientificLinux5, Fedora13
282              
283             =head1 BUGS AND LIMITATIONS
284              
285             Please report any bugs or problems (or praise!) to bugs@lcfg.org,
286             feedback and patches are also always very welcome.
287              
288             =head1 AUTHOR
289              
290             Stephen Quinney <squinney@inf.ed.ac.uk>
291              
292             =head1 LICENSE AND COPYRIGHT
293              
294             Copyright (C) 201O University of Edinburgh. All rights reserved.
295              
296             This library is free software; you can redistribute it and/or modify
297             it under the terms of the GPL, version 2 or later.
298              
299             =cut