File Coverage

lib/App/MtAws/ParentWorker.pm
Criterion Covered Total %
statement 24 58 41.3
branch 0 10 0.0
condition 0 6 0.0
subroutine 8 13 61.5
pod 0 5 0.0
total 32 92 34.7


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::ParentWorker;
22              
23             our $VERSION = '1.114_2';
24              
25 18     18   72 use strict;
  18         22  
  18         460  
26 18     18   56 use warnings;
  18         21  
  18         449  
27 18     18   78 use utf8;
  18         34  
  18         92  
28 18     18   2007 use App::MtAws::LineProtocol;
  18         24  
  18         780  
29 18     18   146 use Carp;
  18         18  
  18         959  
30 18     18   1779 use POSIX;
  18         20539  
  18         152  
31 18     18   31571 use App::MtAws::Utils;
  18         24  
  18         2190  
32 18     18   93 use base q{App::MtAws::QueueEngine};
  18         24  
  18         9314  
33              
34             sub init
35             {
36 0     0 0   my ($self, %args) = @_;
37 0   0       $self->{$_} = $args{$_} || confess for (qw/children disp_select options/);
38 0           $self->add_worker($_) for (keys %{$self->{children}});
  0            
39             }
40              
41             sub queue
42             {
43 0     0 0   my ($self, $worker_id, $task) = @_;
44 0           my $worker = $self->{children}{$worker_id};
45 0 0         send_data($worker->{tochild}, $task->{action}, $task->{_id}, $task->{args}, $task->{attachment}) or
46             $self->comm_error;
47              
48             }
49              
50             sub wait_worker
51             {
52 0     0 0   my ($self) = @_;
53 0           my @ready;
54 0   0       do { @ready = $self->{disp_select}->can_read(); } until @ready || $! != EINTR;
  0            
55 0           for my $fh (@ready) {
56 0           my ($pid, undef, $taskid, $data, $resultattachmentref) = get_data($fh);
57 0 0         $pid or $self->comm_error;
58              
59 0           my $task = $self->unqueue_task($pid);
60              
61 0 0         confess unless $taskid == $task->{_id};
62              
63 0           $task->{result} = $data;
64 0           $task->{attachmentref} = $resultattachmentref;
65              
66 0           print "PID $pid $data->{console_out}\n";
67              
68 0           $task->{cb_task_proxy}->($data, $resultattachmentref);
69              
70 0 0         if ($data->{journal_entry}) {
71 0 0         confess unless defined $self->{journal};
72 0           $self->{journal}->add_entry($data->{journal_entry});
73             }
74 0           return;
75             }
76 0           return 0;
77             }
78              
79             sub process_task
80             {
81 0     0 0   my ($self, $lt, $j) = @_;
82 0           $self->{journal} = $j;
83 0           $self->process($lt);
84             }
85              
86             sub comm_error
87             {
88 0     0 0   my ($self) = @_;
89 0           sleep 1; # let's wait for SIGCHLD in order to have same error message in same cases
90 0           kill (POSIX::SIGUSR2, keys %{$self->{children}});
  0            
91 0           print STDERR "EXIT eof/error when communicate with child process\n";
92 0           exit(1);
93             }
94              
95             1;