Server/Client.pm untested

filter: Statementstmt 0.0% 0 / 392 Branchbran 0.0% 0 / 94 Conditioncond 0.0% 0 / 38 Subroutinesub 0.0% 0 / 46 Podpod 0.0% 0 / 40 totaltotal 0.0% 0 / 610 CC 48 SCAR 30.5
CC
48
Cov
0%
event_read
90.0
CMD_get_status
56.0
close
30.0
CRAP 21.0
1 package Gearman::Server::Client;
2  
3 =head1 NAME
4  
5 Gearman::Server::Client
6  
7 =head1 NAME
8  
9 Used by L<Gearman::Server> to instantiate connections from clients.
10 Clients speak either a binary protocol, for normal operation (calling
11 functions, grabbing function call requests, returning function values,
12 etc), or a text-based line protocol, for relatively rare
13 administrative / monitoring commands.
14  
15 The binary protocol commands aren't currently documented. (FIXME) But
16 they're well-implemented in L<Gearman::Client>, L<Gearman::Worker>,
17 and L<Gearman::Client::Async>, if that's any consolation.
18  
19 The line-based administrative commands are documented below.
20  
21 =cut
22  
23 use strict;
24 use Danga::Socket;
25 use base 'Danga::Socket';
26 use fields (
27 'can_do', # { $job_name => $timeout } $timeout can be undef indicating no timeout
28 'can_do_list',
29 'can_do_iter',
30 'fast_read',
31 'fast_buffer',
32 'read_buf',
33 'sleeping', # 0/1: they've said they're sleeping and we haven't woken them up
34 'timer', # Timer for job cancellation
35 'doing', # { $job_handle => Job }
36 'client_id', # opaque string, no whitespace. workers give this so checker scripts
37 # can tell apart the same worker connected to multiple jobservers.
38 'server', # pointer up to client's server
39 'options',
40 'jobs_done_since_sleep',
41 );
42  
43 # 60k read buffer default, similar to perlbal's backend read.
44 use constant READ_SIZE => 60 * 1024;
45 use constant MAX_READ_SIZE => 512 * 1024;
46  
47 # Class Method:
48 sub new {
49 my Gearman::Server::Client $self = shift;
50 my ($sock, $server) = @_;
51 $self = fields::new($self) unless ref $self;
52 $self->SUPER::new($sock);
53  
54 $self->{fast_read} = undef; # Number of bytes to read as fast as we can (don't try to process them)
55 $self->{fast_buffer} = []; # Array of buffers used during fast read operation
56 $self->{read_buf} = '';
57 $self->{sleeping} = 0;
58 $self->{can_do} = {};
59 $self->{doing} = {}; # handle -> Job
60 $self->{can_do_list} = [];
61 $self->{can_do_iter} = 0; # numeric iterator for where we start looking for jobs
62 $self->{client_id} = "-";
63 $self->{server} = $server;
64 $self->{options} = {};
65 $self->{jobs_done_since_sleep} = 0;
66  
67 return $self;
68 }
69  
70 sub option {
71 my Gearman::Server::Client $self = shift;
72 my $option = shift;
73  
74 return $self->{options}->{$option};
75 }
76  
77 sub close {
78 my Gearman::Server::Client $self = shift;
79  
80 my $doing = $self->{doing};
81  
82 while (my ($handle, $job) = each %$doing) {
83 my $msg = Gearman::Util::pack_res_command("work_fail", $handle);
84 $job->relay_to_listeners($msg);
85 $job->note_finished(0);
86 }
87  
88 # Clear the doing list, since it may contain a set of jobs which contain
89 # references back to us.
90 %$doing = ();
91  
92 # Remove self from sleepers, otherwise it will be leaked if another worker
93 # for the job never connects.
94 my $sleepers = $self->{server}{sleepers};
95 my $sleepers_list = $self->{server}{sleepers_list};
96 for my $job (@{ $self->{can_do_list} }) {
97 my $sleeping = $sleepers->{$job};
98 delete $sleeping->{$self};
99  
100 my $new_sleepers_list;
101 for my $client (@{$sleepers_list->{$job}}) {
102 next unless $client;
103 push @{$new_sleepers_list}, $client unless $sleeping->{$client};
104 }
105 if ($new_sleepers_list) {
106 $self->{server}{sleepers_list}->{$job} = $new_sleepers_list;
107 } else {
108 delete $self->{server}{sleepers_list}->{$job};
109 }
110  
111 delete $sleepers->{$job} unless %$sleeping;
112 }
113  
114 $self->{server}->note_disconnected_client($self);
115  
116 $self->CMD_reset_abilities;
117  
118 $self->SUPER::close;
119 }
120  
121 # Client
122 sub event_read {
123 my Gearman::Server::Client $self = shift;
124  
125 my $read_size = $self->{fast_read} || READ_SIZE;
126 my $bref = $self->read($read_size);
127  
128 # Delay close till after buffers are written on EOF. If we are unable
129 # to write 'err' or 'hup' will be thrown and we'll close faster.
130 return $self->write(sub { $self->close } ) unless defined $bref;
131  
132 if ($self->{fast_read}) {
133 push @{$self->{fast_buffer}}, $$bref;
134 $self->{fast_read} -= length($$bref);
135  
136 # If fast_read is still positive, then we need to read more data
137 return if ($self->{fast_read} > 0);
138  
139 # Append the whole giant read buffer to our main read buffer
140 $self->{read_buf} .= join('', @{$self->{fast_buffer}});
141  
142 # Reset the fast read state for next time.
143 $self->{fast_buffer} = [];
144 $self->{fast_read} = undef;
145 } else {
146 # Exact read size length likely means we have more sitting on the
147 # socket. Buffer up to half a meg in one go.
148 if (length($$bref) == READ_SIZE) {
149 my $limit = int(MAX_READ_SIZE / READ_SIZE);
150 my @crefs = ($$bref);
151 while (my $cref = $self->read(READ_SIZE)) {
152 push(@crefs, $$cref);
153 last if (length($$cref) < READ_SIZE || $limit-- < 1);
154 }
155 $bref = \join('', @crefs);
156 }
157 $self->{read_buf} .= $$bref;
158 }
159  
160 my $found_cmd;
161 do {
162 $found_cmd = 1;
163 my $blen = length($self->{read_buf});
164  
165 if ($self->{read_buf} =~ /^\0REQ(.{8,8})/s) {
166 my ($cmd, $len) = unpack("NN", $1);
167 if ($blen < $len + 12) {
168 # Start a fast read loop to get all the data we need, less
169 # what we already have in the buffer.
170 $self->{fast_read} = $len + 12 - $blen;
171 return;
172 }
173  
174 $self->process_cmd($cmd, substr($self->{read_buf}, 12, $len));
175  
176 # and slide down buf:
177 $self->{read_buf} = substr($self->{read_buf}, 12+$len);
178  
179 } elsif ($self->{read_buf} =~ s/^(\w.+?)?\r?\n//) {
180 # ASCII command case (useful for telnetting in)
181 my $line = $1;
182 $self->process_line($line);
183 } else {
184 $found_cmd = 0;
185 }
186 } while ($found_cmd);
187 }
188  
189 sub event_write {
190 my $self = shift;
191 my $done = $self->write(undef);
192 $self->watch_write(0) if $done;
193 }
194  
195 # Line based command processor
196 sub process_line {
197 my Gearman::Server::Client $self = shift;
198 my $line = shift;
199  
200 if ($line && $line =~ /^(\w+)\s*(.*)/) {
201 my ($cmd, $args) = ($1, $2);
202 $cmd = lc($cmd);
203 my $code = $self->can("TXTCMD_$cmd");
204 if ($code) {
205 $code->($self, $args);
206 return;
207 }
208 }
209  
210 return $self->err_line('unknown_command');
211 }
212  
213 =head1 Binary Protocol Structure
214  
215 All binary protocol exchanges between clients (which can be callers,
216 workers, or both) and the Gearman server have common packet header:
217  
218 4 byte magic -- either "\0REQ" for requests to the server, or
219 "\0RES" for responses from the server
220 4 byte type -- network order integer, representing the packet type
221 4 byte length -- network order length, for data segment.
222 data -- optional, if length is non-zero
223  
224 =head1 Binary Protocol Commands
225  
226 =head2 echo_req (type=16)
227  
228 A debug command. The server will reply with the same data, in a echo_res (type=17) packet.
229  
230 =head2 (and many more...)
231  
232 FIXME: auto-generate protocol docs from internal Gearman::Util table,
233 once annotated with some English?
234  
235 =cut
236  
237 sub CMD_echo_req {
238 my Gearman::Server::Client $self = shift;
239 my $blobref = shift;
240  
241 return $self->res_packet("echo_res", $$blobref);
242 }
243  
244 sub CMD_work_status {
245 my Gearman::Server::Client $self = shift;
246 my $ar = shift;
247 my ($handle, $nu, $de) = split(/\0/, $$ar);
248  
249 my $job = $self->{doing}{$handle};
250 return $self->error_packet("not_worker") unless $job && $job->worker == $self;
251  
252 my $msg = Gearman::Util::pack_res_command("work_status", $$ar);
253 $job->relay_to_listeners($msg);
254 $job->status([$nu, $de]);
255 return 1;
256 }
257  
258 sub CMD_work_complete {
259 my Gearman::Server::Client $self = shift;
260 my $ar = shift;
261  
262 $$ar =~ s/^(.+?)\0//;
263 my $handle = $1;
264  
265 my $job = delete $self->{doing}{$handle};
266 return $self->error_packet("not_worker") unless $job && $job->worker == $self;
267  
268 my $msg = Gearman::Util::pack_res_command("work_complete", join("\0", $handle, $$ar));
269 $job->relay_to_listeners($msg);
270 $job->note_finished(1);
271 if (my $timer = $self->{timer}) {
272 $timer->cancel;
273 $self->{timer} = undef;
274 }
275  
276 return 1;
277 }
278  
279 sub CMD_work_fail {
280 my Gearman::Server::Client $self = shift;
281 my $ar = shift;
282 my $handle = $$ar;
283 my $job = delete $self->{doing}{$handle};
284 return $self->error_packet("not_worker") unless $job && $job->worker == $self;
285  
286 my $msg = Gearman::Util::pack_res_command("work_fail", $handle);
287 $job->relay_to_listeners($msg);
288 $job->note_finished(1);
289 if (my $timer = $self->{timer}) {
290 $timer->cancel;
291 $self->{timer} = undef;
292 }
293  
294 return 1;
295 }
296  
297 sub CMD_work_exception {
298 my Gearman::Server::Client $self = shift;
299 my $ar = shift;
300  
301 $$ar =~ s/^(.+?)\0//;
302 my $handle = $1;
303 my $job = $self->{doing}{$handle};
304  
305 return $self->error_packet("not_worker") unless $job && $job->worker == $self;
306  
307 my $msg = Gearman::Util::pack_res_command("work_exception", join("\0", $handle, $$ar));
308 $job->relay_to_option_listeners($msg, "exceptions");
309  
310 return 1;
311 }
312  
313 sub CMD_pre_sleep {
314 my Gearman::Server::Client $self = shift;
315 $self->{'sleeping'} = 1;
316 $self->{server}->on_client_sleep($self);
317 return 1;
318 }
319  
320 sub CMD_grab_job {
321 my Gearman::Server::Client $self = shift;
322  
323 my $job;
324 my $can_do_size = scalar @{$self->{can_do_list}};
325  
326 unless ($can_do_size) {
327 $self->res_packet("no_job");
328 return;
329 }
330  
331 # the offset where we start asking for jobs, to prevent starvation
332 # of some job types.
333 $self->{can_do_iter} = ($self->{can_do_iter} + 1) % $can_do_size;
334  
335 my $tried = 0;
336 while ($tried < $can_do_size) {
337 my $idx = ($tried + $self->{can_do_iter}) % $can_do_size;
338 $tried++;
339 my $job_to_grab = $self->{can_do_list}->[$idx];
340 $job = $self->{server}->grab_job($job_to_grab)
341 or next;
342  
343 $job->worker($self);
344 $self->{doing}{$job->handle} = $job;
345  
346 my $timeout = $self->{can_do}->{$job_to_grab};
347 if (defined $timeout) {
348 my $timer = Danga::Socket->AddTimer($timeout, sub {
349 return $self->error_packet("not_worker") unless $job->worker == $self;
350  
351 my $msg = Gearman::Util::pack_res_command("work_fail", $job->handle);
352 $job->relay_to_listeners($msg);
353 $job->note_finished(1);
354 $job->clear_listeners;
355 $self->{timer} = undef;
356 });
357 $self->{timer} = $timer;
358 }
359 return $self->res_packet("job_assign",
360 join("\0",
361 $job->handle,
362 $job->func,
363 ${$job->argref},
364 ));
365 }
366  
367 $self->res_packet("no_job");
368 }
369  
370 sub CMD_can_do {
371 my Gearman::Server::Client $self = shift;
372 my $ar = shift;
373  
374 $self->{can_do}->{$$ar} = undef;
375 $self->_setup_can_do_list;
376 }
377  
378 sub CMD_can_do_timeout {
379 my Gearman::Server::Client $self = shift;
380 my $ar = shift;
381  
382 my ($task, $timeout) = $$ar =~ m/([^\0]+)(?:\0(.+))?/;
383  
384 if (defined $timeout) {
385 $self->{can_do}->{$task} = $timeout;
386 } else {
387 $self->{can_do}->{$task} = undef;
388 }
389  
390 $self->_setup_can_do_list;
391 }
392  
393 sub CMD_option_req {
394 my Gearman::Server::Client $self = shift;
395 my $ar = shift;
396  
397 my $success = sub {
398 return $self->res_packet("option_res", $$ar);
399 };
400  
401 if ($$ar eq 'exceptions') {
402 $self->{options}->{exceptions} = 1;
403 return $success->();
404 }
405  
406 return $self->error_packet("unknown_option");
407 }
408  
409 sub CMD_set_client_id {
410 my Gearman::Server::Client $self = shift;
411 my $ar = shift;
412  
413 $self->{client_id} = $$ar;
414 $self->{client_id} =~ s/\s+//g;
415 $self->{client_id} = "-" unless length $self->{client_id};
416 }
417  
418 sub CMD_cant_do {
419 my Gearman::Server::Client $self = shift;
420 my $ar = shift;
421  
422 delete $self->{can_do}->{$$ar};
423 $self->_setup_can_do_list;
424 }
425  
426 sub CMD_get_status {
427 my Gearman::Server::Client $self = shift;
428 my $ar = shift;
429 my $job = $self->{server}->job_by_handle($$ar);
430  
431 # handles can't contain nulls
432 return if $$ar =~ /\0/;
433  
434 my ($known, $running, $num, $den);
435 $known = 0;
436 $running = 0;
437 if ($job) {
438 $known = 1;
439 $running = $job->worker ? 1 : 0;
440 if (my $stat = $job->status) {
441 ($num, $den) = @$stat;
442 }
443 }
444  
445 $num = '' unless defined $num;
446 $den = '' unless defined $den;
447  
448 $self->res_packet("status_res", join("\0",
449 $$ar,
450 $known,
451 $running,
452 $num,
453 $den));
454 }
455  
456 sub CMD_reset_abilities {
457 my Gearman::Server::Client $self = shift;
458  
459 $self->{can_do} = {};
460 $self->_setup_can_do_list;
461 }
462  
463 sub _setup_can_do_list {
464 my Gearman::Server::Client $self = shift;
465 $self->{can_do_list} = [ keys %{$self->{can_do}} ];
466 $self->{can_do_iter} = 0;
467 }
468  
469 sub CMD_submit_job { push @_, 1; &_cmd_submit_job; }
470 sub CMD_submit_job_bg { push @_, 0; &_cmd_submit_job; }
471 sub CMD_submit_job_high { push @_, 1, 1; &_cmd_submit_job; }
472  
473 sub _cmd_submit_job {
474 my Gearman::Server::Client $self = shift;
475 my $ar = shift;
476 my $subscribe = shift;
477 my $high_pri = shift;
478  
479 return $self->error_packet("invalid_args", "No func/uniq header [$$ar].")
480 unless $$ar =~ s/^(.+?)\0(.*?)\0//;
481  
482 my ($func, $uniq) = ($1, $2);
483  
484 my $job = Gearman::Server::Job->new($self->{server}, $func, $uniq, $ar, $high_pri);
485  
486 if ($subscribe) {
487 $job->add_listener($self);
488 } else {
489 # background mode
490 $job->require_listener(0);
491 }
492  
493 $self->res_packet("job_created", $job->handle);
494 $self->{server}->wake_up_sleepers($func);
495 }
496  
497 sub res_packet {
498 my Gearman::Server::Client $self = shift;
499 my ($code, $arg) = @_;
500 $self->write(Gearman::Util::pack_res_command($code, $arg));
501 return 1;
502 }
503  
504 sub error_packet {
505 my Gearman::Server::Client $self = shift;
506 my ($code, $msg) = @_;
507 $self->write(Gearman::Util::pack_res_command("error", "$code\0$msg"));
508 return 0;
509 }
510  
511 sub process_cmd {
512 my Gearman::Server::Client $self = shift;
513 my $cmd = shift;
514 my $blob = shift;
515  
516 my $cmd_name = "CMD_" . Gearman::Util::cmd_name($cmd);
517 my $ret = eval {
518 $self->$cmd_name(\$blob);
519 };
520 return $ret unless $@;
521 warn "Error: $@\n";
522 return $self->error_packet("server_error", $@);
523 }
524  
525 sub event_err { my $self = shift; $self->close; }
526 sub event_hup { my $self = shift; $self->close; }
527  
528 ############################################################################
529  
530 =head1 Line based commands
531  
532 These commands are used for administrative or statistic tasks to be done on the gearman server. They can be entered using a line based client (telnet, etc.) by connecting to the listening port (7003) and are also intended to be machine parsable.
533  
534 =head2 "workers"
535  
536 Emits list of registered workers, their fds, IPs, client ids, and list of registered abilities (function names they can do). Of format:
537  
538 fd ip.x.y.z client_id : func_a func_b func_c
539 fd ip.x.y.z client_id : func_a func_b func_c
540 fd ip.x.y.z client_id : func_a func_b func_c
541 .
542  
543 It ends with a line with just a period.
544  
545 =cut
546  
547 sub TXTCMD_workers {
548 my Gearman::Server::Client $self = shift;
549  
550 foreach my $cl (sort { $a->{fd} <=> $b->{fd} } $self->{server}->clients) {
551 my $fd = $cl->{fd};
552 $self->write("$fd " . $cl->peer_ip_string . " $cl->{client_id} : @{$cl->{can_do_list}}\n");
553  
554 }
555 $self->write(".\n");
556 }
557  
558 =head2 "status"
559  
560 The output format of this function is tab separated columns as follows, followed by a line consisting of a fullstop and a newline (".\n") to indicate the end of output.
561  
562 =over
563  
564 =item Function name
565  
566 A string denoting the name of the function of the job
567  
568 =item Number in queue
569  
570 A positive integer indicating the total number of jobs for this function in the queue. This includes currently running ones as well (next column)
571  
572 =item Number of jobs running
573  
574 A positive integer showing how many jobs of this function are currently running
575  
576 =item Number of capable workers
577  
578 A positive integer denoting the maximum possible count of workers that could be doing this job. Though they may not all be working on it due to other tasks holding them busy.
579  
580 =back
581  
582 =cut
583  
584 sub TXTCMD_status {
585 my Gearman::Server::Client $self = shift;
586  
587 my %funcs; # func -> 1 (set of all funcs to display)
588  
589 # keep track of how many workers can do which functions
590 my %can;
591 foreach my $client ($self->{server}->clients) {
592 foreach my $func (@{$client->{can_do_list}}) {
593 $can{$func}++;
594 $funcs{$func} = 1;
595 }
596 }
597  
598 my %queued_funcs;
599 my %running_funcs;
600  
601 foreach my $job ($self->{server}->jobs) {
602 my $func = $job->func;
603 $queued_funcs{$func}++;
604 if ($job->worker) {
605 $running_funcs{$func}++;
606 }
607 }
608  
609 # also include queued functions (even if there aren't workers)
610 # in our list of funcs to show.
611 $funcs{$_} = 1 foreach keys %queued_funcs;
612  
613 foreach my $func (sort keys %funcs) {
614 my $queued = $queued_funcs{$func} || 0;
615 my $running = $running_funcs{$func} || 0;
616 my $can = $can{$func} || 0;
617 $self->write( "$func\t$queued\t$running\t$can\n" );
618 }
619  
620 $self->write( ".\n" );
621 }
622  
623 =head2 "jobs"
624  
625 Output format is zero or more lines of:
626  
627 [Job function name]\t[Uniq (coalescing) key]\t[Worker address]\t[Number of listeners]\n
628  
629 Follows by a single line of:
630  
631 .\n
632  
633 \t is a literal tab character
634 \n is perl's definition of newline (literal \n on linux, something else on win32)
635  
636 =cut
637  
638 sub TXTCMD_jobs {
639 my Gearman::Server::Client $self = shift;
640  
641 foreach my $job ($self->{server}->jobs) {
642 my $func = $job->func;
643 my $uniq = $job->uniq;
644 my $worker_addr = "-";
645  
646 if (my $worker = $job->worker) {
647 $worker_addr = $worker->peer_addr_string;
648 }
649  
650 my $listeners = $job->listeners;
651  
652 $self->write("$func\t$uniq\t$worker_addr\t$listeners\n");
653 }
654  
655 $self->write(".\n");
656 }
657  
658 =head2 "clients"
659  
660 Output format is zero or more sections of:
661  
662 =over
663  
664 One line of:
665  
666 [Client Address]\n
667  
668 Followed by zero or more lines of:
669  
670 \t[Job Function]\t[Uniq (coalescing) key]\t[Worker Address]\n
671  
672 =back
673  
674 Follows by a single line of:
675  
676 .\n
677  
678 \t is a literal tab character
679 \n is perl's definition of newline (literal \n on linux, something else on win32)
680  
681 =cut
682  
683 sub TXTCMD_clients {
684 my Gearman::Server::Client $self = shift;
685  
686 my %jobs_by_client;
687  
688 foreach my $job ($self->{server}->jobs) {
689 foreach my $client ($job->listeners) {
690 my $ent = $jobs_by_client{$client} ||= [];
691 push @$ent, $job;
692 }
693 }
694  
695 foreach my $client ($self->{server}->clients) {
696 my $client_addr = $client->peer_addr_string;
697 $self->write("$client_addr\n");
698 my $jobs = $jobs_by_client{$client} || [];
699  
700 foreach my $job (@$jobs) {
701 my $func = $job->func;
702 my $uniq = $job->uniq;
703 my $worker_addr = "-";
704  
705 if (my $worker = $job->worker) {
706 $worker_addr = $worker->peer_addr_string;
707 }
708 $self->write("\t$func\t$uniq\t$worker_addr\n");
709 }
710  
711 }
712  
713 $self->write(".\n");
714 }
715  
716 sub TXTCMD_gladiator {
717 my Gearman::Server::Client $self = shift;
718 my $args = shift || "";
719 my $has_gladiator = eval "use Devel::Gladiator; use Devel::Peek; 1;";
720 if ($has_gladiator) {
721 my $all = Devel::Gladiator::walk_arena();
722 my %ct;
723 foreach my $it (@$all) {
724 $ct{ref $it}++;
725 if (ref $it eq "CODE") {
726 my $name = Devel::Peek::CvGV($it);
727 $ct{$name}++ if $name =~ /ANON/;
728 }
729 }
730 $all = undef; # required to free memory
731 foreach my $n (sort { $ct{$a} <=> $ct{$b} } keys %ct) {
732 next unless $ct{$n} > 1 || $args eq "all";
733 $self->write(sprintf("%7d $n\n", $ct{$n}));
734 }
735 }
736 $self->write(".\n");
737 }
738  
739 =head2 "maxqueue" function [max_queue_size]
740  
741 For a given function of job, the maximum queue size is adjusted to be max_queue_size jobs long. A negative value indicates unlimited queue size.
742  
743 If the max_queue_size value is not supplied then it is unset (and the default maximum queue size will apply to this function).
744  
745 This function will return OK upon success, and will return ERR incomplete_args upon an invalid number of arguments.
746  
747 =cut
748  
749 sub TXTCMD_maxqueue {
750 my Gearman::Server::Client $self = shift;
751 my $args = shift;
752 my ($func, $max) = split /\s+/, $args;
753  
754 unless (length $func) {
755 return $self->err_line('incomplete_args');
756 }
757  
758 $self->{server}->set_max_queue($func, $max);
759 $self->write("OK\n");
760 }
761  
762 =head2 "shutdown" ["graceful"]
763  
764 Close the server. Or "shutdown graceful" to close the listening socket, then close the server when traffic has died away.
765  
766 =cut
767  
768 sub TXTCMD_shutdown {
769 my Gearman::Server::Client $self = shift;
770 my $args = shift;
771 if ($args eq "graceful") {
772 $self->write("OK\n");
773 Gearmand::shutdown_graceful();
774 } elsif (! $args) {
775 $self->write("OK\n");
776 exit 0;
777 } else {
778 $self->err_line('unknown_args');
779 }
780 }
781  
782 =head2 "version"
783  
784 Returns server version.
785  
786 =cut
787  
788 sub TXTCMD_version {
789 my Gearman::Server::Client $self = shift;
790 $self->write("$Gearman::Server::VERSION\n");
791 }
792  
793 sub err_line {
794 my Gearman::Server::Client $self = shift;
795 my $err_code = shift;
796 my $err_text = {
797 'unknown_command' => "Unknown server command",
798 'unknown_args' => "Unknown arguments to server command",
799 'incomplete_args' => "An incomplete set of arguments was sent to this command",
800 }->{$err_code};
801  
802 $self->write("ERR $err_code " . eurl($err_text) . "\r\n");
803 return 0;
804 }
805  
806 sub eurl {
807 my $a = $_[0];
808 $a =~ s/([^a-zA-Z0-9_\,\-.\/\\\: ])/uc sprintf("%%%02x",ord($1))/eg;
809 $a =~ tr/ /+/;
810 return $a;
811 }
812  
813 1;