Newer Older
103 lines | 3.098kb
add files
Yuki Kimoto authored on 2014-03-26
1
package Mojolicious::Command::daemon;
2
use Mojo::Base 'Mojolicious::Command';
3

            
4
use Getopt::Long qw(GetOptionsFromArray :config no_auto_abbrev no_ignore_case);
5
use Mojo::Server::Daemon;
6

            
7
has description => "Start application with HTTP and WebSocket server.\n";
8
has usage       => <<EOF;
9
usage: $0 daemon [OPTIONS]
10

            
11
These options are available:
12
  -b, --backlog <size>         Listen backlog size, defaults to SOMAXCONN.
13
  -c, --clients <number>       Maximum number of concurrent clients, defaults
14
                               to 1000.
15
  -g, --group <name>           Group name for process.
16
  -i, --inactivity <seconds>   Inactivity timeout, defaults to the value of
17
                               MOJO_INACTIVITY_TIMEOUT or 15.
18
  -l, --listen <location>      One or more locations you want to listen on,
19
                               defaults to the value of MOJO_LISTEN or
20
                               "http://*:3000".
21
  -p, --proxy                  Activate reverse proxy support, defaults to
22
                               the value of MOJO_REVERSE_PROXY.
23
  -r, --requests <number>      Maximum number of requests per keep-alive
24
                               connection, defaults to 25.
25
  -u, --user <name>            Username for process.
26
EOF
27

            
28
sub run {
29
  my ($self, @args) = @_;
30

            
31
  my $daemon = Mojo::Server::Daemon->new(app => $self->app);
32
  GetOptionsFromArray \@args,
33
    'b|backlog=i'    => sub { $daemon->backlog($_[1]) },
34
    'c|clients=i'    => sub { $daemon->max_clients($_[1]) },
35
    'g|group=s'      => sub { $daemon->group($_[1]) },
36
    'i|inactivity=i' => sub { $daemon->inactivity_timeout($_[1]) },
37
    'l|listen=s'     => \my @listen,
38
    'p|proxy' => sub { $ENV{MOJO_REVERSE_PROXY} = 1 },
39
    'r|requests=i' => sub { $daemon->max_requests($_[1]) },
40
    'u|user=s'     => sub { $daemon->user($_[1]) };
41

            
42
  $daemon->listen(\@listen) if @listen;
43
  $daemon->run;
44
}
45

            
46
1;
47

            
48
=encoding utf8
49

            
50
=head1 NAME
51

            
52
Mojolicious::Command::daemon - Daemon command
53

            
54
=head1 SYNOPSIS
55

            
56
  use Mojolicious::Command::daemon;
57

            
58
  my $daemon = Mojolicious::Command::daemon->new;
59
  $daemon->run(@ARGV);
60

            
61
=head1 DESCRIPTION
62

            
63
L<Mojolicious::Command::daemon> starts applications with
64
L<Mojo::Server::Daemon> backend.
65

            
66
This is a core command, that means it is always enabled and its code a good
67
example for learning to build new commands, you're welcome to fork it.
68

            
69
=head1 ATTRIBUTES
70

            
71
L<Mojolicious::Command::daemon> inherits all attributes from
72
L<Mojolicious::Command> and implements the following new ones.
73

            
74
=head2 description
75

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

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

            
81
=head2 usage
82

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

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

            
88
=head1 METHODS
89

            
90
L<Mojolicious::Command::daemon> inherits all methods from
91
L<Mojolicious::Command> and implements the following new ones.
92

            
93
=head2 run
94

            
95
  $daemon->run(@ARGV);
96

            
97
Run this command.
98

            
99
=head1 SEE ALSO
100

            
101
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
102

            
103
=cut