lib/Net/Download/Queue.pm

filter: Statementstmt 87.7% 43 / 49 Branchbran 50.0% 2 / 4 Conditioncond 75.0% 3 / 4 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 87.5% 14 / 16 Podpod 100% 7 / 7 totaltotal 85.3% 70 / 82 CC 6 SCAR 6.9
CC
6
Cov
84%
percentComplete
6.0
oDownloadAdd
3.0
oDownloadDequeue
2.0
CRAP 2.0
« lib/Net/Download/Queue/Download.pm ↑ Summary lib/Net/Download/Queue/DBI.pm »
1 =head1 NAME
2
3 Net::Download::Queue - Download files with one or more workers.
4
5 =head1 SYNOPSIS
6
7 # *** In the app which needs to download files
8 use Net::Download::Queue;
9 my $oQueue = Net::Download::Queue->new() or die;
10 my $oDownload = $oQueue->oDownloadAdd(
11 "http://www.darserman.com/Perl/TexQL/texql.pl",
12 "./downloads",
13 "texql.pl.txt",
14 $urlReferer, #Optional
15 ) or die;
16 #The url is now queued
17
18
19 # *** Or using the command line
20 download_queue --url=http://www.darserman.com/Perl/TexQL/texql.pl \
21 --dir=./downloads --file=texql.pl.txt --
22 #The url is now queued
23
24
25
26 # *** On another command line (you can have many of these)
27 download_queue --process
28 #Urls are downloaded as they appear in the queue
29
30
31 =head1 DESCRIPTION
32
33 Download files asynchronously in a queued fashion.
34
35 Your application (or a CLI script) can add files to the queue, and
36 other workes will process the queue and actually download the files.
37
38 =cut
39
40
41
42
43
44 package Net::Download::Queue;
45
46 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
48
49 7 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
50
51 7 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 7 ▶use HTTP::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 7 ▶use HTTP::Request::Common qw(HEAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
54
55 7 ▶use Net::Download::Queue::DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 7 ▶use Net::Download::Queue::Download;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 7 ▶use Net::Download::Queue::DownloadStatus;
Subroutine BEGIN: called CC=1 SCAR=0.0
58
59
60
61
62
63
64 7 our $VERSION = '0.04';
65
66
67
68 =head1 METHODS
69
70 =head2 new()
71
72 Create new queue.
73
74 =cut
75 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
76 2 my $self = bless {}, shift;
77
78 2 return($self);
79 }
80
81
82
83
84
85 =head2 oDownloadAdd($url, $dirDownload, $file, [$urlReferer = ""])
86
87 Add $url to the queue, to be downloaded in $dirDownload/$file.
88
89 Return the new Download object on success, else die.
90
91 =cut
92 2 ▶sub oDownloadAdd {
Subroutine oDownloadAdd: called CC=3 SCAR=11.0
Pod documented
93 2 my $self = shift;
94 2 my ($url, $dirDownload, $file, $urlReferer) = @_;
95 2 ▶ $urlReferer ||= "";
Condition100%2/2
$urlReferer ||= ""
l!l
11
MC/DC100%1/1
$urlReferer ||= ""
$urlReferer
Truth table100%2/2
A
A= $urlReferer
Aresult
11
00
96
97 2 ▶ return( Net::Download::Queue::Download->create({
Condition50%1/2
'Net::Download::Queue::Download'->create({'url', $url, 'dirDownload', $dirDownload, 'fileDownload', $file, 'urlReferer', $urlReferer}) || die()
l!l
20
MC/DC0%0/1
'Net::Download::Queue::Download'->create({'url', $url, 'dirDownload', $dirDownload, 'fileDownload', $file, 'urlReferer', $urlReferer}) || die()
'Net::Download::Queue::Download'->create({'url', $url, 'dirDownload', $dirDownload, 'fileDownload', $file, 'urlReferer', $urlReferer})
Truth table50%1/2
A
A= 'Net::Download::Queue::Download'->create({'url', $url, 'dirDownload', $dirDownload, 'fileDownload', $file, 'urlReferer', $urlReferer})
Aresult
11
00
98 url => $url,
99 dirDownload => $dirDownload,
100 fileDownload => $file,
101 urlReferer => $urlReferer,
102 }) or die );
103
104 }
105
106
107
108
109
110 =head2 oDownloadDequeue()
111
112 If there is any pending download in the queue, return a Download
113 object, or return undef if there are no pending downloads.
114
115 The Download object is now in "downloading" state.
116
117 Die on errors.
118
119 =cut
120 4 ▶sub oDownloadDequeue {
Subroutine oDownloadDequeue: called CC=2 SCAR=6.9
Pod documented
121 4 my $self = shift;
122
123 #Race condition, ignore that for now.
124 4 ▶ my $oDownload = Net::Download::Queue::Download->search_first({
Branch: 1 branch
BranchTrueFalseTotal
my $oDownload = "Net::Download::Queue::Download"->search_first({"download_status_id", "Net::Download::Queue::Download"->oDownloadStatus("queued")}) or return undef 1 3 4
125 download_status_id => Net::Download::Queue::Download->oDownloadStatus("queued"),
126 }) or return(undef);
127
128 1 $oDownload->setDownloading();
129 #End race condition
130
131 1 return($oDownload);
132 }
133
134
135
136
137
138 =head2 requeueDownloading()
139
140 Set any downloads in status "downloading" back into status "queued".
141
142 Die on errors.
143
144 =cut
145 1 ▶sub requeueDownloading {
Subroutine requeueDownloading: called CC=2 SCAR=6.9
Pod documented
146 1 my $self = shift;
147
148 1 $_->setQueued() for(Net::Download::Queue::Download->retrieve_downloading);
149
150 1 return(1);
151 }
152
153
154
155
156
157 =head2 percentComplete()
158
159 Return the percent 0..100 how complete the download of all current
160 downloads are.
161
162 If none is current, that's 100% complete.
163
164 Die on errors.
165
166 =cut
167 0 ▶sub percentComplete {
Subroutine percentComplete: not called CC=2 SCAR=17.9
Pod documented
168 0 my $self = shift;
169
170 0 ▶ my $currentCount = Net::Download::Queue::Download->sql_bytesSumCurrent->select_val or return(100);
Branch: 1 branch
BranchTrueFalseTotal
my $currentCount = "Net::Download::Queue::Download"->sql_bytesSumCurrent->select_val or return 100 0 0 0
171 0 my $downloadingCount = Net::Download::Queue::Download->sql_bytesSumDownloading->select_val;
172 # my $currentCount = Net::Download::Queue::Download->retrieve_current or return(100);
173 # my $downloadingCount = Net::Download::Queue::Download->retrieve_downloading;
174
175 0 my $percent = $downloadingCount / ($currentCount + $downloadingCount) * 100;
176
177 0 return($percent);
178 }
179
180
181
182
183
184 =head1 CLASS METHODS
185
186 =head2 rebuildDatabase()
187
188 Empty and rebuild the SQLite database.
189
190 Return 1 on success, else die on errors.
191
192 =cut
193 5 ▶sub rebuildDatabase {
Subroutine rebuildDatabase: called CC=1 SCAR=0.0
Pod documented
194 5 Net::Download::Queue::DBI->rebuildDatabase();
195 }
196
197
198
199
200
201 =head2 ensureDatabase()
202
203 Rebuild the SQLite database if it's not present.
204
205 Return 1 on success, else die on errors.
206
207 =cut
208 0 ▶sub ensureDatabase {
Subroutine ensureDatabase: not called CC=1 SCAR=6.9
Pod documented
209 0 Net::Download::Queue::DBI->ensureDatabase();
210 }
211
212
213
214
215
216 7 1;
217
218
219
220
221
222 __END__
« lib/Net/Download/Queue/Download.pm ↑ Summary lib/Net/Download/Queue/DBI.pm »