| 1 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 2 | # GX Framework (c) 2009-2011 Jörg A. Uzarek <uzarek@runlevelnull.de> | ||
| 3 | # File: GX/Script/Server/FCGI.pm | ||
| 4 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 5 | |||
| 6 | package GX::Script::Server::FCGI; | ||
| 7 | |||
| 8 | use FCGI (); | ||
| 9 | |||
| 10 | |||
| 11 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 12 | # Class setup | ||
| 13 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 14 | |||
| 15 | use GX::Class; | ||
| 16 | |||
| 17 | extends 'GX::Script'; | ||
| 18 | |||
| 19 | build; | ||
| 20 | |||
| 21 | |||
| 22 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 23 | # Public methods | ||
| 24 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 25 | |||
| 26 | sub run { | ||
| 27 | |||
| 28 | my $self = ref $_[0] ? shift : shift->new; | ||
| 29 | |||
| 30 | $self->_process_run_args( @_ ); | ||
| 31 | $self->_process_argv; | ||
| 32 | $self->_set_inc; | ||
| 33 | |||
| 34 | $self->load_application; | ||
| 35 | |||
| 36 | my $application = $self->application_class; | ||
| 37 | my $options = $self->options; | ||
| 38 | |||
| 39 | my %env; | ||
| 40 | my $request; | ||
| 41 | |||
| 42 | if ( $options->{'listen'} ) { | ||
| 43 | |||
| 44 | my $old_umask = umask; | ||
| 45 | |||
| 46 | umask 0; | ||
| 47 | |||
| 48 | my $socket = FCGI::OpenSocket( $options->{'listen'}, $options->{'queue-size'} ); | ||
| 49 | |||
| 50 | umask $old_umask; | ||
| 51 | |||
| 52 | $socket or die "Cannot open FastCGI socket ($!)\n"; | ||
| 53 | |||
| 54 | $request = FCGI::Request( | ||
| 55 | \*STDIN, | ||
| 56 | \*STDOUT, | ||
| 57 | \*STDERR, | ||
| 58 | \%env, | ||
| 59 | $socket, | ||
| 60 | FCGI::FAIL_ACCEPT_ON_INTR() | ||
| 61 | ); | ||
| 62 | |||
| 63 | $self->print_message( "Starting to listen on " . $options->{'listen'} . " for requests" ); | ||
| 64 | |||
| 65 | } | ||
| 66 | else { | ||
| 67 | |||
| 68 | $request = FCGI::Request( | ||
| 69 | \*STDIN, | ||
| 70 | \*STDOUT, | ||
| 71 | \*STDERR, | ||
| 72 | \%env | ||
| 73 | ); | ||
| 74 | |||
| 75 | } | ||
| 76 | |||
| 77 | my $daemonize = $options->{'daemonize'} && $options->{'listen'}; | ||
| 78 | |||
| 79 | if ( $daemonize ) { | ||
| 80 | |||
| 81 | require POSIX; | ||
| 82 | |||
| 83 | fork && exit; | ||
| 84 | |||
| 85 | } | ||
| 86 | |||
| 87 | my $manager; | ||
| 88 | |||
| 89 | if ( $options->{'processes'} ) { | ||
| 90 | |||
| 91 | $options->{'listen'} or die "No socket specified\n"; | ||
| 92 | |||
| 93 | $self->print_message( "Spawning " . $options->{'processes'} . " application processes" ); | ||
| 94 | |||
| 95 | require FCGI::ProcManager; | ||
| 96 | |||
| 97 | FCGI::ProcManager->import; | ||
| 98 | |||
| 99 | $manager = FCGI::ProcManager->new( | ||
| 100 | { | ||
| 101 | 'n_processes' => $options->{'processes'}, | ||
| 102 | 'pid_fname' => $options->{'pidfile'} | ||
| 103 | } | ||
| 104 | ); | ||
| 105 | |||
| 106 | } | ||
| 107 | |||
| 108 | if ( $daemonize ) { | ||
| 109 | |||
| 110 | $self->print_message( "Detaching daemon (pid: $$)" ); | ||
| 111 | |||
| 112 | POSIX::setsid() or die "Cannot detach from controlling terminal\n"; | ||
| 113 | |||
| 114 | chdir '/'; | ||
| 115 | |||
| 116 | open( STDIN, '/dev/null' ); | ||
| 117 | open( STDOUT, '> /dev/null' ); | ||
| 118 | open( STDERR, '> /dev/null' ); | ||
| 119 | |||
| 120 | } | ||
| 121 | |||
| 122 | if ( $manager ) { | ||
| 123 | |||
| 124 | $manager->pm_manage; | ||
| 125 | |||
| 126 | while ( $request->Accept >= 0 ) { | ||
| 127 | $manager->pm_pre_dispatch; | ||
| 128 | $application->handler( \%env ); | ||
| 129 | $manager->pm_post_dispatch; | ||
| 130 | } | ||
| 131 | |||
| 132 | } | ||
| 133 | else { | ||
| 134 | |||
| 135 | while ( $request->Accept >= 0 ) { | ||
| 136 | $application->handler( \%env ); | ||
| 137 | } | ||
| 138 | |||
| 139 | } | ||
| 140 | |||
| 141 | $self->exit; | ||
| 142 | |||
| 143 | } | ||
| 144 | |||
| 145 | sub show_help { | ||
| 146 | |||
| 147 | my $self = shift; | ||
| 148 | |||
| 149 | my $script_name = $self->script_name; | ||
| 150 | |||
| 151 | ( my $socket = $self->application_class ) =~ s/::/_/g; | ||
| 152 | $socket = File::Spec->rel2abs( lc( $socket ) . '.socket', File::Spec->tmpdir ); | ||
| 153 | |||
| 154 | $self->print_message( <<EOT ); | ||
| 155 | NAME | ||
| 156 | |||
| 157 | $script_name - Start a FastCGI Application Server | ||
| 158 | |||
| 159 | USAGE | ||
| 160 | |||
| 161 | $script_name [OPTIONS] | ||
| 162 | |||
| 163 | OPTIONS | ||
| 164 | |||
| 165 | -d, --daemonize | ||
| 166 | Daemonize the application server. | ||
| 167 | |||
| 168 | -l <socket>, --listen <socket> | ||
| 169 | Listen on <socket> for requests. The given value is passed to FCGI::OpenSocket(). | ||
| 170 | |||
| 171 | --pidfile <file> | ||
| 172 | Use <file> as the pid file for FCGI::ProcManager. | ||
| 173 | |||
| 174 | -p <n>, --processes <n> | ||
| 175 | Spawn <n> application server processes using FCGI::ProcManager. | ||
| 176 | |||
| 177 | --queue-size <n> | ||
| 178 | The queue size for FCGI::OpenSocket(). | ||
| 179 | |||
| 180 | MORE OPTIONS | ||
| 181 | |||
| 182 | -h, --help | ||
| 183 | Print this help and exit. | ||
| 184 | |||
| 185 | -v, --version | ||
| 186 | Print the version information for this script and exit. | ||
| 187 | |||
| 188 | --copyright | ||
| 189 | Print the copyright notice for this script and exit. | ||
| 190 | |||
| 191 | EXAMPLES | ||
| 192 | |||
| 193 | $script_name --listen $socket | ||
| 194 | $script_name --listen $socket --processes 5 | ||
| 195 | $script_name --listen $socket --processes 5 --daemonize | ||
| 196 | EOT | ||
| 197 | |||
| 198 | } | ||
| 199 | |||
| 200 | |||
| 201 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 202 | # Private methods | ||
| 203 | # ---------------------------------------------------------------------------------------------------------------------- | ||
| 204 | |||
| 205 | sub _argv_options { | ||
| 206 | |||
| 207 | my $self = shift; | ||
| 208 | |||
| 209 | return ( | ||
| 210 | $self->SUPER::_argv_options, | ||
| 211 | 'daemonize|d', | ||
| 212 | 'listen|l=s', | ||
| 213 | 'pidfile=s', | ||
| 214 | 'processes|p=i', | ||
| 215 | 'queue-size|q=i' | ||
| 216 | ); | ||
| 217 | |||
| 218 | } | ||
| 219 | |||
| 220 | sub _initialize_options { | ||
| 221 | |||
| 222 | my $self = shift; | ||
| 223 | |||
| 224 | my $options = $self->SUPER::_initialize_options; | ||
| 225 | |||
| 226 | $options->{'queue-size'} = 100; | ||
| 227 | |||
| 228 | return $options; | ||
| 229 | |||
| 230 | } | ||
| 231 | |||
| 232 | sub _process_run_args { | ||
| 233 | |||
| 234 | my $self = shift; | ||
| 235 | my %args = @_; | ||
| 236 | |||
| 237 | if ( defined $args{'application'} ) { | ||
| 238 | $self->application_class( $args{'application'} ); | ||
| 239 | } | ||
| 240 | elsif ( ! $self->application_class ) { | ||
| 241 | die "No application specified\n"; | ||
| 242 | } | ||
| 243 | |||
| 244 | return; | ||
| 245 | |||
| 246 | } | ||
| 247 | |||
| 248 | |||
| 249 | 1; | ||
| 250 | |||
| 251 | __END__ |