Clio/ProcessManager.pm

filter: Statementstmt 26.1% 23 / 88 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 35.2% 6 / 17 Podpod 100% 5 / 5 totaltotal 23.7% 34 / 143 CC 17 SCAR 36.4
CC
17
Cov
20%
get_first_available
90.0
_idle_processes_maintenance
56.0
_start_num_processes
12.0
CRAP 38.2
↑ Summary Clio/Config.pm »
1  
2 package Clio::ProcessManager;
3 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 $Clio::ProcessManager::AUTHORITY = 'cpan:AJGB';
5 }
6 {
7 2 $Clio::ProcessManager::VERSION = '0.02';
8 }
9 # ABSTRACT: Process manager
10  
11 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Clio::Process;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2 with 'Clio::Role::HasContext';
19 2 with 'Clio::Role::UUIDMaker';
20  
21  
22 has 'processes' => (
23 is => 'ro',
24 0 ▶ default => sub { +{} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
25 2 );
26  
27 has '_check_idle_loop' => (
28 is => 'ro',
29 init_arg => undef,
30 lazy => 1,
31 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
32 0 my $self = shift;
33 AnyEvent->timer(
34 after => 2,
35 interval => 1,
36 0 ▶ cb => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
37 0 $self->_idle_processes_maintenance();
38 },
39 0 );
40 },
41 2 );
42  
43  
44 0 ▶sub start {
Subroutine start: not called CC=1 SCAR=6.9
Pod documented
45 0 my $self = shift;
46  
47 0 my $config = $self->c->config->CommandConfig;
48 0 my $log = $self->c->log;
49  
50 $self->_start_num_processes(
51 $config->{StartCommands},
52 $config->{Exec},
53 0 );
54 0 $self->_check_idle_loop;
55 }
56  
57 0 ▶sub _start_num_processes {
Subroutine _start_num_processes: not called CC=3 SCAR=24.8
58 0 my ($self, $number, $cmd) = @_;
59  
60 0 ▶ return unless $number >= 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $number >= 1 0 0 0
61  
62 0 my $cv = AnyEvent->condvar;
63 0 $cv->begin;
64 0 for ( 1 .. $number ) {
65 0 $cv->begin;
66 0 my $s; $s = AnyEvent->timer(
67 after => 0,
68 0 ▶ cb => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
69 0 undef $s;
70 0 $self->create_process( $cmd )->start;
71 0 $cv->end;
72 }
73 0 );
74 }
75 0 $cv->end;
76 }
77  
78  
79 0 ▶sub create_process {
Subroutine create_process: not called CC=1 SCAR=6.9
Pod documented
80 0 my ($self, $cmd) = @_;
81  
82 0 my $uuid = $self->create_uuid;
83 0 $self->c->log->debug("Creating process $uuid");
84  
85 0 return $self->processes->{ $uuid } = Clio::Process->new(
86 manager => $self,
87 id => $uuid,
88 command => $cmd,
89 );
90 }
91  
92  
93 0 ▶sub get_first_available {
Subroutine get_first_available: not called CC=9 SCAR=45.0
Pod documented
94 0 my ($self, %args) = @_;
95  
96 0 my $config = $self->c->config->CommandConfig;
97 0 my $log = $self->c->log;
98  
99 0 ▶ if ( my $client_id = $args{client_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $client_id = $args{"client_id"}) 0 0 0
100 0 my $clients_manager = $self->c->server->clients_manager;
101 0 ▶ if ( my $client = $clients_manager->clients->{ $client_id } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $client = $clients_manager->clients->{$client_id}) 0 0 0
102 0 ▶ if ( my $proc = $client->_process ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $proc = $client->_process) 0 0 0
103 0 ▶ if ( exists $proc->_clients->{ $client_id } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $proc->_clients->{$client_id}) 0 0 0
104 0 $log->trace("Restored connection from client $client_id");
105 0 return $proc;
106 }
107 }
108 }
109 }
110  
111 0 ▶ while ( my ($uuid, $proc) = each %{ $self->processes } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$self->processes;}) 0 0 0
112 0 ▶ if ( ! $config->{MaxClientsPerCommand} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $config->{'MaxClientsPerCommand'}) { } 0 0 0
elsif ($proc->clients_count < $config->{'MaxClientsPerCommand'}) { } 0 0 0
113 0 return $proc;
114 }
115 elsif ( $proc->clients_count < $config->{MaxClientsPerCommand} ) {
116 0 return $proc;
117 }
118 }
119 0 ▶ if ( $self->total_count < $config->{MaxCommands} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->total_count < $config->{"MaxCommands"}) 0 0 0
120 0 my $proc = $self->create_process( $config->{Exec} );
121 0 $proc->start;
122 0 return $proc;
123 }
124  
125 0 return;
126 }
127  
128  
129 0 ▶sub total_count {
Subroutine total_count: not called CC=1 SCAR=6.9
Pod documented
130 0 my $self = shift;
131  
132 0 return scalar keys %{ $self->processes };
133 }
134  
135 0 ▶sub _idle_processes_maintenance {
Subroutine _idle_processes_maintenance: not called CC=7 SCAR=40.3
136 0 my $self = shift;
137  
138 0 my $config = $self->c->config->CommandConfig;
139 0 my $log = $self->c->log;
140  
141 0 ▶ my $min_idle = $config->{MinSpareCommands} || 0;
Condition0%0/2
$config->{'MinSpareCommands'} || 0
l!l
00
MC/DC0%0/1
$config->{'MinSpareCommands'} || 0
$config->{'MinSpareCommands'}
Truth table0%0/2
A
A= $config->{'MinSpareCommands'}
Aresult
11
00
142 0 ▶ my $max_idle = $config->{MaxSpareCommands} || 0;
Condition0%0/2
$config->{'MaxSpareCommands'} || 0
l!l
00
MC/DC0%0/1
$config->{'MaxSpareCommands'} || 0
$config->{'MaxSpareCommands'}
Truth table0%0/2
A
A= $config->{'MaxSpareCommands'}
Aresult
11
00
143  
144 0 my $cur_idle = 0;
145  
146 0 for my $uuid ( keys %{ $self->processes } ) {
147 0 my $proc = $self->processes->{$uuid};
148  
149 0 ▶ if ( $proc->is_idle && ++$cur_idle > $max_idle ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proc->is_idle and ++$cur_idle > $max_idle) 0 0 0
Condition0%0/3
$proc->is_idle and ++$cur_idle > $max_idle
!ll&&!rl&&r
000
MC/DC0%0/2
$proc->is_idle and ++$cur_idle > $max_idle
$proc->is_idle ++$cur_idle > $max_idle
Truth table0%0/3
A and B
A= $proc->is_idleB= ++$cur_idle > $max_idle
ABresult
0X0
100
111
150 0 $self->stop_process($proc->id);
151 }
152 }
153 0 ▶ $log->debug("Stopped ", ($cur_idle - $max_idle)," idle processes")
Branch: 1 branch
BranchTrueFalseTotal
if $cur_idle > $max_idle 0 0 0
154 if $cur_idle > $max_idle;
155  
156 $self->_start_num_processes(
157 $min_idle - $cur_idle,
158 $config->{Exec},
159 0 );
160 }
161  
162  
163 0 ▶sub stop_process {
Subroutine stop_process: not called CC=1 SCAR=6.9
Pod documented
164 0 my ($self, $process_id) = @_;
165  
166 0 $self->c->log->debug("Stopping process $process_id");
167  
168 0 $self->processes->{ $process_id }->stop;
169  
170 0 delete $self->processes->{ $process_id };
171  
172 }
173  
174 2 1;
175  
176  
177 __END__
↑ Summary Clio/Config.pm »