FCGI/Engine/Core.pm

filter: Statementstmt 90.1% 73 / 81 Branchbran 61.5% 16 / 26 Conditioncond 46.6% 7 / 15 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 88.8% 16 / 18 Podpod 0.0% 0 / 9 totaltotal 71.7% 112 / 156 CC 18 SCAR 19.6
CC
18
Cov
79%
prepare_environment
21.9
run
9.4
BUILD
3.0
CRAP 7.1
« FCGI/Engine/Manager/Server/Plackup.pm ↑ Summary FCGI/Engine/PSGI.pm »
1 package FCGI::Engine::Core;
2 9 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 9 ▶use Class::Load ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use FCGI;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use MooseX::Daemonize::Pid::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use FCGI::Engine::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use FCGI::Engine::ProcManager;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 9 ▶use constant DEBUG => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 9 our $VERSION = '0.22';
13 9 our $AUTHORITY = 'cpan:STEVAN';
14  
15 9 with 'MooseX::Getopt',
16 'MooseX::Daemonize::Core';
17  
18 9 has 'listen' => (
19 metaclass => 'Getopt',
20 is => 'ro',
21 isa => 'FCGI::Engine::Listener',
22 coerce => 1,
23 cmd_aliases => 'l',
24 predicate => 'is_listening',
25 );
26  
27 has 'nproc' => (
28 metaclass => 'Getopt',
29 is => 'ro',
30 isa => 'Int',
31 10 ▶ default => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
32 9 cmd_aliases => 'n',
33 );
34  
35 9 has 'pidfile' => (
36 metaclass => 'Getopt',
37 is => 'ro',
38 isa => 'MooseX::Daemonize::Pid::File',
39 coerce => 1,
40 cmd_aliases => 'p',
41 predicate => 'has_pidfile',
42 );
43  
44 9 has 'detach' => (
45 metaclass => 'Getopt',
46 is => 'ro',
47 isa => 'Bool',
48 cmd_flag => 'daemon',
49 cmd_aliases => 'd',
50 predicate => 'should_detach',
51 );
52  
53 has 'manager' => (
54 metaclass => 'Getopt',
55 is => 'ro',
56 isa => 'Str',
57 9 ▶ default => sub { 'FCGI::Engine::ProcManager' },
Subroutine __ANON__: called CC=1 SCAR=0.0
58 9 cmd_aliases => 'M',
59 );
60  
61 9 has 'use_manager' => (
62 metaclass => 'Getopt',
63 is => 'ro',
64 isa => 'Bool',
65 default => 0,
66 );
67  
68 # options to specify in your script
69  
70 9 has 'pre_fork_init' => (
71 metaclass => 'NoGetopt',
72 is => 'ro',
73 isa => 'CodeRef',
74 predicate => 'has_pre_fork_init',
75 );
76  
77 ## methods ...
78  
79 11 ▶sub BUILD {
Subroutine BUILD: called CC=3 SCAR=11.0
Pod undocumented
80 11 my $self = shift;
81 11 ▶ ($self->has_pidfile)
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_listening 6 5 11
Condition80%4/5
$self->is_listening && ($self->has_pidfile || confess('You must specify a pidfile if you are listening'))
!ll&&!rl&&r
560
$self->has_pidfile or confess("You must specify a pidfile if you are listening")
l!l
51
MC/DC0%0/2
$self->is_listening && ($self->has_pidfile || confess('You must specify a pidfile if you are listening'))
$self->is_listening $self->has_pidfile
Truth table66%2/3
A && B
A= $self->is_listeningB= $self->has_pidfile
ABresult
0X0
100
111
82 || confess "You must specify a pidfile if you are listening"
83 if $self->is_listening;
84 }
85  
86 7 ▶sub initialize {
Subroutine initialize: called CC=2 SCAR=7.1
Pod undocumented
87 7 my ( $self, %addtional_options ) = @_;
88  
89 7 ▶ $self->pre_fork_init->(%addtional_options)
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_pre_fork_init 0 7 7
90 if $self->has_pre_fork_init;
91  
92 7 inner();
93 }
94  
95 7 ▶sub create_socket {
Subroutine create_socket: called CC=2 SCAR=6.9
Pod undocumented
96 7 my $self = shift;
97 7 my $socket = 0;
98 7 ▶ if ($self->is_listening) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_listening) 3 4 7
99 3 my $old_umask = umask;
100 3 umask(0);
101 3 $socket = FCGI::OpenSocket($self->listen, 100);
102 3 umask($old_umask);
103 }
104 7 $socket;
105 }
106  
107 0 ▶sub create_environment { +{} }
Subroutine create_environment: not called CC=1 SCAR=6.9
Pod undocumented
108  
109 7 ▶sub create_request {
Subroutine create_request: called CC=1 SCAR=0.0
Pod undocumented
110 7 my ( $self, $socket, $env ) = @_;
111 7 return FCGI::Request(
112 \*STDIN,
113 \*STDOUT,
114 \*STDERR,
115 $env,
116 $socket,
117 &FCGI::FAIL_ACCEPT_ON_INTR
118 );
119 }
120  
121 3 ▶sub create_proc_manager {
Subroutine create_proc_manager: called CC=1 SCAR=0.0
Pod undocumented
122 3 my ( $self, %addtional_options ) = @_;
123  
124 # make sure any subclasses are loaded ...
125 3 Class::Load::load_class( $self->manager );
126  
127 3 return $self->manager->new({
128 n_processes => $self->nproc,
129 pidfile => $self->pidfile,
130 %addtional_options
131 });
132 }
133  
134 3 ▶sub prepare_environment {
Subroutine prepare_environment: called CC=6 SCAR=30.9
Pod undocumented
135 3 my ($self, $_env) = @_;
136  
137 3 my $env = inner();
138  
139 # Cargo-culted from Catalyst::Engine::FastCGI
140 # and Plack::Server::FCGI, thanks guys :)
141 3 ▶ if ( $env->{SERVER_SOFTWARE} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($env->{"SERVER_SOFTWARE"}) 0 3 3
142 0 ▶ if ( $env->{SERVER_SOFTWARE} =~ /lighttpd/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($env->{'SERVER_SOFTWARE'} =~ /lighttpd/) { } 0 0 0
elsif ($env->{'SERVER_SOFTWARE'} =~ /^nginx/) { } 0 0 0
143 0 ▶ $env->{PATH_INFO} ||= delete $env->{SCRIPT_NAME};
Condition0%0/3
$env->{"PATH_INFO"} ||= delete $env->{"SCRIPT_NAME"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$env->{"PATH_INFO"} ||= delete $env->{"SCRIPT_NAME"}
$env->{"PATH_INFO"} delete $env->{"SCRIPT_NAME"}
Truth table0%0/3
A ||= B
A= $env->{"PATH_INFO"}B= delete $env->{"SCRIPT_NAME"}
ABresult
1X1
011
000
144 0 ▶ $env->{SCRIPT_NAME} ||= '';
Condition0%0/2
$env->{"SCRIPT_NAME"} ||= ""
l!l
00
MC/DC0%0/1
$env->{"SCRIPT_NAME"} ||= ""
$env->{"SCRIPT_NAME"}
Truth table0%0/2
A
A= $env->{"SCRIPT_NAME"}
Aresult
11
00
145 0 $env->{SERVER_NAME} =~ s/:\d+$//; # cut off port number
146 }
147 elsif ( $env->{SERVER_SOFTWARE} =~ /^nginx/ ) {
148 0 my $script_name = $env->{SCRIPT_NAME};
149 0 $env->{PATH_INFO} =~ s/^$script_name//g;
150 }
151 }
152  
153 3 $env;
154 }
155  
156 0 ▶sub handle_request { confess __PACKAGE__ . " is abstract, override handle_request" }
Subroutine handle_request: not called CC=1 SCAR=6.9
Pod undocumented
157  
158 7 ▶sub run {
Subroutine run: called CC=9 SCAR=22.4
Pod undocumented
159 7 my ($self, %addtional_options) = @_;
160  
161 7 $self->initialize( %addtional_options );
162  
163 7 my $socket = $self->create_socket;
164 7 my $env = $self->create_environment;
165 7 my $request = $self->create_request( $socket, $env );
166  
167 7 my $proc_manager;
168  
169 7 ▶ if ($self->is_listening) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->is_listening) { } 3 4 7
elsif ($self->use_manager) { } 1 3 4
170  
171 3 ▶ $self->daemon_fork && return if $self->detach;
Branch: 1 branch
BranchTrueFalseTotal
if $self->detach 3 0 3
Condition60%3/5
$self->detach && ($self->daemon_fork && (return))
!ll&&!rl&&r
030
$self->daemon_fork and return
!ll
21
MC/DC0%0/2
$self->detach && ($self->daemon_fork && (return))
$self->detach $self->daemon_fork
Truth table33%1/3
A && B
A= $self->detachB= $self->daemon_fork
ABresult
0X0
100
111
172  
173 2 $proc_manager = $self->create_proc_manager( %addtional_options );
174  
175 2 ▶ $self->daemon_detach(
Branch: 1 branch
BranchTrueFalseTotal
if $self->detach 2 0 2
176 # Not sure we need this ...
177 no_double_fork => 1,
178 # we definetely need this ...
179 dont_close_all_files => 1,
180 ) if $self->detach;
181  
182 2 $proc_manager->manage;
183 }
184  
185 # We do not listen but we do want more than one processes being forked and
186 # want to take the benefit of running the process manager as well. This
187 # makes sense if the FastCGI script is started directly via Apache.
188 elsif ( $self->use_manager ) {
189 1 $proc_manager = $self->create_proc_manager( %addtional_options );
190 1 $proc_manager->manage;
191 }
192  
193 5 ▶ while ($request->Accept() >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($request->Accept >= 0) 3 5 8
194  
195 3 ▶ $proc_manager && $proc_manager->pre_dispatch;
Branch: 1 branch
BranchTrueFalseTotal
$proc_manager and $proc_manager->pre_dispatch 0 3 3
196  
197 3 $self->handle_request(
198 $self->prepare_environment( $env ),
199 $request
200 );
201  
202 3 ▶ $proc_manager && $proc_manager->post_dispatch;
Branch: 1 branch
BranchTrueFalseTotal
$proc_manager and $proc_manager->post_dispatch 0 3 3
203 }
204 }
205  
206 9 1;
207  
208 __END__
« FCGI/Engine/Manager/Server/Plackup.pm ↑ Summary FCGI/Engine/PSGI.pm »