forked from vsespb/mt-aws-glacier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParentWorker.pm
123 lines (111 loc) · 3.22 KB
/
ParentWorker.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# 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 ParentWorker;
use strict;
use warnings;
use utf8;
use LineProtocol;
use Carp;
sub new
{
my ($class, %args) = @_;
my $self = \%args;
$self->{children}||die;
$self->{disp_select}||die;
$self->{options}||die;
@{$self->{freeworkers}} = keys %{$self->{children}};
bless $self, $class;
return $self;
}
sub process_task
{
my ($self, $ft, $journal) = @_;
my $task_list = {};
while (1) {
if ( @{$self->{freeworkers}} ) {
my ($result, $task) = $ft->get_task();
if ($result eq 'wait') {
if (scalar keys %{$self->{children}} == scalar @{$self->{freeworkers}}) {
die;
}
my $r = $self->wait_worker($task_list, $ft, $journal);
return $r if $r;
} elsif ($result eq 'ok') {
my $worker_pid = shift @{$self->{freeworkers}};
my $worker = $self->{children}->{$worker_pid};
$task_list->{$task->{id}} = $task;
send_command($worker->{tochild}, $task->{id}, $task->{action}, $task->{data}, $task->{attachment});
} else {
die;
}
} else {
my $r = $self->wait_worker($task_list, $ft, $journal);
return $r if $r;
}
}
}
sub wait_worker
{
my ($self, $task_list, $ft, $journal) = @_;
my @ready = $self->{disp_select}->can_read();
for my $fh (@ready) {
if (eof($fh)) {
$self->{disp_select}->remove($fh);
die "Unexpeced EOF in Pipe";
next;
}
my ($pid, $taskid, $data) = get_response($fh);
push @{$self->{freeworkers}}, $pid;
die unless my $task = $task_list->{$taskid};
$task->{result} = $data;
print "PID $pid $task->{result}->{console_out}\n";
my ($result) = $ft->finish_task($task);
delete $task_list->{$taskid};
if ($task->{result}->{journal_entry}) {
confess unless defined $journal;
$journal->add_entry($task->{result}->{journal_entry});
}
if ($result eq 'done') {
return $task->{result};
}
}
return 0;
}
sub send_command
{
my ($fh, $taskid, $action, $data, $attachmentref) = @_;
my $data_e = encode_data($data);
my $attachmentsize = $attachmentref ? length($$attachmentref) : 0;
my $line = "$taskid\t$action\t$attachmentsize\t$data_e\n";
#print ">$line\n";
print $fh $line;
print $fh $$attachmentref if $attachmentsize;
}
sub get_response
{
my ($fh) = @_;
my $line = <$fh>;
chomp $line;
# print "<$line\n";
my ($pid, $taskid, $data_e) = split /\t/, $line;
my $data = decode_data($data_e);
return ($pid, $taskid, $data);
}
1;