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