File Coverage

lib/App/MtAws/QueueJob.pm
Criterion Covered Total %
statement 63 66 95.4
branch 25 34 73.5
condition 2 3 66.6
subroutine 14 17 82.3
pod 0 11 0.0
total 104 131 79.3


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;
22              
23             our $VERSION = '1.114_2';
24              
25 33     33   41815 use strict;
  33         52  
  33         910  
26 33     33   132 use warnings;
  33         49  
  33         793  
27              
28 33     33   132 use Carp;
  33         40  
  33         1515  
29 33     33   814 use App::MtAws::QueueJobResult;
  33         41  
  33         23010  
30              
31             sub new
32             {
33 1455     1455 0 1369657 my ($class, %args) = @_;
34 1455         1989 my $self = \%args;
35 1455         2085 bless $self, $class;
36 1455         2865 $self->{_state} = 'default';
37 1455         2094 $self->{_jobs} = []; # Jobs is array, but, as you can see below only one element can be stored in it at time.
38 1455         4296 $self->init();
39 1363         4053 return $self;
40             }
41              
42 4449     4449 0 6523 sub enter { $_[0]->{_state} = $_[1]; JOB_RETRY }
  4449         3998  
43              
44             sub set_task_proxy_callback
45             {
46 3300     3300 0 3398 my ($self, $res) = @_;
47 3300         3280 my $cb = $res->{task}{cb};
48             $res->{task}{cb_task_proxy} = sub {
49 3026 100   3026   392627 if (my @r = $cb->(@_)) {
50 833         1759 my $result = parse_result(@r);
51 833 50       2577 $self->enter($result->{state}) if defined($result->{state});
52 833 50       1375 confess if $result->{job};
53 833 50       1893 confess if $result->{task};
54             }
55 3025         21962 return;
56             }
57 3300         12803 }
58              
59              
60             sub set_job_proxy_callback
61             {
62 303     303 0 321 my ($self, $j) = @_;
63 303         372 my $cb = $j->{cb};
64             $j->{cb_job_proxy} = sub {
65 297 50   297   978 if (my @r = $cb->($j->{job})) {
66 297         637 my $result = parse_result(@r);
67 297 50       986 $self->enter($result->{state}) if defined($result->{state});
68 297 50       530 confess if $result->{job};
69 297 50       1032 confess if $result->{task};
70             }
71 297         2624 undef $j;
72             }
73 303         1245 }
74              
75             sub push_job
76             {
77 304     304 0 353 my ($self, $j) = @_;
78 304 100       964 $self->set_job_proxy_callback($j) if ($j->{cb});
79 304         315 push @{ $self->{_jobs} }, $j;
  304         553  
80             }
81              
82             sub next
83             {
84 8478     8478 0 250114 my ($self) = @_;
85              
86 8478         7182 while () {
87 11076 100       8187 if ( @{ $self->{_jobs} } ) {
  11076         17545  
88 1814         3297 my $res = $self->{_jobs}[-1]{job}->next();
89 1814 50       4404 confess unless $res->isa('App::MtAws::QueueJobResult');
90 1814 100       2528 if ($res->{code} eq JOB_DONE) {
91 298         280 my $j = pop @{ $self->{_jobs} };
  298         496  
92 298 100       910 $j->{cb_job_proxy}->() if $j->{cb_job_proxy};
93             #redo; # we already 'redo' in this loop
94             } else {
95 1516         3270 return $res;
96             }
97             } else {
98 9262         13859 my $method = "on_$self->{_state}";
99 9262         22467 my $res = parse_result($self->$method());
100 9262 100       23345 $self->enter(delete $res->{state}) if defined($res->{state});
101 9262 100       14376 $self->push_job(delete $res->{job}) if defined($res->{job});
102 9262 50 66     20635 $self->set_task_proxy_callback($res) if $res->{task} && $res->{task}{cb};
103 9262 100       17339 redo if $res->{code} eq JOB_RETRY;
104 6962         15510 return $res;
105             }
106             }
107             }
108              
109 1576     1576 0 3463 sub on_wait { JOB_WAIT }
110 1267     1267 0 3147 sub on_done { JOB_DONE }
111 0     0 0   sub on_die { confess "on_die"; }
112 0     0 0   sub on_default { confess "Unimplemented"; }
113 0     0 0   sub init { confess "Unimplemented"; }
114              
115              
116              
117             1;