Merge remote branch 'svn/trunk' into psgi
[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 sub _plack_loader_args {
79     my ($self) = shift;
80     return (
81         map { $_->[0] => $self->${ \($_->[1] ? $_->[1]->[0] : $_->[0]) } }
82         Data::OptList::mkopt([
83             qw/pidfile listen manager nproc keep_stderr proc_title/,
84             detach     => [ 'daemon' ],
85         ])
86     );
87 }
88
89 sub _application_args {
90     my ($self) = shift;
91     return (
92         $self->listen,
93         {
94             nproc       => $self->nproc,
95             pidfile     => $self->pidfile,
96             manager     => $self->manager,
97             detach      => $self->daemon,
98             keep_stderr => $self->keeperr,
99             proc_title  => $self->proc_title,
100         }
101     );
102 }
103
104 __PACKAGE__->meta->make_immutable;
105
106 =head1 NAME
107
108 Catalyst::Script::FastCGI - The FastCGI Catalyst Script
109
110 =head1 SYNOPSIS
111
112   myapp_fastcgi.pl [options]
113
114  Options:
115    -? --help       display this help and exits
116    -l --listen     Socket path to listen on
117                    (defaults to standard input)
118                    can be HOST:PORT, :PORT or a
119                    filesystem path
120    -n --nproc      specify number of processes to keep
121                    to serve requests (defaults to 1,
122                    requires -listen)
123    -p --pidfile    specify filename for pid file
124                    (requires -listen)
125    -d --daemon     daemonize (requires -listen)
126    -M --manager    specify alternate process manager
127                    (FCGI::ProcManager sub-class)
128                    or empty string to disable
129    -e --keeperr    send error messages to STDOUT, not
130                    to the webserver
131       --proc_title set the process title
132
133 =head1 DESCRIPTION
134
135 Run a Catalyst application as fastcgi.
136
137 =head1 AUTHORS
138
139 Catalyst Contributors, see Catalyst.pm
140
141 =head1 COPYRIGHT
142
143 This library is free software. You can redistribute it and/or modify it under
144 the same terms as Perl itself.
145
146 =cut