lib/Gearman/Driver.pm

filter: Statementstmt 90.3% 197 / 218 Branchbran 59.2% 32 / 54 Conditioncond 75.6% 31 / 41 MC/DCmcdc 54.1% 13 / 24 Subroutinesub 91.4% 32 / 35 Podpod 66.6% 4 / 6 totaltotal 81.7% 309 / 378 CC 66 SCAR 25.0
CC
66
Cov
83%
_daemonize
42.0
_observer_callback
21.8
_add_jobs
19.6
CRAP 12.1
« lib/Gearman/Driver/Console/Client.pm ↑ Summary lib/Gearman/Driver/Worker/AttributeParser.pm »
1 package Gearman::Driver;
2  
3 28 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 28 ▶use Moose::Util qw(apply_all_roles);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 28 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 28 ▶use Gearman::Driver::Observer;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 28 ▶use Gearman::Driver::Console;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 28 ▶use Gearman::Driver::Job;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 28 ▶use Gearman::Driver::Job::Method;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 28 ▶use Log::Log4perl qw(:easy);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 28 ▶use MooseX::Types::Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 28 ▶use POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 28 with qw(MooseX::Log::Log4perl MooseX::SimpleConfig MooseX::Getopt Gearman::Driver::Loader);
14  
15 28 our $VERSION = '0.02008';
16  
17 =head1 NAME
18  
19 Gearman::Driver - Manages Gearman workers
20  
21 =head1 SYNOPSIS
22  
23 package My::Workers::One;
24  
25 # Yes, you need to do it exactly this way
26 use base qw(Gearman::Driver::Worker);
27 use Moose;
28  
29 # this method will be registered with gearmand as 'My::Workers::One::scale_image'
30 sub scale_image : Job {
31 my ( $self, $job, $workload ) = @_;
32 # do something
33 }
34  
35 # this method will be registered with gearmand as 'My::Workers::One::do_something_else'
36 sub do_something_else : Job : MinProcesses(2) : MaxProcesses(15) {
37 my ( $self, $job, $workload ) = @_;
38 # do something
39 }
40  
41 # this method wont be registered with gearmand at all
42 sub do_something_internal {
43 my ( $self, $job, $workload ) = @_;
44 # do something
45 }
46  
47 1;
48  
49 package My::Workers::Two;
50  
51 use base qw(Gearman::Driver::Worker);
52 use Moose;
53  
54 # this method will be registered with gearmand as 'My::Workers::Two::scale_image'
55 sub scale_image : Job {
56 my ( $self, $job, $workload ) = @_;
57 # do something
58 }
59  
60 1;
61  
62 package main;
63  
64 use Gearman::Driver;
65  
66 my $driver = Gearman::Driver->new(
67 namespaces => [qw(My::Workers)],
68 server => 'localhost:4730,otherhost:4731',
69 interval => 60,
70 );
71  
72 #or should save all config into a YAML config file, then read config from it.
73 my $driver = Gearman::Driver->new(configfile => '/etc/gearman-driver/config.yml');
74  
75 $driver->run;
76  
77 =head1 DESCRIPTION
78  
79 Warning: This framework is still B<EXPERIMENTAL>!
80  
81 Having hundreds of Gearman workers running in separate processes can
82 consume a lot of RAM. Often many of these workers share the same
83 code/objects, like the database layer using L<DBIx::Class> for
84 example. This is where L<Gearman::Driver> comes in handy:
85  
86 You write some base class which inherits from
87 L<Gearman::Driver::Worker>. Your base class loads your database layer
88 for example. Each of your worker classes inherit from that base
89 class. In the worker classes you can register single methods as jobs
90 with gearmand. It's even possible to control how many workers doing
91 that job/method in parallel. And this is the point where you'll
92 save some RAM: Instead of starting each worker in a separate process
93 L<Gearman::Driver> will fork each worker from the main process. This
94 will take advantage of copy-on-write on Linux and save some RAM.
95  
96 There's only one mandatory parameter which has to be set when calling
97 the constructor: namespaces
98  
99 use Gearman::Driver;
100 my $driver = Gearman::Driver->new( namespaces => [qw(My::Workers)] );
101  
102 See also: L<namespaces|/namespaces>. If you do not set
103 L<server|/server> (gearmand) attribute the default will be used:
104 C<localhost:4730>
105  
106 Each module found in your namespaces will be loaded and introspected,
107 looking for methods having the 'Job' attribute set:
108  
109 package My::Workers::ONE;
110  
111 sub scale_image : Job {
112 my ( $self, $job, $workload ) = @_;
113 # do something
114 }
115  
116 This method will be registered as job function with gearmand, verify
117 it by doing:
118  
119 plu@mbp ~$ telnet localhost 4730
120 Trying ::1...
121 Connected to localhost.
122 Escape character is '^]'.
123 status
124 My::Workers::ONE::scale_image 0 0 1
125 .
126 ^]
127 telnet> Connection closed.
128  
129 If you dont like to use the full package name you can also specify
130 a custom prefix:
131  
132 package My::Workers::ONE;
133  
134 sub prefix { 'foo_bar_' }
135  
136 sub scale_image : Job {
137 my ( $self, $job, $workload ) = @_;
138 # do something
139 }
140  
141 This would register 'foo_bar_scale_image' with gearmand.
142  
143 See also: L<prefix|Gearman::Driver::Worker/prefix>
144  
145 =head1 ATTRIBUTES
146  
147 See also L<Gearman::Driver::Loader/ATTRIBUTES>.
148  
149 =head2 server
150  
151 A list of Gearman servers the workers should connect to. The format
152 for the server list is: C<host[:port][,host[:port]]>
153  
154 See also: L<Gearman::XS>
155  
156 =over 4
157  
158 =item * default: C<localhost:4730>
159  
160 =item * isa: C<Str>
161  
162 =back
163  
164 =cut
165  
166 28 has 'server' => (
167 default => 'localhost:4730',
168 documentation => 'Gearman host[:port][,host[:port]]',
169 is => 'rw',
170 isa => 'Str',
171 required => 1,
172 );
173  
174 =head2 console_port
175  
176 Gearman::Driver has a telnet management console, see also:
177  
178 L<Gearman::Driver::Console>
179  
180 =over 4
181  
182 =item * default: C<47300>
183  
184 =item * isa: C<Int>
185  
186 =back
187  
188 Set this to C<0> to disable management console at all.
189  
190 =cut
191  
192 28 has 'console_port' => (
193 default => 47300,
194 documentation => 'Port of management console (default: 47300)',
195 is => 'rw',
196 isa => 'Int',
197 required => 1,
198 );
199  
200 =head2 interval
201  
202 Each n seconds L<Net::Telnet::Gearman> is used in
203 L<Gearman::Driver::Observer> to check status of free/running/busy
204 workers on gearmand. This is used to fork more workers depending
205 on the queue size and the MinProcesses/MaxProcesses
206 L<attribute|Gearman::Driver::Worker/METHODATTRIBUTES> of the
207 job method. See also: L<Gearman::Driver::Worker>
208  
209 =over 4
210  
211 =item * default: C<5>
212  
213 =item * isa: C<Int>
214  
215 =back
216  
217 =cut
218  
219 28 has 'interval' => (
220 default => '5',
221 documentation => 'Interval in seconds (see Gearman::Driver::Observer)',
222 is => 'rw',
223 isa => 'Int',
224 required => 1,
225 );
226  
227 =head2 max_idle_time
228  
229 Whenever L<Gearman::Driver::Observer> notices that there are more
230 processes running than actually necessary (depending on min_processes
231 and max_processes setting) it will kill them. By default this happens
232 immediately. If you change this value to C<300>, a process which is
233 not necessary is killed after 300 seconds.
234  
235 Please remember that this also depends on what value you set
236 L</interval> to. The max_idle_time is only checked each n seconds
237 where n is L</interval>. Besides that it makes only sense when you
238 have workers where L<Gearman::Driver::Worker/MinProcesses> is set to
239 C<0>.
240  
241 =over 4
242  
243 =item * default: C<0>
244  
245 =item * isa: C<Int>
246  
247 =back
248  
249 =cut
250  
251 28 has 'max_idle_time' => (
252 default => '0',
253 documentation => 'How many seconds a worker may be idle before its killed',
254 is => 'rw',
255 isa => 'Int',
256 required => 1,
257 );
258  
259 =head2 logfile
260  
261 Path to logfile.
262  
263 =over 4
264  
265 =item * isa: C<Str>
266  
267 =item * default: C<gearman_driver.log>
268  
269 =back
270  
271 =cut
272  
273 28 has 'logfile' => (
274 coerce => 1,
275 default => 'gearman_driver.log',
276 documentation => 'Path to logfile (default: gearman_driver.log)',
277 is => 'rw',
278 isa => 'Path::Class::File',
279 );
280  
281 =head2 loglayout
282  
283 See also L<Log::Log4perl>.
284  
285 =over 4
286  
287 =item * isa: C<Str>
288  
289 =item * default: C<[%d] %p %m%n>
290  
291 =back
292  
293 =cut
294  
295 28 has 'loglayout' => (
296 default => '[%d] %p %m%n',
297 documentation => 'Log message layout (default: [%d] %p %m%n)',
298 is => 'rw',
299 isa => 'Str',
300 );
301  
302 =head2 loglevel
303  
304 See also L<Log::Log4perl>.
305  
306 =over 4
307  
308 =item * isa: C<Str>
309  
310 =item * default: C<INFO>
311  
312 =back
313  
314 =cut
315  
316 28 has 'loglevel' => (
317 default => 'INFO',
318 documentation => 'Log level (default: INFO)',
319 is => 'rw',
320 isa => 'Str',
321 );
322  
323 =head2 unknown_job_callback
324  
325 Whenever L<Gearman::Driver::Observer> sees a job that isnt handled
326 it will call this CodeRef, passing following arguments:
327  
328 =over 4
329  
330 =item * C<$driver>
331  
332 =item * C<$status>
333  
334 =back
335  
336 my $driver = Gearman::Driver->new(
337 namespaces => [qw(My::Workers)],
338 unknown_job_callback => sub {
339 my ( $driver, $status ) = @_;
340 # notify nagios here for example
341 }
342 );
343  
344 C<$status> might look like:
345  
346 $VAR1 = {
347 'busy' => 0,
348 'free' => 0,
349 'name' => 'GDExamples::Convert::unknown_job',
350 'queue' => 6,
351 'running' => 0
352 };
353  
354 =cut
355  
356 has 'unknown_job_callback' => (
357 13 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
358 0 ▶ sub { }
Subroutine __ANON__: not called CC=1 SCAR=0.0
359 13 },
360 28 is => 'rw',
361 isa => 'CodeRef',
362 traits => [qw(NoGetopt)],
363 );
364  
365 =head2 worker_options
366  
367 You can pass runtime options to the worker module, these will merged with 'GLOBAL' and pass to the worker constructor. ( worker options override globals )
368  
369 =over 4
370  
371 =item * default: C<{}>
372  
373 =item * isa: C<HashRef>
374  
375 =back
376  
377 Example:
378  
379 my $driver = Gearman::Driver->new(
380 namespaces => [qw(My::Workers)],
381 worker_options => {
382 'GLOBAL' => {
383 'config' => $config,
384 },
385 'My::Workers::MysqlPing' => {
386 'dsn' => 'DBI:mysql:database=test;host=localhost;mysql_auto_reconnect=1;mysql_enable_utf8=1;mysql_server_prepare=1;',
387 },
388 'My::Workers::ImageThumbnail' => {
389 'default_format' => 'jpeg',
390 'default_size => ' 133 x 100 ',
391 }
392 }
393 );
394  
395 You should define these in a runtime config (See also L</configfile>), might be:
396  
397 ---
398 worker_options:
399 'My::App::Worker::MysqlPing':
400 'dsn': 'DBI:mysql:database=test;host=localhost;mysql_auto_reconnect=1;mysql_enable_utf8=1;mysql_server_prepare=1;'
401 'user': 'root'
402 'password:': ''
403 'My::App::Worker::ImageThumbnail':
404 'default_format': 'jpeg'
405 'default_size': '133x100'
406  
407 =cut
408  
409 has 'worker_options' => (
410 isa => 'HashRef',
411 is => 'rw',
412 12 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
413 28 traits => [qw(Hash NoGetopt)],
414 );
415  
416 =head2 Job runtime attributes
417  
418 You can override a job attribute by its name here. This help to tuning job some runtime-related options (like max_processes, min_processes) handy.
419 You just change the options in a config file, no need to modify the worker code anymore.
420  
421 Currently only 'max_processes', 'min_processes' make sense. The hash key is "worker_module::job_key", job_key is ProcessGroup attribute or
422 job method name.
423  
424 #in your config file: /etc/gearman-driver.yml (YAML)
425 ---
426 job_runtime_attributes:
427 'My::App::Worker::job1':
428 max_processes: 25
429 min_processes: 2
430 #job has a ProcessGroup attribute named 'group1'
431 'My::App::Worker::group1':
432 max_processes: 10
433 min_processes: 2
434 #then run as:
435 gearman_driver.pl --configfile /etc/gearman_driver.yml
436  
437 =cut
438  
439 has 'job_runtime_attributes' => (
440 isa => 'HashRef',
441 is => 'rw',
442 12 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
443 28 traits => [qw(Hash NoGetopt)],
444 );
445  
446 =head2 configfile
447  
448 Runtime config file path, You can provide a default configfile pathname like so:
449  
450 has +configfile ( default => '/etc/gearman-driver.yaml' );
451  
452 You can pass an array of filenames if you want, like:
453  
454 has +configfile ( default => sub { [ '/etc/gearman-driver.yaml','/opt/my-app/etc/config.yml' ] });
455  
456 =cut
457  
458 28 has '+configfile' => (
459 documentation => 'Gearman-driver runtime config path',
460 );
461  
462 =head2 daemonize
463  
464 Detach self and run as a daemon.
465  
466 =cut
467  
468 28 has 'daemonize' => (
469 isa => 'Bool',
470 is => 'rw',
471 default => 0,
472 documentation => 'Let Gearman-driver run as a daemon'
473 );
474  
475 =head1 INTERNAL ATTRIBUTES
476  
477 This might be interesting for subclassing L<Gearman::Driver>.
478  
479 =head2 jobs
480  
481 Stores all L<Gearman::Driver::Job> instances. There are also two
482 methods:
483  
484 =over 4
485  
486 =item * L<get_job|Gearman::Driver/get_job>
487  
488 =item * L<has_job|Gearman::Driver/has_job>
489  
490 =back
491  
492 Example:
493  
494 {
495 'My::Workers::ONE::scale_image' => bless( {...}, 'Gearman::Driver::Job' ),
496 'My::Workers::ONE::do_something_else' => bless( {...}, 'Gearman::Driver::Job' ),
497 'My::Workers::TWO::scale_image' => bless( {...}, 'Gearman::Driver::Job' ),
498 }
499  
500 =over 4
501  
502 =item * isa: C<HashRef>
503  
504 =item * readonly: C<True>
505  
506 =back
507  
508 =cut
509  
510 has 'jobs' => (
511 13 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
512 28 handles => {
513 _set_job => 'set',
514 get_job => 'get',
515 has_job => 'defined',
516 all_jobs => 'values',
517 },
518 is => 'ro',
519 isa => 'HashRef',
520 traits => [qw(Hash NoGetopt)],
521 );
522  
523 =head2 observer
524  
525 Instance of L<Gearman::Driver::Observer>.
526  
527 =over 4
528  
529 =item * isa: C<Gearman::Driver::Observer>
530  
531 =item * readonly: C<True>
532  
533 =back
534  
535 =cut
536  
537 28 has 'observer' => (
538 is => 'ro',
539 isa => 'Gearman::Driver::Observer',
540 traits => [qw(NoGetopt)],
541 );
542  
543 =head2 console
544  
545 Instance of L<Gearman::Driver::Console>.
546  
547 =over 4
548  
549 =item * isa: C<Gearman::Driver::Console>
550  
551 =item * readonly: C<True>
552  
553 =back
554  
555 =cut
556  
557 28 has 'console' => (
558 is => 'ro',
559 isa => 'Gearman::Driver::Console',
560 traits => [qw(NoGetopt)],
561 );
562  
563 28 has 'session' => (
564 is => 'ro',
565 isa => 'POE::Session',
566 traits => [qw(NoGetopt)],
567 );
568  
569 28 has 'pid' => (
570 default => $$,
571 is => 'ro',
572 isa => 'Int',
573 );
574  
575  
576 28 has '+logger' => ( traits => [qw(NoGetopt)] );
577 28 has '+wanted' => ( traits => [qw(NoGetopt)] );
578 28 has '+modules' => ( traits => [qw(NoGetopt)] );
579  
580 =head1 METHODS
581  
582 =head2 add_job
583  
584 There's one mandatory param (hashref) with following keys:
585  
586 =over 4
587  
588 =item * max_processes (mandatory)
589  
590 Maximum number of processes that may be forked.
591  
592 =item * min_processes (mandatory)
593  
594 Minimum number of processes that should be forked.
595  
596 =item * name (mandatory)
597  
598 Job name/alias that method should be registered with Gearman.
599  
600 =item * methods (mandatory)
601  
602 ArrayRef of HashRefs containing following keys:
603  
604 =over 4
605  
606 =item * body (mandatory)
607  
608 CodeRef to the job method.
609  
610 =item * name (mandatory)
611  
612 The name this method should be registered with gearmand.
613  
614 =item * decode (optionally)
615  
616 Name of a decoder method in your worker object.
617  
618 =item * encode (optionally)
619  
620 Name of a encoder method in your worker object.
621  
622 =back
623  
624 =item * worker (mandatory)
625  
626 Worker object that should be passed as first parameter to the job
627 method.
628  
629 =back
630  
631 Basically you never really need this method if you use
632 L</namespaces>. But L</namespaces> depends on method attributes which
633 some people do hate. In this case, feel free to setup your C<$driver>
634 this way:
635  
636 package My::Workers::One;
637  
638 use Moose;
639 use JSON::XS;
640 extends 'Gearman::Driver::Worker::Base';
641  
642 # this method will be registered with gearmand as 'My::Workers::One::scale_image'
643 sub scale_image {
644 my ( $self, $job, $workload ) = @_;
645 # do something
646 }
647  
648 # this method will be registered with gearmand as 'My::Workers::One::do_something_else'
649 sub do_something_else {
650 my ( $self, $job, $workload ) = @_;
651 # do something
652 }
653  
654 sub encode_json {
655 my ( $self, $result ) = @_;
656 return JSON::XS::encode_json($result);
657 }
658  
659 sub decode_json {
660 my ( $self, $workload ) = @_;
661 return JSON::XS::decode_json($workload);
662 }
663  
664 1;
665  
666 package main;
667  
668 use Gearman::Driver;
669 use My::Workers::One;
670  
671 my $driver = Gearman::Driver->new(
672 server => 'localhost:4730,otherhost:4731',
673 interval => 60,
674 );
675  
676 my $worker = My::Workers::One->new();
677  
678 # run each method in an own process
679 foreach my $method (qw(scale_image do_something_else)) {
680 $driver->add_job(
681 {
682 max_processes => 5,
683 min_processes => 1,
684 name => $method,
685 worker => $worker,
686 methods => [
687 {
688 body => $w1->meta->find_method_by_name($method)->body,
689 decode => 'decode_json',
690 encode => 'encode_json',
691 name => $method,
692 },
693 ]
694 }
695 );
696 }
697  
698 # share both methods in a single process
699 $driver->add_job(
700 {
701 max_processes => 5,
702 min_processes => 1,
703 name => 'some_alias',
704 worker => $worker,
705 methods => [
706 {
707 body => $w1->meta->find_method_by_name('scale_image')->body,
708 decode => 'decode_json',
709 encode => 'encode_json',
710 name => 'scale_image',
711 },
712 {
713 body => $w1->meta->find_method_by_name('do_something_else')->body,
714 decode => 'decode_json',
715 encode => 'encode_json',
716 name => 'do_something_else',
717 },
718 ]
719 }
720 );
721  
722 $driver->run;
723  
724 =cut
725  
726 18 ▶sub add_job {
Subroutine add_job: called CC=7 SCAR=19.5
Pod documented
727 18 my ( $self, $params ) = @_;
728  
729 18 $params->{name} = $params->{worker}->prefix . $params->{name};
730  
731 18 foreach my $key ( keys %$params ) {
732 90 ▶ delete $params->{$key} unless defined $params->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params->{$key} 26 64 90
733 }
734  
735 18 my @methods = ();
736 18 foreach my $args ( @{ delete $params->{methods} } ) {
737 38 foreach my $key ( keys %$args ) {
738 152 ▶ delete $args->{$key} unless defined $args->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{$key} 56 96 152
739 }
740 38 $args->{name} = $params->{worker}->prefix . $args->{name};
741 38 push @methods, Gearman::Driver::Job::Method->new( %$args, worker => $params->{worker} );
742 }
743  
744 18 my $job = Gearman::Driver::Job->new(
745 driver => $self,
746 methods => \@methods,
747 %$params
748 );
749  
750 18 $self->_set_job( $params->{name} => $job );
751  
752 18 ▶ $self->log->debug( sprintf "Added new job: %s (processes: %d)", $params->{name}, $params->{min_processes} || 1 );
Condition100%2/2
$params->{'min_processes'} || 1
l!l
216
MC/DC100%1/1
$params->{'min_processes'} || 1
$params->{'min_processes'}
Truth table100%2/2
A
A= $params->{'min_processes'}
Aresult
11
00
753  
754 18 return 1;
755 }
756  
757 =head2 get_jobs
758  
759 Returns all L<Gearman::Driver::Job> objects ordered by jobname.
760  
761 =cut
762  
763 37 ▶sub get_jobs {
Subroutine get_jobs: called CC=2 SCAR=6.9
Pod documented
764 37 my ($self) = @_;
765 37 my @result = ();
766 37 foreach my $name ( sort keys %{ $self->jobs } ) {
767 57 push @result, $self->get_job($name);
768 }
769 37 return @result;
770 }
771  
772 =head2 run
773  
774 This must be called after the L<Gearman::Driver> object is instantiated.
775  
776 =cut
777  
778 13 ▶sub run {
Subroutine run: called CC=2 SCAR=6.9
Pod documented
779 13 my ($self) = @_;
780 13 push @INC, @{ $self->lib };
781 13 $self->load_namespaces;
782  
783 13 ▶ $self->_daemonize if $self->daemonize;
Branch: 1 branch
BranchTrueFalseTotal
if $self->daemonize 0 13 13
784  
785 13 $self->_start_observer;
786 13 $self->_start_console;
787 13 $self->_start_session;
788 13 POE::Kernel->run();
789 }
790  
791 =head2 shutdown
792  
793 Sends TERM signal to all child processes and exits Gearman::Driver.
794  
795 =cut
796  
797 12 ▶sub shutdown {
Subroutine shutdown: called CC=1 SCAR=0.0
Pod documented
798 12 my ($self) = @_;
799 12 POE::Kernel->signal( $self->{session}, 'TERM' );
800 }
801  
802 0 ▶sub DEMOLISH {
Subroutine DEMOLISH: not called CC=2 SCAR=17.9
Pod undocumented
803 0 my ($self) = @_;
804 0 ▶ if ( $self->pid eq $$ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->pid eq $$) 0 0 0
805 0 $self->shutdown;
806 }
807 }
808  
809 =head2 has_job
810  
811 Params: $name
812  
813 Returns true/false if the job exists.
814  
815 =head2 get_job
816  
817 Params: $name
818  
819 Returns the job instance.
820  
821 =cut
822  
823 13 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
824 13 my ($self) = @_;
825 13 $self->_setup_logger;
826 }
827  
828 13 ▶sub _setup_logger {
Subroutine _setup_logger: called CC=2 SCAR=7.1
829 13 my ($self) = @_;
830  
831 13 ▶ unless (Log::Log4perl->initialized()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("Log::Log4perl"->initialized) 13 0 13
832 13 Log::Log4perl->easy_init(
833 {
834 file => sprintf( '>>%s', $self->logfile ),
835 layout => $self->loglayout,
836 level => $self->loglevel,
837 },
838 );
839 }
840 }
841  
842 13 ▶sub _start_observer {
Subroutine _start_observer: called CC=2 SCAR=7.0
843 13 my ($self) = @_;
844 13 ▶ if ( $self->interval > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->interval > 0) 13 0 13
845 $self->{observer} = Gearman::Driver::Observer->new(
846 39 ▶ callback => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
847 39 my ($response) = @_;
848 39 $self->_observer_callback($response);
849 },
850 13 interval => $self->interval,
851 server => $self->server,
852 );
853 }
854 }
855  
856 13 ▶sub _start_console {
Subroutine _start_console: called CC=2 SCAR=7.1
857 13 my ($self) = @_;
858 13 ▶ if ( $self->console_port > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->console_port > 0) 13 0 13
859 13 $self->{console} = Gearman::Driver::Console->new(
860 driver => $self,
861 port => $self->console_port,
862 );
863 }
864 }
865  
866 39 ▶sub _observer_callback {
Subroutine _observer_callback: called CC=20 SCAR=30.8
867 39 my ( $self, $response ) = @_;
868  
869 # When $job->add_process is called and ProcessGroup is used
870 # this may end up in a race condition and more processes than
871 # wanted are started. To fix that we remember what kind of
872 # processes we need to start in each single run of this callback.
873 39 my %to_start = ();
874  
875 39 my $status = $response->{data};
876 39 foreach my $row (@$status) {
877 70 ▶ if ( my $job = $self->_find_job( $row->{name} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $job = $self->_find_job($row->{'name'})) { } 70 0 70
878 70 ▶ $to_start{$job->name} ||= 0;
Condition50%1/2
$to_start{$job->name} ||= 0
l!l
070
MC/DC0%0/1
$to_start{$job->name} ||= 0
$to_start{$job->name}
Truth table50%1/2
A
A= $to_start{$job->name}
Aresult
11
00
879 70 ▶ if ( $job->count_processes <= $row->{busy} && $row->{queue} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($job->count_processes <= $row->{'busy'} and $row->{'queue'}) { } 7 63 70
elsif ($job->count_processes and $job->count_processes > $job->min_processes and $row->{'queue'} == 0) { } 10 106 116
Condition100%3/3
$job->count_processes <= $row->{"busy"} and $row->{"queue"}
!ll&&!rl&&r
28357
MC/DC100%2/2
$job->count_processes <= $row->{"busy"} and $row->{"queue"}
$job->count_processes <= $row->{"busy"} $row->{"queue"}
Truth table100%3/3
A and B
A= $job->count_processes <= $row->{"busy"}B= $row->{"queue"}
ABresult
0X0
100
111
880 7 my $diff = $row->{queue} - $row->{busy};
881 7 my $free = $job->max_processes - $job->count_processes;
882 7 ▶ if ($free) {
Branch: 1 branch
BranchTrueFalseTotal
if ($free) 5 2 7
883 5 ▶ my $start = $diff > $free ? $free : $diff;
Branch: 1 branch
BranchTrueFalseTotal
$diff > $free ? : 0 5 5
884 5 $to_start{$job->name} += $start;
885 }
886 }
887  
888 ▶ elsif ( $job->count_processes && $job->count_processes > $job->min_processes && $row->{queue} == 0 ) {
Condition83%5/6
$job->count_processes and $job->count_processes > $job->min_processes and $row->{"queue"} == 0
!ll&&!rl&&r
53010
$job->count_processes and $job->count_processes > $job->min_processes
!ll&&!rl&&r
351810
MC/DC66%2/3
$job->count_processes and $job->count_processes > $job->min_processes and $row->{"queue"} == 0
$job->count_processes $job->count_processes > $job->min_processes $row->{"queue"} == 0
Truth table75%3/4
A and B and C
A= $job->count_processesB= $job->count_processes > $job->min_processesC= $row->{"queue"} == 0
ABCresult
0XX0
10X0
1100
1111
889 10 my $idle = time - $job->lastrun;
890 10 ▶ if ( $job->lastrun && ($idle >= $self->max_idle_time) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($job->lastrun and $idle >= $self->max_idle_time) 1 9 10
Condition66%2/3
$job->lastrun and $idle >= $self->max_idle_time
!ll&&!rl&&r
091
MC/DC50%1/2
$job->lastrun and $idle >= $self->max_idle_time
$job->lastrun $idle >= $self->max_idle_time
Truth table66%2/3
A and B
A= $job->lastrunB= $idle >= $self->max_idle_time
ABresult
0X0
100
111
891 1 my $stop = $job->count_processes - $job->min_processes;
892 1 $self->log->debug( sprintf "Stopping %d process(es) of type %s (idle: %d)",
893 $stop, $job->name, $idle );
894 1 $job->remove_process for 1 .. $stop;
895 }
896 }
897 }
898 else {
899 0 ▶ $self->unknown_job_callback->( $self, $row ) if $row->{queue} > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $row->{"queue"} > 0 0 0 0
900 }
901 }
902  
903 39 foreach my $name (keys %to_start) {
904 61 my $job = $self->get_job($name);
905 61 my $start = $to_start{$name};
906 61 my $free = $job->max_processes - $job->count_processes;
907 61 ▶ $start = $free if $start > $free;
Branch: 1 branch
BranchTrueFalseTotal
if $start > $free 0 61 61
908 61 ▶ if ($start) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start) 5 56 61
909 5 $self->log->debug( sprintf "Starting %d new process(es) of type %s", $start, $job->name );
910 5 $job->add_process for 1 .. $start;
911 }
912 }
913  
914 39 my $error = $response->{error};
915 39 foreach my $e (@$error) {
916 0 $self->log->error( sprintf "Gearman::Driver::Observer: %s", $e );
917 }
918 }
919  
920 70 ▶sub _find_job {
Subroutine _find_job: called CC=4 SCAR=13.9
921 70 my ( $self, $name ) = @_;
922 70 foreach my $job ( $self->all_jobs ) {
923 92 foreach my $method ( @{ $job->methods } ) {
924 116 ▶ return $job if $method->name eq $name;
Branch: 1 branch
BranchTrueFalseTotal
if $method->name eq $name 70 46 116
925 }
926 }
927 0 return 0;
928 }
929  
930 13 ▶sub _start_session {
Subroutine _start_session: called CC=1 SCAR=0.0
931 13 my ($self) = @_;
932 13 $self->{session} = POE::Session->create(
933 object_states => [
934 $self => {
935 _start => '_start',
936 got_sig => '_on_sig',
937 monitor_processes => '_monitor_processes',
938 }
939 ]
940 );
941 }
942  
943 12 ▶sub _on_sig {
Subroutine _on_sig: called CC=3 SCAR=11.0
944 12 my ( $self, $kernel, $heap ) = @_[ OBJECT, KERNEL, HEAP ];
945  
946 12 foreach my $job ( $self->get_jobs ) {
947 17 foreach my $process ( $job->get_processes ) {
948 18 $self->log->info( sprintf '(%d) [%s] Process killed', $process->PID, $job->name );
949 18 $process->kill();
950 }
951 }
952  
953 12 $kernel->sig_handled();
954  
955 12 exit(0);
956 }
957  
958 13 ▶sub _start {
Subroutine _start: called CC=2 SCAR=6.9
959 13 $_[KERNEL]->sig( $_ => 'got_sig' ) for qw(INT QUIT ABRT KILL TERM);
960 13 $_[OBJECT]->_add_jobs;
961 13 $_[OBJECT]->_start_jobs;
962 13 $_[KERNEL]->delay( monitor_processes => 5 );
963 }
964  
965 13 ▶sub _add_jobs {
Subroutine _add_jobs: called CC=19 SCAR=29.7
966 13 my ($self) = @_;
967 13 my $worker_options = $self->worker_options;
968 13 my $job_runtime_attributes = $self->job_runtime_attributes;
969  
970 13 foreach my $module ( $self->get_modules ) {
971 my %module_options = (
972 13 ▶ %{ $worker_options->{GLOBAL} || {} },
Condition100%2/2
$worker_options->{'GLOBAL'} || {}
l!l
112
MC/DC100%1/1
$worker_options->{'GLOBAL'} || {}
$worker_options->{'GLOBAL'}
Truth table100%2/2
A
A= $worker_options->{'GLOBAL'}
Aresult
11
00
973 13 ▶ %{ $worker_options->{$module} || {} },
Condition100%2/2
$worker_options->{$module} || {}
l!l
112
MC/DC100%1/1
$worker_options->{$module} || {}
$worker_options->{$module}
Truth table100%2/2
A
A= $worker_options->{$module}
Aresult
11
00
974 );
975 13 $module_options{server} = $self->server;
976 13 my $worker = $module->new( %module_options );
977 13 my %methods = ();
978 13 foreach my $method ( $module->meta->get_nearest_methods_with_attributes ) {
979 38 apply_all_roles( $method => 'Gearman::Driver::Worker::AttributeParser' );
980  
981 38 $method->default_attributes( $worker->default_attributes );
982 38 $method->override_attributes( $worker->override_attributes );
983  
984 38 ▶ next unless $method->has_attribute('Job');
Branch: 1 branch
BranchTrueFalseTotal
unless $method->has_attribute("Job") 0 38 38
985  
986 38 ▶ my $name = $method->get_attribute('ProcessGroup') || $method->name;
Condition66%2/3
$method->get_attribute('ProcessGroup') || $method->name
l!l&&r!l&&!r
28100
MC/DC0%0/2
$method->get_attribute('ProcessGroup') || $method->name
$method->get_attribute('ProcessGroup') $method->name
Truth table66%2/3
A || B
A= $method->get_attribute('ProcessGroup')B= $method->name
ABresult
1X1
011
000
987 38 ▶ $methods{$name} ||= [];
Condition100%2/2
$methods{$name} ||= []
l!l
2018
MC/DC100%1/1
$methods{$name} ||= []
$methods{$name}
Truth table100%2/2
A
A= $methods{$name}
Aresult
11
00
988 38 push @{ $methods{$name} }, $method;
989 }
990  
991 13 foreach my $name ( keys %methods ) {
992 18 my @methods = ();
993 18 my ( $min_processes, $max_processes );
994  
995 18 foreach my $method ( @{ $methods{$name} } ) {
996 38 ▶ warn sprintf "MinProcesses redefined in ProcessGroup(%s) at %s::%s",
Branch: 1 branch
BranchTrueFalseTotal
if defined $min_processes and $method->has_attribute("MinProcesses") 0 38 38
Condition66%2/3
defined $min_processes and $method->has_attribute("MinProcesses")
!ll&&!rl&&r
3530
MC/DC0%0/2
defined $min_processes and $method->has_attribute("MinProcesses")
defined $min_processes $method->has_attribute("MinProcesses")
Truth table66%2/3
A and B
A= defined $min_processesB= $method->has_attribute("MinProcesses")
ABresult
0X0
100
111
997 $method->get_attribute('ProcessGroup'), ref($worker), $method->name
998 if defined $min_processes && $method->has_attribute('MinProcesses');
999  
1000 38 ▶ warn sprintf "MaxProcesses redefined in ProcessGroup(%s) at %s::%s",
Branch: 1 branch
BranchTrueFalseTotal
if defined $max_processes and $method->has_attribute("MaxProcesses") 0 38 38
Condition33%1/3
defined $max_processes and $method->has_attribute("MaxProcesses")
!ll&&!rl&&r
3800
MC/DC0%0/2
defined $max_processes and $method->has_attribute("MaxProcesses")
defined $max_processes $method->has_attribute("MaxProcesses")
Truth table33%1/3
A and B
A= defined $max_processesB= $method->has_attribute("MaxProcesses")
ABresult
0X0
100
111
1001 $method->get_attribute('ProcessGroup'), ref($worker), $method->name
1002 if defined $max_processes && $method->has_attribute('MaxProcesses');
1003  
1004 38 ▶ $min_processes ||= $method->get_attribute('MinProcesses');
Condition100%3/3
$min_processes ||= $method->get_attribute("MinProcesses")
l!l&&r!l&&!r
3134
MC/DC100%2/2
$min_processes ||= $method->get_attribute("MinProcesses")
$min_processes $method->get_attribute("MinProcesses")
Truth table100%3/3
A ||= B
A= $min_processesB= $method->get_attribute("MinProcesses")
ABresult
1X1
011
000
1005 38 ▶ $max_processes ||= $method->get_attribute('MaxProcesses');
Condition66%2/3
$max_processes ||= $method->get_attribute("MaxProcesses")
l!l&&r!l&&!r
0236
MC/DC50%1/2
$max_processes ||= $method->get_attribute("MaxProcesses")
$max_processes $method->get_attribute("MaxProcesses")
Truth table66%2/3
A ||= B
A= $max_processesB= $method->get_attribute("MaxProcesses")
ABresult
1X1
011
000
1006  
1007 38 push @methods,
1008 {
1009 body => $method->body,
1010 name => $method->name,
1011 decode => $method->get_attribute('Decode'),
1012 encode => $method->get_attribute('Encode'),
1013 };
1014 }
1015  
1016 18 ▶ my $job_runtime_attributes = $self->job_runtime_attributes->{$module.'::'.$name} || {};
Condition100%2/2
$self->job_runtime_attributes->{$module . '::' . $name} || {}
l!l
117
MC/DC100%1/1
$self->job_runtime_attributes->{$module . '::' . $name} || {}
$self->job_runtime_attributes->{$module . '::' . $name}
Truth table100%2/2
A
A= $self->job_runtime_attributes->{$module . '::' . $name}
Aresult
11
00
1017 18 ▶ if (defined $job_runtime_attributes->{min_processes} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $job_runtime_attributes->{"min_processes"}) 1 17 18
1018 1 $min_processes = $job_runtime_attributes->{min_processes} ;
1019 }
1020  
1021 18 ▶ if (defined $job_runtime_attributes->{max_processes}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $job_runtime_attributes->{"max_processes"}) 1 17 18
1022 1 $max_processes = $job_runtime_attributes->{max_processes};
1023 }
1024  
1025 $self->add_job(
1026 {
1027 18 max_processes => $max_processes,
1028 min_processes => $min_processes,
1029 methods => \@methods,
1030 name => $name,
1031 worker => $worker,
1032 }
1033 );
1034 }
1035 }
1036 }
1037  
1038 13 ▶sub _start_jobs {
Subroutine _start_jobs: called CC=3 SCAR=11.0
1039 13 my ($self) = @_;
1040  
1041 13 foreach my $job ( $self->get_jobs ) {
1042 18 for ( 1 .. $job->min_processes ) {
1043 14 $job->add_process();
1044 }
1045 }
1046 }
1047  
1048 5 ▶sub _monitor_processes {
Subroutine _monitor_processes: called CC=4 SCAR=13.9
1049 5 my $self = $_[OBJECT];
1050 5 foreach my $job ( $self->get_jobs ) {
1051 8 ▶ if ( $job->count_processes < $job->min_processes ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($job->count_processes < $job->min_processes) 3 5 8
1052 3 my $start = $job->min_processes - $job->count_processes;
1053 3 $self->log->debug( sprintf "Starting %d new process(es) of type %s", $start, $job->name );
1054 3 $job->add_process for 1 .. $start;
1055 }
1056 }
1057 5 $_[KERNEL]->delay( monitor_processes => 5 );
1058 }
1059  
1060  
1061 0 ▶sub _daemonize {
Subroutine _daemonize: not called CC=6 SCAR=37.4
1062 0 my $self = shift;
1063 0 ▶ my $logfile = $self->logfile || '/dev/null';
Condition0%0/2
$self->logfile || '/dev/null'
l!l
00
MC/DC0%0/1
$self->logfile || '/dev/null'
$self->logfile
Truth table0%0/2
A
A= $self->logfile
Aresult
11
00
1064 # fallback to /dev/null
1065 0 ▶ $logfile = '/dev/null' unless -w $logfile;
Branch: 1 branch
BranchTrueFalseTotal
unless -w $logfile 0 0 0
1066 0 require POSIX;
1067 0 ▶ fork && exit;
Branch: 1 branch
BranchTrueFalseTotal
fork and exit 0 0 0
1068 ## Detach ourselves from the terminal
1069 0 ▶ croak "Cannot detach from controlling terminal" unless POSIX::setsid();
Branch: 1 branch
BranchTrueFalseTotal
unless POSIX::setsid() 0 0 0
1070 0 ▶ fork && exit;
Branch: 1 branch
BranchTrueFalseTotal
fork and exit 0 0 0
1071 0 umask 0;
1072 0 close(STDIN);
1073 0 close(STDOUT);
1074 0 close(STDERR);
1075 ## Reopen stderr, stdout, stdin to $logfile
1076 0 open(STDIN, "+>$logfile");
1077 0 open(STDOUT, "+>&STDIN");
1078 0 open(STDERR, "+>&STDIN");
1079 0 chdir "/";
1080 }
1081  
1082 28 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
1083  
1084 28 __PACKAGE__->meta->make_immutable;
1085  
1086 =head1 SCRIPT
1087  
1088 There's also a script C<gearman_driver.pl> which is installed with
1089 this distribution. It just instantiates L<Gearman::Driver> with its
1090 default values, having most of the options exposed to the command
1091 line using L<MooseX::Getopt>.
1092  
1093 usage: gearman_driver.pl [long options...]
1094 --loglevel Log level (default: INFO)
1095 --lib Example: --lib ./lib --lib /custom/lib
1096 --server Gearman host[:port][,host[:port]]
1097 --logfile Path to logfile (default: gearman_driver.log)
1098 --console_port Port of management console (default: 47300)
1099 --interval Interval in seconds (see Gearman::Driver::Observer)
1100 --loglayout Log message layout (default: [%d] %p %m%n)
1101 --namespaces Example: --namespaces My::Workers --namespaces My::OtherWorkers
1102 --configfile Read options from this file. Example: --configfile ./etc/gearman-driver-config.yml
1103 --daemonize Run as daemon.
1104  
1105 =head1 AUTHOR
1106  
1107 Johannes Plunien E<lt>plu@cpan.orgE<gt>
1108  
1109 =head1 CONTRIBUTORS
1110  
1111 Uwe Voelker, <uwe.voelker@gmx.de>
1112  
1113 Night Sailer <nightsailer@gmail.com>
1114  
1115 Robert Bohne, <rbo@cpan.org>
1116  
1117 =head1 COPYRIGHT AND LICENSE
1118  
1119 Copyright 2009 by Johannes Plunien
1120  
1121 This library is free software; you can redistribute it and/or modify
1122 it under the same terms as Perl itself.
1123  
1124 =head1 SEE ALSO
1125  
1126 =over 4
1127  
1128 =item * L<Gearman::Driver::Adaptor>
1129  
1130 =item * L<Gearman::Driver::Console>
1131  
1132 =item * L<Gearman::Driver::Console::Basic>
1133  
1134 =item * L<Gearman::Driver::Console::Client>
1135  
1136 =item * L<Gearman::Driver::Job>
1137  
1138 =item * L<Gearman::Driver::Job::Method>
1139  
1140 =item * L<Gearman::Driver::Loader>
1141  
1142 =item * L<Gearman::Driver::Observer>
1143  
1144 =item * L<Gearman::Driver::Worker>
1145  
1146 =item * L<Gearman::XS>
1147  
1148 =item * L<Gearman>
1149  
1150 =item * L<Gearman::Server>
1151  
1152 =item * L<Log::Log4perl>
1153  
1154 =item * L<Module::Find>
1155  
1156 =item * L<Moose>
1157  
1158 =item * L<MooseX::Getopt>
1159  
1160 =item * L<MooseX::Log::Log4perl>
1161  
1162 =item * L<MooseX::MethodAttributes>
1163  
1164 =item * L<Net::Telnet::Gearman>
1165  
1166 =item * L<POE>
1167  
1168 =item * L<http://www.gearman.org/>
1169  
1170 =back
1171  
1172 =head1 REPOSITORY
1173  
1174 L<http://github.com/plu/gearman-driver/>
1175  
1176 =cut
1177  
1178 28 1;
« lib/Gearman/Driver/Console/Client.pm ↑ Summary lib/Gearman/Driver/Worker/AttributeParser.pm »