blib/lib/App/CLI/Plugin/Parallel/ForkManager.pm

filter: Statementstmt 65.2% 15 / 23 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 80.0% 4 / 5 Podpod 0.0% 0 / 1 totaltotal 50.0% 19 / 38 CC 4 SCAR 23.5
CC
4
Cov
50%
setup
20.0
BEGIN
1.0
BEGIN
1.0
CRAP 10.5
↑ Summary
1 package App::CLI::Plugin::Parallel::ForkManager;
2  
3 =pod
4  
5 =head1 NAME
6  
7 App::CLI::Plugin::Parallel::ForkManager - for App::CLI::Extension fork plugin module
8  
9 =head1 VERSION
10  
11 1.1
12  
13 =head1 SYNOPSIS
14  
15 # MyApp.pm
16 package MyApp;
17
18 use strict;
19 use base qw(App::CLI::Extension);
20
21 # extension method
22 __PACKAGE__->load_plugins(qw(Parallel::ForkManager));
23
24 # extension method
25 __PACKAGE__->config( parallel_fork_manager => 5 );
26
27 1;
28
29 # MyApp/Fork.pm
30 package MyApp::Fork;
31
32 use strict;
33 use base qw(App::CLI::Command);
34 use LWP::UserAgent;
35 use HTTP::Request;
36
37 our %LINKS = (cpan => "http://search.cpan.org", perl => "http://www.perl.org", foo => "http://foo.foo/");
38
39 sub options { return ("maxprocs=i" => "maxprocs") };
40
41 sub run {
42
43 my($self, @argv) = @_;
44
45 $self->pm->run_on_start(sub {
46
47 my ($pid, $ident) = @_;
48 print "$ident PID[$pid] start\n";
49 });
50
51 $self->pm->run_on_finish(sub {
52
53 my ($pid, $exit_value, $ident) = @_;
54 print "$ident PID[$pid] finish. exit_value: $exit_value\n";
55 });
56
57 foreach my $key (keys %LINKS) {
58
59 my $pid = $self->pm->start($key) and next;
60 my $ua = LWP::UserAgent->new;
61 my $req = HTTP::Request->new(GET => $LINKS{$key});
62 my $res = $ua->request($req);
63 if ($res->is_success) {
64 printf "%s's status code: %d\n", $key, $res->code;
65 } else {
66 printf "ERROR: $key %s\n", $res->status_line;
67 }
68 $self->pm->finish;
69 }
70
71 $self->pm->wait_all_children;
72 }
73
74 1;
75
76 # myapp
77 #!/usr/bin/perl
78
79 use strict;
80 use MyApp;
81
82 MyApp->dispatch;
83
84 # execute
85 [kurt@localhost ~] ./myapp fork
86 perl PID[3193] start
87 cpan PID[3194] start
88 foo PID[3195] start
89 perl's status code: 200
90 perl PID[3193] finish. exit_value: 0
91 cpan's status code: 200
92 cpan PID[3194] finish. exit_value: 0
93 foo's status code: 200
94 cpan PID[3195] finish. exit_value: 0
95  
96 =head1 DESCRIPTION
97  
98 App::CLI::Plugin::Parallel::ForkManager - Parallel::ForkManager plugin module
99  
100 pm method setting
101  
102 __PACKAGE__->config( parallel_fork_manager => $maxprocs );
103  
104 or if --maxprocs option is defined. it applies.
105  
106 # in MyApp/**.pm
107 sub options {
108 return ( "maxprocs=i" => "maxprocs" ) ;
109 }
110
111 # execute
112 [kurt@localhost ~] ./myapp fork --maxprocs=10
113  
114 =head1 METHOD
115  
116 =head2 pm
117  
118 return Parallel::ForkManager object.
119  
120 =cut
121  
122 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
123 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
124 1 ▶use base qw(Class::Accessor::Grouped);
Subroutine BEGIN: called CC=1 SCAR=0.0
125 1 ▶use Parallel::ForkManager;
Subroutine BEGIN: called CC=1 SCAR=0.0
126  
127 1 __PACKAGE__->mk_group_accessors(inherited => "pm");
128 1 our $VERSION = '1.1';
129  
130 0 ▶sub setup {
Subroutine setup: not called CC=4 SCAR=30.0
Pod undocumented
131  
132 0 my($self, @argv) = @_;
133 0 my $maxprocs;
134 0 ▶ if (exists $self->config->{parallel_fork_manager}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->config->{"parallel_fork_manager"}) 0 0 0
135 0 $maxprocs = $self->config->{parallel_fork_manager};
136 }
137 0 ▶ if (exists $self->{maxprocs} && defined $self->{maxprocs}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"maxprocs"} and defined $self->{"maxprocs"}) 0 0 0
Condition0%0/3
exists $self->{"maxprocs"} and defined $self->{"maxprocs"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->{"maxprocs"} and defined $self->{"maxprocs"}
exists $self->{"maxprocs"} defined $self->{"maxprocs"}
Truth table0%0/3
A and B
A= exists $self->{"maxprocs"}B= defined $self->{"maxprocs"}
ABresult
0X0
100
111
138 0 $maxprocs = $self->{maxprocs};
139 }
140 0 $self->pm(Parallel::ForkManager->new($maxprocs));
141 0 $self->maybe::next::method(@argv);
142 }
143  
144 1 1;
145  
146 __END__
↑ Summary