forked from vsespb/mt-aws-glacier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJobListProxy.pm
103 lines (92 loc) · 2.48 KB
/
JobListProxy.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# mt-aws-glacier - Amazon Glacier sync client
# Copyright (C) 2012-2013 Victor Efimov
# http://mt-aws.com (also http://vs-dev.com) [email protected]
# License: GPLv3
#
# This file is part of "mt-aws-glacier"
#
# mt-aws-glacier is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# mt-aws-glacier is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
package JobListProxy;
use strict;
use warnings;
use utf8;
use ProxyTask;
sub new
{
my ($class, %args) = @_;
my $self = \%args;
$self->{jobs}||die;
$self->{jobs_h} = {};
$self->{jobs_a} = [];
my $i = 1;
for my $job (@{$self->{jobs}}) {
push @{$self->{jobs_a}}, { jobid => $i, job => $job };
$self->{jobs_h}->{$i} = $job;
++$i;
}
$self->{pending}={};
$self->{uid}=0;
$self->{all_raised} = 0;
bless $self, $class;
return $self;
}
# returns "ok" "wait" "ok subtask"
sub get_task
{
my ($self) = @_;
if (scalar @{$self->{jobs_a}}) {
my $maxcnt = 30;
for my $job (@{$self->{jobs_a}}) {
my ($status, $task) = $job->{job}->get_task();
if ($status eq 'wait') {
last unless ($maxcnt--);
} else {
my $newtask = ProxyTask->new(id => ++$self->{uid}, jobid => $job->{jobid}, task => $task);
$self->{pending}->{$newtask->{id}} = $newtask;
return ($status, $newtask);
}
}
return ('wait');
} else {
die;
}
}
# returns "ok", "done"
sub finish_task
{
my ($self, $task) = @_;
my $jobid = $task->{jobid};
$task->{task}->{result} = $task->{result}; # TODO: move to ProxyTask
my ($status, @res) = $self->{jobs_h}->{$jobid}->finish_task($task->{task});
delete $self->{pending}->{$task->{id}};
if ($status eq 'ok'){
return ("ok");
} elsif ($status eq 'done') {
delete $self->{jobs_h}->{$jobid};
my $idx = 0;
for my $j (@{$self->{jobs_a}}) {
if ($j->{jobid} == $task->{jobid}) {
splice(@{$self->{jobs_a}}, $idx, 1);
last;
}
++$idx;
}
if (scalar @{$self->{jobs_a}}) {
return 'ok';
} else {
return 'done';
}
}
}
1;