File: DaemonPrefork.pm

package info (click to toggle)
libmojolicious-perl 0.999926-1%2Bsqueeze2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,672 kB
  • ctags: 949
  • sloc: perl: 17,391; makefile: 4
file content (150 lines) | stat: -rw-r--r-- 5,100 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# Copyright (C) 2008-2010, Sebastian Riedel.

package Mojo::Command::DaemonPrefork;

use strict;
use warnings;

use base 'Mojo::Command';

use Mojo::Server::Daemon::Prefork;

use Getopt::Long 'GetOptions';

__PACKAGE__->attr(description => <<'EOF');
Start application with preforking HTTP 1.1 backend.
EOF
__PACKAGE__->attr(usage => <<"EOF");
usage: $0 daemon_prefork [OPTIONS]

These options are available:
  --clients <number>             Set maximum number of concurrent clients per
                                 child, defaults to 1.
  --daemonize                    Daemonize parent.
  --group <name>                 Set group name for children.
  --idle <seconds>               Set time children can be idle without
                                 getting killed, defaults to 30.
  --interval <seconds>           Set interval for process maintainance,
                                 defaults to 15.
  --keepalive <seconds>          Set keep-alive timeout, defaults to 15.
  --keepaliverequests <number>   Set maximum number of requests per
                                 keep-alive connection, defaults to 100.
  --listen <locations>           Set a comma separated list of locations you
                                 want to listen on, defaults to http://*:3000.
  --lock <path>                  Set path to lock file, defaults to a random
                                 temporary file.
  --maxspare <number>            Set maximum amount of idle children,
                                 defaults to 10.
  --minspare <number>            Set minimum amount of idle children,
                                 defaults to 5.
  --pid <path>                   Set path to pid file, defaults to a random
                                 temporary file.
  --queue <size>                 Set listen queue size, defaults to
                                 SOMAXCONN.
  --reload                       Automatically reload application when the
                                 source code changes.
  --requests <number>            Set maximum number of requests a worker
                                 process is allowed to handle, defaults to
                                 1000.
  --servers <number>             Set maximum number of children, defaults to
                                 100.
  --start <number>               Set number of children to spawn at startup,
                                 defaults to 5.
  --user <name>                  Set user name for children.
EOF

# Dear Mr. President, there are too many states nowadays.
# Please eliminate three.
# P.S. I am not a crackpot.
sub run {
    my $self   = shift;
    my $daemon = Mojo::Server::Daemon::Prefork->new;

    # Options
    my $daemonize;
    @ARGV = @_ if @_;
    GetOptions(
        'clients=i'   => sub { $daemon->max_clients($_[1]) },
        daemonize     => \$daemonize,
        'group=s'     => sub { $daemon->group($_[1]) },
        'idle=i'      => sub { $daemon->idle_timeout($_[1]) },
        'interval=i'  => sub { $daemon->cleanup_interval($_[1]) },
        'keepalive=i' => sub { $daemon->keep_alive_timeout($_[1]) },
        'keepaliverequests=i' =>
          sub { $daemon->max_keep_alive_requests($_[1]) },
        'listen=s'   => sub { $daemon->listen($_[1]) },
        'lock=s'     => sub { $daemon->lock_file($_[1]) },
        'maxspare=i' => sub { $daemon->max_spare_servers($_[1]) },
        'minspare=i' => sub { $daemon->min_spare_servers($_[1]) },
        'pid=s'      => sub { $daemon->pid_file($_[1]) },
        'queue=i'    => sub { $daemon->listen_queue_size($_[1]) },
        reload       => sub { $daemon->reload(1) },
        'requests=i' => sub { $daemon->max_requests($_[1]) },
        'servers=i'  => sub { $daemon->max_servers($_[1]) },
        'start=i'    => sub { $daemon->start_servers($_[1]) },
        'user=s'     => sub { $daemon->user($_[1]) }
    );

    # Daemonize
    $daemon->daemonize if $daemonize;

    # Run
    $daemon->run;

    return $self;
}

1;
__END__

=head1 NAME

Mojo::Command::DaemonPrefork - Prefork Daemon Command

=head1 SYNOPSIS

    use Mojo::Command::Daemon::Prefork;

    my $daemon = Mojo::Command::Daemon::Prefork->new;
    $daemon->run(@ARGV);

=head1 DESCRIPTION

L<Mojo::Command::Daemon::Prefork> is a command interface to
L<Mojo::Server::Daemon::Prefork>.

=head1 ATTRIBUTES

L<Mojo::Command::Daemon::Prefork> inherits all attributes from
L<Mojo::Command> and implements the following new ones.

=head2 C<description>

    my $description = $daemon->description;
    $daemon         = $daemon->description('Foo!');

Short description of this command, used for the command list.

=head2 C<usage>

    my $usage = $daemon->usage;
    $daemon   = $daemon->usage('Foo!');

Usage information for this command, used for the help screen.

=head1 METHODS

L<Mojo::Command::Daemon::Prefork> inherits all methods from L<Mojo::Command>
and implements the following new ones.

=head2 C<run>

    $daemon = $daemon->run(@ARGV);

Run this command.

=head1 SEE ALSO

L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicious.org>.

=cut