drop namespace::autoclean
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Script / FastCGI.pm
1 package Catalyst::Script::FastCGI;
2 use Moose;
3 use Data::OptList;
4 use namespace::clean -except => [ 'meta' ];
5
6 sub _plack_engine_name { 'FCGI' }
7
8 with 'Catalyst::ScriptRole';
9
10 has listen => (
11     traits        => [qw(Getopt)],
12     cmd_aliases   => 'l',
13     isa           => 'Str',
14     is            => 'ro',
15     documentation => 'Specify a listening port/socket',
16 );
17
18 has pidfile => (
19     traits        => [qw(Getopt)],
20     cmd_aliases   => [qw/pid p/],
21     isa           => 'Str',
22     is            => 'ro',
23     documentation => 'Specify a pidfile',
24 );
25
26 has daemon => (
27     traits        => [qw(Getopt)],
28     isa           => 'Bool',
29     is            => 'ro',
30     cmd_aliases   => [qw/d detach/], # Eww, detach is here as we fucked it up.. Deliberately not documented
31     documentation => 'Daemonize (go into the background)',
32 );
33
34 has manager => (
35     traits        => [qw(Getopt)],
36     isa           => 'Str',
37     is            => 'ro',
38     cmd_aliases   => 'M',
39     documentation => 'Use a different FastCGI process manager class',
40 );
41
42 has keeperr => (
43     traits        => [qw(Getopt)],
44     cmd_aliases   => 'e',
45     isa           => 'Bool',
46     is            => 'ro',
47     documentation => 'Log STDERR',
48 );
49
50 has nproc => (
51     traits        => [qw(Getopt)],
52     cmd_aliases   => 'n',
53     isa           => 'Int',
54     is            => 'ro',
55     documentation => 'Specify a number of child processes',
56 );
57
58 has proc_title => (
59     traits        => [qw(Getopt)],
60     isa           => 'Str',
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) = @_;
74     $self->proc_title;
75 }
76
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
88 sub _plack_loader_args {
89     my ($self) = shift;
90
91     my $opts = Data::OptList::mkopt([
92       qw/manager nproc proc_title/,
93             pid             => [ 'pidfile' ],
94             daemonize       => [ 'daemon' ],
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;
105 }
106
107 around _application_args => sub {
108     my ($orig, $self) = @_;
109     return (
110         $self->listen,
111         {
112             %{ $self->$orig },
113             nproc       => $self->nproc,
114             pidfile     => $self->pidfile,
115             manager     => $self->manager,
116             detach      => $self->daemon,
117             keep_stderr => $self->keeperr,
118             proc_title  => $self->proc_title,
119         }
120     );
121 };
122
123 __PACKAGE__->meta->make_immutable;
124 1;
125
126 =head1 NAME
127
128 Catalyst::Script::FastCGI - The FastCGI Catalyst Script
129
130 =head1 SYNOPSIS
131
132   myapp_fastcgi.pl [options]
133
134  Options:
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
152
153 =head1 DESCRIPTION
154
155 Run a Catalyst application as fastcgi.
156
157 =head1 SEE ALSO
158
159 L<Catalyst::ScriptRunner>
160
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