Commit | Line | Data |
0ba6e8aa |
1 | package Catalyst::Script::FastCGI; |
cc999ce2 |
2 | use Moose; |
6640e166 |
3 | use Data::OptList; |
2adf69c3 |
4 | use namespace::autoclean; |
cc999ce2 |
5 | |
c821df21 |
6 | sub _plack_engine_name { 'FCGI' } |
7 | |
d3082fac |
8 | with 'Catalyst::ScriptRole'; |
f4dc8d2f |
9 | |
10 | has listen => ( |
ad8b4c91 |
11 | traits => [qw(Getopt)], |
12 | cmd_aliases => 'l', |
8c848c33 |
13 | isa => 'Str', |
ad8b4c91 |
14 | is => 'ro', |
d3082fac |
15 | documentation => 'Specify a listening port/socket', |
f4dc8d2f |
16 | ); |
17 | |
18 | has pidfile => ( |
ad8b4c91 |
19 | traits => [qw(Getopt)], |
ad08ab75 |
20 | cmd_aliases => [qw/pid p/], |
8c848c33 |
21 | isa => 'Str', |
ad8b4c91 |
22 | is => 'ro', |
d3082fac |
23 | documentation => 'Specify a pidfile', |
f4dc8d2f |
24 | ); |
25 | |
ab7eb5a9 |
26 | has daemon => ( |
ad8b4c91 |
27 | traits => [qw(Getopt)], |
8c848c33 |
28 | isa => 'Bool', |
ad8b4c91 |
29 | is => 'ro', |
7388bcae |
30 | cmd_aliases => [qw/d detach/], # Eww, detach is here as we fucked it up.. Deliberately not documented |
53c6ec79 |
31 | documentation => 'Daemonize (go into the background)', |
f4dc8d2f |
32 | ); |
33 | |
ab7eb5a9 |
34 | has manager => ( |
ad8b4c91 |
35 | traits => [qw(Getopt)], |
8c848c33 |
36 | isa => 'Str', |
ad8b4c91 |
37 | is => 'ro', |
38 | cmd_aliases => 'M', |
53c6ec79 |
39 | documentation => 'Use a different FastCGI process manager class', |
f4dc8d2f |
40 | ); |
41 | |
53c6ec79 |
42 | has keeperr => ( |
ad8b4c91 |
43 | traits => [qw(Getopt)], |
44 | cmd_aliases => 'e', |
8c848c33 |
45 | isa => 'Bool', |
ad8b4c91 |
46 | is => 'ro', |
d3082fac |
47 | documentation => 'Log STDERR', |
f4dc8d2f |
48 | ); |
49 | |
50 | has nproc => ( |
ad8b4c91 |
51 | traits => [qw(Getopt)], |
52 | cmd_aliases => 'n', |
8c848c33 |
53 | isa => 'Int', |
ad8b4c91 |
54 | is => 'ro', |
53c6ec79 |
55 | documentation => 'Specify a number of child processes', |
f4dc8d2f |
56 | ); |
57 | |
8865ee12 |
58 | has proc_title => ( |
9c74923d |
59 | traits => [qw(Getopt)], |
8c848c33 |
60 | isa => 'Str', |
9c74923d |
61 | is => 'ro', |
62 | lazy => 1, |
63 | builder => '_build_proc_title', |
64 | documentation => 'Set the process title', |
65 | ); |
66 | |
67 | sub _build_proc_title { |
68 | my ($self) = @_; |
69 | return sprintf 'perl-fcgi-pm [%s]', $self->application_name; |
70 | } |
71 | |
72 | sub BUILD { |
73 | my ($self) = @_; |
182f8b1e |
74 | $self->proc_title; |
9c74923d |
75 | } |
76 | |
f06d7696 |
77 | # Munge the 'listen' arg so that Plack::Handler::FCGI will accept it. |
78 | sub _listen { |
79 | my ($self) = @_; |
80 | |
81 | if (defined (my $listen = $self->listen)) { |
82 | return [ $listen ]; |
83 | } else { |
84 | return undef; |
85 | } |
86 | } |
87 | |
36a53c3a |
88 | sub _plack_loader_args { |
89 | my ($self) = shift; |
f06d7696 |
90 | |
91 | my $opts = Data::OptList::mkopt([ |
fd480b06 |
92 | qw/manager nproc proc_title/, |
93 | pid => [ 'pidfile' ], |
94 | daemonize => [ 'daemon' ], |
f06d7696 |
95 | keep_stderr => [ 'keeperr' ], |
96 | listen => [ '_listen' ], |
97 | ]); |
98 | |
99 | my %args = map { $_->[0] => $self->${ \($_->[1] ? $_->[1]->[0] : $_->[0]) } } @$opts; |
100 | |
101 | # Plack::Handler::FCGI thinks manager => undef means "use no manager". |
102 | delete $args{'manager'} unless defined $args{'manager'}; |
103 | |
104 | return %args; |
36a53c3a |
105 | } |
106 | |
aee7cdcc |
107 | around _application_args => sub { |
108 | my ($orig, $self) = @_; |
d3082fac |
109 | return ( |
cc999ce2 |
110 | $self->listen, |
57dc50b0 |
111 | { |
aee7cdcc |
112 | %{ $self->$orig }, |
9c74923d |
113 | nproc => $self->nproc, |
114 | pidfile => $self->pidfile, |
115 | manager => $self->manager, |
116 | detach => $self->daemon, |
53c6ec79 |
117 | keep_stderr => $self->keeperr, |
182f8b1e |
118 | proc_title => $self->proc_title, |
57dc50b0 |
119 | } |
cc999ce2 |
120 | ); |
aee7cdcc |
121 | }; |
cc999ce2 |
122 | |
73e4f0f1 |
123 | __PACKAGE__->meta->make_immutable; |
f4de8c99 |
124 | 1; |
73e4f0f1 |
125 | |
d3082fac |
126 | =head1 NAME |
127 | |
128 | Catalyst::Script::FastCGI - The FastCGI Catalyst Script |
129 | |
130 | =head1 SYNOPSIS |
131 | |
53c6ec79 |
132 | myapp_fastcgi.pl [options] |
133 | |
134 | Options: |
8865ee12 |
135 | -? --help display this help and exits |
136 | -l --listen Socket path to listen on |
137 | (defaults to standard input) |
138 | can be HOST:PORT, :PORT or a |
139 | filesystem path |
140 | -n --nproc specify number of processes to keep |
141 | to serve requests (defaults to 1, |
142 | requires -listen) |
143 | -p --pidfile specify filename for pid file |
144 | (requires -listen) |
145 | -d --daemon daemonize (requires -listen) |
146 | -M --manager specify alternate process manager |
147 | (FCGI::ProcManager sub-class) |
148 | or empty string to disable |
149 | -e --keeperr send error messages to STDOUT, not |
150 | to the webserver |
151 | --proc_title set the process title |
d3082fac |
152 | |
153 | =head1 DESCRIPTION |
154 | |
53c6ec79 |
155 | Run a Catalyst application as fastcgi. |
d3082fac |
156 | |
383c5be6 |
157 | =head1 SEE ALSO |
158 | |
159 | L<Catalyst::ScriptRunner> |
160 | |
d3082fac |
161 | =head1 AUTHORS |
162 | |
163 | Catalyst Contributors, see Catalyst.pm |
164 | |
165 | =head1 COPYRIGHT |
166 | |
167 | This library is free software. You can redistribute it and/or modify it under |
168 | the same terms as Perl itself. |
169 | |
170 | =cut |