| 1 |
#!perl |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
use Getopt::Long; |
| 7 |
use Pod::Usage; |
| 8 |
use Server::Starter qw(start_server restart_server stop_server); |
| 9 |
|
| 10 |
my %opts = ( |
| 11 |
port => [], |
| 12 |
path => [], |
| 13 |
); |
| 14 |
|
| 15 |
GetOptions( |
| 16 |
map { |
| 17 |
$_ => do { |
| 18 |
my $name = (split '=', $_, 2)[0]; |
| 19 |
$name =~ s/-/_/g; |
| 20 |
$opts{$name} ||= undef; |
| 21 |
ref($opts{$name}) ? $opts{$name} : \$opts{$name}; |
| 22 |
}, |
| 23 |
} qw(port=s path=s interval=i log-file=s pid-file=s dir=s signal-on-hup=s signal-on-term=s |
| 24 |
backlog=i envdir=s enable-auto-restart daemonize auto-restart-interval=i kill-old-delay=i |
| 25 |
status-file=s restart stop help version), |
| 26 |
) or exit 1; |
| 27 |
pod2usage( |
| 28 |
-exitval => 0, |
| 29 |
-verbose => 1, |
| 30 |
) if $opts{help}; |
| 31 |
if ($opts{version}) { |
| 32 |
print "$Server::Starter::VERSION\n"; |
| 33 |
exit 0; |
| 34 |
} |
| 35 |
|
| 36 |
if ($opts{restart}) { |
| 37 |
restart_server(%opts); |
| 38 |
exit 0; |
| 39 |
} |
| 40 |
|
| 41 |
if ($opts{stop}) { |
| 42 |
stop_server(%opts); |
| 43 |
exit 0; |
| 44 |
} |
| 45 |
|
| 46 |
if ($opts{daemonize}) { |
| 47 |
die "Usage: --log-file option must be specified together with --deamonize\n" |
| 48 |
unless defined $opts{log_file}; |
| 49 |
} |
| 50 |
|
| 51 |
# validate options |
| 52 |
die "server program not specified\n" |
| 53 |
unless @ARGV; |
| 54 |
|
| 55 |
start_server( |
| 56 |
%opts, |
| 57 |
exec => \@ARGV, |
| 58 |
); |
| 59 |
|
| 60 |
__END__ |
| 61 |
|
| 62 |
=head1 NAME |
| 63 |
|
| 64 |
start_server - a superdaemon for hot-deploying server programs |
| 65 |
|
| 66 |
=head1 SYNOPSIS |
| 67 |
|
| 68 |
start_server [options] -- server-prog server-arg1 server-arg2 ... |
| 69 |
|
| 70 |
# start Plack using Starlet listening at TCP port 8000 |
| 71 |
start_server --port=8000 -- plackup -s Starlet --max-workers=100 index.psgi |
| 72 |
|
| 73 |
=head1 DESCRIPTION |
| 74 |
|
| 75 |
This script is a frontend of L<Server::Starter>. For more information please refer to the documentation of the module. |
| 76 |
|
| 77 |
=head1 OPTIONS |
| 78 |
|
| 79 |
=head2 --port=(port|host:port|port=fd|host:port=fd) |
| 80 |
|
| 81 |
TCP port to listen to (if omitted, will not bind to any ports) |
| 82 |
|
| 83 |
If host is not specified, then the program will bind to the default address of IPv4 ("0.0.0.0"). |
| 84 |
Square brackets should be used to specify an IPv6 address (e.g. --port=[::1]:8080) |
| 85 |
|
| 86 |
If fd is specified, then start_server allocates the socket at the given number. |
| 87 |
|
| 88 |
The command binds to UDP ports if the port numbers are prefixed by "u" (e.g., --port=u443). |
| 89 |
|
| 90 |
=head2 --path=path |
| 91 |
|
| 92 |
path at where to listen using unix socket (optional) |
| 93 |
|
| 94 |
=head2 --dir=path |
| 95 |
|
| 96 |
working directory, start_server do chdir to before exec (optional) |
| 97 |
|
| 98 |
=head2 --interval=seconds |
| 99 |
|
| 100 |
minimum interval to respawn the server program (default: 1) |
| 101 |
|
| 102 |
=head2 --signal-on-hup=SIGNAL |
| 103 |
|
| 104 |
name of the signal to be sent to the server process when start_server receives a SIGHUP (default: SIGTERM). If you use this option, be sure to also use C<--signal-on-term> below. |
| 105 |
|
| 106 |
=head2 --signal-on-term=SIGNAL |
| 107 |
|
| 108 |
name of the signal to be sent to the server process when start_server receives a SIGTERM (default: SIGTERM) |
| 109 |
|
| 110 |
=head2 --pid-file=filename |
| 111 |
|
| 112 |
if set, writes the process id of the start_server process to the file |
| 113 |
|
| 114 |
=head2 --status-file=filename |
| 115 |
|
| 116 |
if set, writes the status of the server process(es) to the file |
| 117 |
|
| 118 |
=head2 --envdir=ENVDIR |
| 119 |
|
| 120 |
directory that contains environment variables to the server processes and superdaemon. |
| 121 |
It is inspired by C<envdir> in C<daemontools>. |
| 122 |
This can be overwritten by environment variable C<ENVDIR>. |
| 123 |
|
| 124 |
=head2 --log-file=file |
| 125 |
|
| 126 |
=head2 --log-file="| cmd args..." |
| 127 |
|
| 128 |
if set, redirects STDOUT and STDERR to given file or command |
| 129 |
|
| 130 |
=head2 --daemonize |
| 131 |
|
| 132 |
deamonizes the server (by doing fork,setsid,fork). Must be used together with C<--log-file>. |
| 133 |
|
| 134 |
=head2 --enable-auto-restart |
| 135 |
|
| 136 |
enables automatic restart by time. |
| 137 |
This can be overwritten by environment variable C<ENABLE_AUTO_RESTART>. |
| 138 |
|
| 139 |
=head2 --auto-restart-interval=seconds |
| 140 |
|
| 141 |
automatic restart interval (default 360). It is used with C<--enable-auto-restart> option. |
| 142 |
This can be overwritten by environment variable C<AUTO_RESTART_INTERVAL>. |
| 143 |
|
| 144 |
=head2 --kill-old-delay=seconds |
| 145 |
|
| 146 |
time to suspend to send a signal to the old worker. The default value is 5 when C<--enable-auto-restart> is set, 0 otherwise. |
| 147 |
This can be overwritten by environment variable C<KILL_OLD_DELAY>. |
| 148 |
|
| 149 |
=head2 --backlog=size |
| 150 |
|
| 151 |
specifies a listen backlog parameter, whose default is SOMAXCONN (usually 128 on Linux). While SOMAXCONN is enough for most loads, large backlog is required for heavy loads. |
| 152 |
|
| 153 |
=head2 --restart |
| 154 |
|
| 155 |
this is a wrapper command that reads the pid of the start_server process from --pid-file, sends SIGHUP to the process and waits until the server(s) of the older generation(s) die by monitoring the contents of the --status-file |
| 156 |
|
| 157 |
=head2 --stop |
| 158 |
|
| 159 |
this is a wrapper command that reads the pid of the start_server process from --pid-file, sends SIGTERM to the process. |
| 160 |
|
| 161 |
=head2 --help |
| 162 |
|
| 163 |
prints this help |
| 164 |
|
| 165 |
=head2 --version |
| 166 |
|
| 167 |
prints the version number |
| 168 |
|
| 169 |
=head1 AUTHOR |
| 170 |
|
| 171 |
Kazuho Oku |
| 172 |
|
| 173 |
=head1 SEE ALSO |
| 174 |
|
| 175 |
L<Server::Starter> |
| 176 |
|
| 177 |
=head1 LICENSE |
| 178 |
|
| 179 |
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. |
| 180 |
|
| 181 |
=cut |