Jifty/Script/Server.pm untested

filter: Statementstmt 0.0% 0 / 90 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 29 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 189 CC 15 SCAR 34.1
CC
15
Cov
0%
_run_server
90.0
_fork
20.0
run
12.0
CRAP 30.2
1 use warnings;
2 use strict;
3  
4 package Jifty::Script::Server;
5 use base qw/Jifty::Script/;
6  
7 # XXX: if test::builder is not used, sometimes connection is not
8 # properly closed, causing the client to wait for the content for a
9 # 302 redirect, see t/06-signup.t, which timeouts after test 24.
10 # If we load this after we load the rest of Jifty, its die handler clobbers ours.
11 # HATE. And even worse, Test::Builder clobbers our global SIG{__DIE__} handler.
12 # So we work around the work around. The real answer is to figure out how Test::Builder
13 # makes our test server not hang and do just that
14 my $x;
15 BEGIN {$x = $SIG{__DIE__}; }
16 use Test::Builder ();
17 BEGIN { $SIG{__DIE__} = $x;}
18  
19  
20 use Plack::Loader;
21 use File::Path ();
22 use Jifty::Util;
23  
24 use constant PIDDIR => Jifty::Util->absolute_path('var');
25 use constant PIDFILE => PIDDIR . '/jifty-server.pid';
26 use constant PARENTPIDFILE => PIDDIR . '/jifty-server-parent.pid';
27  
28 =head1 NAME
29  
30 Jifty::Script::Server - A standalone webserver for your Jifty application
31  
32 =head1 SYNOPSIS
33  
34 jifty server
35 jifty server --port 6666
36 jifty server --stop
37  
38 =head1 DESCRIPTION
39  
40 Starts and stops Jifty's standalone web server.
41  
42 =head2 options
43  
44 =over 8
45  
46 =item --port
47  
48 The port to run the server on. Overrides the port in the config file, if it is
49 set there. The default port is 8888.
50  
51 =item --user USER
52  
53 The user to become after binding to the port. It is advised that you
54 use this when binding to low ports, instead of running as C<root>.
55 This option only works if the server is using a L<Net::Server>
56 subclass.
57  
58 =item --group GROUP
59  
60 The group to become after binding to the port. Like C<--user>, this
61 option only works if the server is using a L<Net::Server> subclass.
62  
63 =item --host HOSTNAME
64  
65 The host to bind to. This option only works if the server is using a
66 L<Net::Server> subclass.
67  
68 =item --stop
69  
70 Stops the server, if it is running. This is accomplished by reading
71 the PID from C<var/jifty-server.pid>
72  
73 =item --restart
74  
75 restart the server, if it is running. This is accomplished by
76 killing the child pid, and let parent pid create a new one
77  
78 =item --sigready SIGNAL
79  
80 Sets the signal number that should be sent to the server's parent
81 process when the server is ready to accept connections.
82  
83 =item --quiet
84  
85 Reduces the amount of debug output sent by the server
86  
87 =item --dbiprof
88  
89 Turns on DBI profiling; see L<DBI::ProfileDumper>.
90  
91 =item B<--help>
92  
93 Print a brief help message and exits.
94  
95 =item B<--man>
96  
97 Prints the manual page and exits.
98  
99 =back
100  
101 =cut
102  
103 sub options {
104 my $self = shift;
105 return (
106 $self->SUPER::options,
107 'p|port=s' => 'port',
108 'stop' => 'stop',
109 'restart' => 'restart',
110 'sigready=s' => 'sigready',
111 'quiet' => 'quiet',
112 'dbiprof' => 'dbiprof',
113 'host=s' => 'host',
114 'u|user=s' => 'user',
115 'g|group=s' => 'group',
116 's|server=s' => 'server',
117 )
118 }
119  
120 =head1 DESCRIPTION
121  
122 When you're getting started with Jifty, this is the server you
123 want. It's lightweight and easy to work with.
124  
125 =head1 METHODS
126  
127 =head2 run
128  
129 C<run> takes no arguments, but starts up a Jifty server process for
130 you.
131  
132 =cut
133  
134 sub run {
135 my $self = shift;
136 if ( $self->{stop} ) {
137 $self->_stop;
138 }
139 elsif ( $self->{restart} ) {
140 $self->_stop_child;
141 }
142 else {
143 $self->_fork;
144 }
145 }
146  
147 sub _fork {
148 my $self = shift;
149 my $pid = fork;
150  
151 die 'fork failed' unless defined $pid;
152 if ($pid) {
153 mkdir PIDDIR or die "Can't create directory @{[PIDDIR]}: $!"
154 if !-d PIDDIR;
155  
156 open my $fh, '>', PARENTPIDFILE or die "Can't open @{[PARENTPIDFILE]} for writing: $!";
157 print $fh $$;
158 close $fh;
159  
160 wait;
161 sleep 1; # to make cpu happy
162 $self->_fork;
163 }
164 else {
165 $self->_run_server;
166 }
167 }
168  
169 sub _run_server {
170 my $self = shift;
171 $self->print_help;
172 Jifty->new();
173  
174 # Purge stale mason cache data
175 my $data_dir = Jifty->config->framework('Web')->{'DataDir'};
176 File::Path::rmtree( [ "$data_dir/cache", "$data_dir/obj" ] )
177 if Jifty->handler->view('Jifty::View::Mason::Handler')
178 and -d $data_dir;
179  
180 $SIG{TERM} = sub {exit};
181 mkdir PIDDIR
182 or die "Can't create directory @{[PIDDIR]}: $!"
183 if !-d PIDDIR;
184 open my $fh, '>', PIDFILE
185 or die "Can't open @{[PIDFILE]} for writing: $!";
186 print $fh $$;
187 close $fh;
188  
189 Jifty->handle->dbh->{Profile} = '6/DBI::ProfileDumper'
190 if $self->{dbiprof};
191  
192 $ENV{JIFTY_SERVER_SIGREADY} ||= $self->{sigready}
193 if $self->{sigready};
194  
195 Jifty->config->framework('Web')->{'Port'} = $self->{port} if $self->{port};
196 my $port = Jifty->config->framework('Web')->{'Port'} || 8888;
197  
198 my %args = (
199 port => $port,
200 server_ready => sub {
201 Jifty->log->info( 'You can connect to your server at http://'
202 . ( $self->{host} || 'localhost' )
203 . ":$port/" );
204 },
205 );
206  
207 $args{$_} = $self->{$_} for grep defined $self->{$_}, qw/host user group/;
208  
209 my $server_class = $self->{server} || 'Standalone';
210 if (Jifty->find_plugin('Jifty::Plugin::PubSub') and $server_class !~ /^(Twiggy|Feersum)$/i) {
211 if (Jifty::Util->try_to_require("Twiggy")) {
212 $server_class = "Twiggy";
213 } elsif (Jifty::Util->try_to_require("Feersum")) {
214 $server_class = "Feersum";
215 } else {
216 die "An event-based PSGI server (i.e. Twiggy, Feersum) is needed to run PubSub or RPC";
217 }
218 }
219 $Jifty::SERVER = Plack::Loader->load($server_class, %args);
220 $Jifty::SERVER->run(Jifty->handler->psgi_app);
221 }
222  
223 sub _stop {
224 my $self = shift;
225 mkdir PIDDIR or die "Can't create directory @{[PIDDIR]}: $!"
226 if !-d PIDDIR;
227 open my $fh, '<', PARENTPIDFILE or die "Can't open @{[PARENTPIDFILE]} for reading: $!";
228 my $pid = <$fh>;
229 kill 'TERM' => $pid;
230  
231 $self->_stop_child;
232 return;
233 }
234  
235 sub _stop_child {
236 open my $fh, '<', PIDFILE or die "Can't open @{[PIDFILE]} for reading: $!";
237 my $pid = <$fh>;
238 kill 'TERM' => $pid;
239 }
240  
241 1;