File Coverage

lib/App/MtAws/QueueJob/DownloadSegments.pm
Criterion Covered Total %
statement 54 55 100.0
branch 24 28 89.2
condition n/a
subroutine 11 11 100.0
pod 0 4 0.0
total 89 98 92.8


line stmt bran cond sub pod time code
1             # mt-aws-glacier - Amazon Glacier sync client
2             # Copyright (C) 2012-2014 Victor Efimov
3             # http://mt-aws.com (also http://vs-dev.com) vs@vs-dev.com
4             # License: GPLv3
5             #
6             # This file is part of "mt-aws-glacier"
7             #
8             # mt-aws-glacier is free software: you can redistribute it and/or modify
9             # it under the terms of the GNU General Public License as published by
10             # the Free Software Foundation, either version 3 of the License, or
11             # (at your option) any later version.
12             #
13             # mt-aws-glacier is distributed in the hope that it will be useful,
14             # but WITHOUT ANY WARRANTY; without even the implied warranty of
15             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16             # GNU General Public License for more details.
17             #
18             # You should have received a copy of the GNU General Public License
19             # along with this program. If not, see <http://www.gnu.org/licenses/>.
20              
21             package App::MtAws::QueueJob::DownloadSegments;
22              
23             our $VERSION = '1.114_2';
24              
25 8     8   1185 use strict;
  8         12  
  8         201  
26 8     8   29 use warnings;
  8         13  
  8         177  
27 8     8   25 use Carp;
  8         13  
  8         368  
28              
29 8     8   39 use App::MtAws::QueueJobResult;
  8         12  
  8         410  
30 8     8   102 use base 'App::MtAws::QueueJob';
  8         11  
  8         1802  
31 8     8   3452 use App::MtAws::IntermediateFile;
  8         16  
  8         3288  
32              
33             sub init
34             {
35 275     275 0 396 my ($self) = @_;
36              
37 275 100       842 $self->{size}||confess;
38 274 100       870 $self->{archive_id}||confess;
39 273 100       692 $self->{jobid}||confess;
40 272 100       716 $self->{file_downloads}{'segment-size'}||confess;
41 271 100       753 defined($self->{relfilename})||confess;
42 270 100       745 defined($self->{filename})||confess;
43 269 100       709 defined($self->{mtime})||confess;
44              
45 268         426 $self->{position} = 0;
46 268         532 $self->{segments} = {};
47              
48 268         775 $self->enter("tempfile");
49 268         512 return $self;
50             }
51              
52             sub on_tempfile
53             {
54 264     264 0 325 my ($self) = @_;
55 264         1159 $self->{i_tmp} = App::MtAws::IntermediateFile->new(target_file => $self->{filename}, mtime => $self->{mtime});
56 264         1969 return state('download'), JOB_RETRY;
57             }
58              
59             sub on_download
60             {
61 1634     1634 0 1871 my ($self) = @_;
62              
63             # uncoverable branch false count:3
64 1634 100       3406 if ($self->{position} < $self->{size}) {
    50          
65 1374         1654 my $download_size = $self->{size} - $self->{position};
66 1374 50       2868 my $segment_size = $self->{file_downloads}{'segment-size'}*1048576 or confess;
67 1374 100       2541 $download_size = $segment_size if $download_size > $segment_size;
68              
69 1374         1599 my $position_now = $self->{position}; # self->position will change, unlike position_now
70              
71             # uncoverable branch true
72 1374 50       4206 confess if $self->{segments}{ $position_now }++;
73              
74             my @result = task "segment_download_job", {
75 5496         11333 (map { $_ => $self->{$_} } qw/archive_id relfilename filename jobid/),
76             position => $self->{position}, download_size => $download_size, tempfile => $self->{i_tmp}->tempfilename
77             } => sub {
78 1112 50   1112   2883 delete $self->{segments}{ $position_now } or confess;
79 1112         3434 return;
80 1374         1940 };
81              
82 1374         2938 $self->{position} += $download_size;
83              
84 1374         4422 return @result;
85             } elsif ($self->{position} == $self->{size}) {
86 260         730 return state 'finishing';
87             } else {
88 0         0 confess; # uncoverable statement
89             }
90             }
91              
92             sub on_finishing
93             {
94 568     568 0 634 my ($self) = @_;
95 568 100       532 if (keys %{$self->{segments}}) {
  568         1421  
96 342         991 JOB_WAIT
97             } else {
98 226         865 $self->{i_tmp}->make_permanent;
99 226         54885 undef $self->{i_tmp}; # explicit destroy for very-very old File::Temp
100 226         629 state('done');
101             }
102             }
103              
104             1;