Debian Codegraph @

bin/gearmand

raw ◀ ▶
1 Debian patch touch this file
1 #!/usr/bin/env perl
2
3 =head1 NAME
4
•5 gearmand-perl - Gearman client/worker connector.
6
7 =head1 SYNOPSIS
8
•9 gearmand-perl --daemon
10
11 =head1 DESCRIPTION
12
13 This is the main executable for L<Gearman::Server>. It provides
14 command-line configuration of port numbers, pidfiles, and
15 daemonization.
16
17 =head1 OPTIONS
18
19 =over
20
21 =item --daemonize / -d
22
23 Make the daemon run in the background (good for init.d scripts, bad
24 for running under daemontools/supervise).
25
26 =item --port=4730 / -p 4730
27
28 Set the port number, defaults to 4730.
29
30 =item --listen hostname / -L hostname
31
32 Address the server should listen on.
33
34 Default is
35
•36 =item --pidfile=/some/dir/gearmand-perl.pid
37
38 Write a pidfile when starting up
39
40 =item --debug=1
41
42 Enable debugging (currently the only debug output is when a client or worker connects).
43
44 =item --accept=10
45
46 Number of new connections to accept each time we see a listening socket ready. This doesn't usually
47 need to be tuned by anyone, however in dire circumstances you may need to do it quickly.
48
49 =item --wakeup=3
50
51 Number of workers to wake up per job inserted into the queue.
52
53 Zero (0) is a perfectly acceptable answer, and can be used if you don't care much about job latency.
54 This would bank on the base idea of a worker checking in with the server every so often.
55
56 Negative One (-1) indicates that all sleeping workers should be woken up.
57
58 All other negative numbers will cause the server to throw exception and not start.
59
60 =item --wakeup-delay=
61
62 Time interval before waking up more workers (the value specified by --wakeup) when jobs are still in
63 the queue.
64
65 Zero (0) means go as fast as possible, but not all at the same time. Similar to -1 on --wakeup, but
66 is more cooperative in gearmand's multitasking model.
67
68 Negative One (-1) means that this event won't happen, so only the initial workers will be woken up to
69 handle jobs in the queue.
70
71 =item --version
72
73 Display the version and exit.
74
75 =back
76
77 =head1 COPYRIGHT
78
79 Copyright 2005-2007, Danga Interactive
80
81 You are granted a license to use it under the same terms as Perl itself.
82
83 =head1 WARRANTY
84
85 This is free software. IT COMES WITHOUT WARRANTY OF ANY KIND.
86
87 =head1 AUTHORS
88
89 Brad Fitzpatrick <[email protected]>
90
91 Brad Whitaker <[email protected]>
92
93 =head1 SEE ALSO
94
95 L<Gearman::Server>
96
97 L<Gearman::Client>
98
99 L<Gearman::Worker>
100
101 L<Gearman::Client::Async>
102
103 =cut
104
105 package Gearmand;
106 use strict;
107 use warnings;
108
109 BEGIN {
110 # Provide informative names to anonymous subroutines
111 $^P = 0x200;
112 }
113
114 use Gearman::Server;
115
116 use Carp;
117 use Danga::Socket 1.52;
118 use Gearman::Util;
119 use Getopt::Long;
120 use IO::Socket::INET;
121 use POSIX ();
122 use Pod::Usage;
123 use Scalar::Util ();
124
125 use vars qw($DEBUG);
126
127 $DEBUG = 0;
128
129 my ($daemonize, $nokeepalive, $notify_pid, $opt_pidfile, $accept, $wakeup,
130 $wakeup_delay, $conf_host,);
131 my $conf_port = 4730;
132
133 Getopt::Long::GetOptions(
134 'd|daemonize' => \$daemonize,
135 'p|port=i' => \$conf_port,
136 'listen|L=s' => \$conf_host,
137 'debug=i' => \$DEBUG,
138 'pidfile=s' => \$opt_pidfile,
139 'accept=i' => \$accept,
140 'wakeup=i' => \$wakeup,
141 'wakeup-delay=f' => \$wakeup_delay,
142 'version|V' => sub {
143 print "Gearman::Server $Gearman::Server::VERSION$/";
144 exit;
145 },
146 'help|?' => sub {
147 pod2usage(-verbose => 1);
148 exit;
149 },
150
151 # for test suite only.
152 'notifypid|n=i' => \$notify_pid,
153 );
154
155 daemonize() if $daemonize;
156
157 # true if we've closed listening socket, and we're waiting for a
158 # convenient place to kill the process
159 our $graceful_shutdown = 0;
160
161 # handled manually
162 $SIG{'PIPE'} = "IGNORE";
163 my $server = Gearman::Server->new(
164 wakeup => $wakeup,
165 wakeup_delay => $wakeup_delay,
166 );
167 my $ssock = $server->create_listening_sock(
168 $conf_port,
169 accept_per_loop => $accept,
170 local_addr => $conf_host
171 );
172
173 if ($opt_pidfile) {
174 open my $fh, '>', $opt_pidfile or die "Could not open $opt_pidfile: $!";
175 print $fh "$$\n";
176 close $fh;
177 }
178
179 sub shutdown_graceful {
180 return if $graceful_shutdown;
181
182 my $ofds = Danga::Socket->OtherFds;
183 delete $ofds->{ fileno($ssock) };
184 $ssock->close;
185 $graceful_shutdown = 1;
186 shutdown_if_calm();
187 } ## end sub shutdown_graceful
188
189 sub shutdown_if_calm {
190 exit 0 unless $server->jobs_outstanding;
191 }
192
193 sub daemonize {
194 my ($pid, $sess_id, $i);
195
196 ## Fork and exit parent
197 if ($pid = fork) { exit 0; }
198
199 ## Detach ourselves from the terminal
200 croak "Cannot detach from controlling terminal"
201 unless $sess_id = POSIX::setsid();
202
203 ## Prevent possibility of acquiring a controlling terminal
204 $SIG{'HUP'} = 'IGNORE';
205 if ($pid = fork) { exit 0; }
206
207 ## Change working directory
208 chdir "/";
209
210 ## Clear file creation mask
211 umask 0;
212
213 ## Close open file descriptors
214 close(STDIN);
215 close(STDOUT);
216 close(STDERR);
217
218 ## Reopen stderr, stdout, stdin to /dev/null
219 open(STDIN, "+>/dev/null");
220 open(STDOUT, "+>&STDIN");
221 open(STDERR, "+>&STDIN");
222 } ## end sub daemonize
223
224 kill 'USR1', $notify_pid if $notify_pid;
225 Danga::Socket->EventLoop();
226
227 # Local Variables:
228 # mode: perl
229 # c-basic-indent: 4
230 # indent-tabs-mode: nil
231 # End: