Clio/Process.pm

filter: Statementstmt 33.3% 21 / 63 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 31.2% 5 / 16 Podpod 100% 7 / 7 totaltotal 38.3% 33 / 86 CC 3 SCAR 9.6
CC
3
Cov
33%
__ANON__
6.0
stop
6.0
__ANON__
2.0
CRAP 2.6
« Clio/Client.pm ↑ Summary Clio/Log/Log4perl.pm »
1  
2 package Clio::Process;
3 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 $Clio::Process::AUTHORITY = 'cpan:AJGB';
5 }
6 {
7 3 $Clio::Process::VERSION = '0.02';
8 }
9 # ABSTRACT: Process wrapper
10  
11 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use AnyEvent::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 3 with 'Clio::Role::HasManager';
18  
19  
20 3 has 'id' => (
21 is => 'ro',
22 required => 1,
23 );
24  
25  
26 3 has 'command' => (
27 is => 'ro',
28 required => 1,
29 );
30  
31 has '_clients' => (
32 is => 'ro',
33 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
34 3 init_arg => undef,
35 );
36  
37 3 has '_handle' => (
38 is => 'rw',
39 init_arg => undef,
40 );
41  
42  
43 0 ▶sub start {
Subroutine start: not called CC=1 SCAR=6.9
Pod documented
44 0 my $self = shift;
45  
46 0 my $log = $self->log;
47  
48 $self->_handle(
49 AnyEvent::Run->new(
50 cmd => [ $self->command ],
51 autocork => 0,
52 no_delay => 1,
53 priority => 19,
54 0 ▶ on_error => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
55 0 my ($handle, $fatal, $msg) = @_;
56 0 my $pid = $self->id;
57 0 $log->fatal("Process $pid error: $msg");
58 0 $self->manager->stop_process( $pid );
59 },
60 0 ▶ on_eof => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
61 0 my ($handle) = @_;
62 0 my $pid = $self->id;
63 0 $log->fatal("Process $pid reached EOF");
64 0 $self->manager->stop_process( $pid );
65 },
66 )
67 0 );
68  
69 0 ▶ my $reader; $reader = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
70 0 my ($handle, $line, $eol) = @_;
71  
72 0 my $pid = $self->id;
73  
74 0 $log->trace("Process $pid reading: '$line'");
75  
76 0 for my $cid ( keys %{ $self->{_clients} } ) {
77 0 $log->trace("Process $pid writing to client $cid");
78 0 $self->_clients->{$cid}->write( $line );
79 }
80  
81 0 $self->_handle->push_read( line => $reader );
82 0 };
83 0 $self->_handle->push_read( line => $reader );
84 }
85  
86  
87 0 ▶sub stop {
Subroutine stop: not called CC=2 SCAR=17.9
Pod documented
88 0 my $self = shift;
89
90 0 $self->log->debug("Stopping process ", $self->id);
91  
92 0 my $cm = $self->manager->c->server->clients_manager;
93  
94 0 $cm->disconnect_client($_) for keys %{ $self->_clients };
95  
96 0 $self->_handle->destroy;
97 0 $self->_handle(undef);
98 }
99  
100  
101 0 ▶sub write {
Subroutine write: not called CC=1 SCAR=6.9
Pod documented
102 0 my $self = shift;
103
104 0 $self->log->trace("Process ", $self->id, " writing '@_'");
105  
106 0 $self->_handle->push_write( @_ );
107 }
108  
109  
110 0 ▶sub add_client {
Subroutine add_client: not called CC=1 SCAR=6.9
Pod documented
111 0 my ($self, $client) = @_;
112  
113 0 $self->_clients->{ $client->id } = $client;
114 }
115  
116  
117 0 ▶sub remove_client {
Subroutine remove_client: not called CC=1 SCAR=6.9
Pod documented
118 0 my ($self, $client_id) = @_;
119  
120 0 delete $self->_clients->{ $client_id };
121 }
122  
123  
124 0 ▶sub clients_count {
Subroutine clients_count: not called CC=1 SCAR=6.9
Pod documented
125 0 my $self = shift;
126  
127 0 return scalar keys %{ $self->_clients };
128 }
129  
130  
131 0 ▶sub is_idle {
Subroutine is_idle: not called CC=1 SCAR=6.9
Pod documented
132 0 my $self = shift;
133  
134 0 return $self->clients_count == 0;
135 }
136  
137 3 1;
138  
139  
140 __END__
« Clio/Client.pm ↑ Summary Clio/Log/Log4perl.pm »