| 1 | package Gearman::Driver::Observer; | ||||||||||||||||||||||
| 2 | |||||||||||||||||||||||
| 3 | 28 | ▶ | use Moose; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 4 | 28 | ▶ | use Net::Telnet::Gearman; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 5 | 28 | ▶ | use POE; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 6 | |||||||||||||||||||||||
| 7 | =head1 NAME | ||||||||||||||||||||||
| 8 | |||||||||||||||||||||||
| 9 | Gearman::Driver::Observer - Observes Gearman status interface | ||||||||||||||||||||||
| 10 | |||||||||||||||||||||||
| 11 | =head1 DESCRIPTION | ||||||||||||||||||||||
| 12 | |||||||||||||||||||||||
| 13 | Each n seconds L<Net::Telnet::Gearman> is used to fetch status of | ||||||||||||||||||||||
| 14 | free/running/busy workers from the Gearman server. L<Gearman::Driver> | ||||||||||||||||||||||
| 15 | decides to fork more workers depending on the queue size and the | ||||||||||||||||||||||
| 16 | MinProcesses/MaxProcesses attribute of the job methods. | ||||||||||||||||||||||
| 17 | |||||||||||||||||||||||
| 18 | Currently there's no public interface. | ||||||||||||||||||||||
| 19 | |||||||||||||||||||||||
| 20 | =cut | ||||||||||||||||||||||
| 21 | |||||||||||||||||||||||
| 22 | 28 | has 'callback' => ( | |||||||||||||||||||||
| 23 | is => 'rw', | ||||||||||||||||||||||
| 24 | isa => 'CodeRef', | ||||||||||||||||||||||
| 25 | required => 1, | ||||||||||||||||||||||
| 26 | ); | ||||||||||||||||||||||
| 27 | |||||||||||||||||||||||
| 28 | 28 | has 'interval' => ( | |||||||||||||||||||||
| 29 | is => 'rw', | ||||||||||||||||||||||
| 30 | isa => 'Int', | ||||||||||||||||||||||
| 31 | required => 1, | ||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||
| 33 | |||||||||||||||||||||||
| 34 | 28 | has 'server' => ( | |||||||||||||||||||||
| 35 | is => 'rw', | ||||||||||||||||||||||
| 36 | isa => 'Str', | ||||||||||||||||||||||
| 37 | required => 1, | ||||||||||||||||||||||
| 38 | ); | ||||||||||||||||||||||
| 39 | |||||||||||||||||||||||
| 40 | has 'telnet' => ( | ||||||||||||||||||||||
| 41 | auto_deref => 1, | ||||||||||||||||||||||
| 42 | 13 | ▶ | default => sub { [] }, | ||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 43 | 28 | is => 'ro', | |||||||||||||||||||||
| 44 | isa => 'ArrayRef[Net::Telnet::Gearman]', | ||||||||||||||||||||||
| 45 | ); | ||||||||||||||||||||||
| 46 | |||||||||||||||||||||||
| 47 | 28 | has 'session' => ( | |||||||||||||||||||||
| 48 | is => 'ro', | ||||||||||||||||||||||
| 49 | isa => 'POE::Session', | ||||||||||||||||||||||
| 50 | ); | ||||||||||||||||||||||
| 51 | |||||||||||||||||||||||
| 52 | 13 | ▶ | sub BUILD { | ||||||||||||||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||
| 53 | 13 | my ($self) = @_; | |||||||||||||||||||||
| 54 | |||||||||||||||||||||||
| 55 | 13 | $self->_connect(); | |||||||||||||||||||||
| 56 | |||||||||||||||||||||||
| 57 | 13 | $self->{session} = POE::Session->create( | |||||||||||||||||||||
| 58 | object_states => [ | ||||||||||||||||||||||
| 59 | $self => { | ||||||||||||||||||||||
| 60 | _start => '_start', | ||||||||||||||||||||||
| 61 | fetch_status => '_fetch_status' | ||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||
| 63 | ] | ||||||||||||||||||||||
| 64 | ); | ||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||
| 66 | |||||||||||||||||||||||
| 67 | 13 | ▶ | sub _start { | ||||||||||||||||||||
|
Subroutine
_start: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 68 | 13 | $_[KERNEL]->delay( fetch_status => $_[OBJECT]->interval ); | |||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||
| 70 | |||||||||||||||||||||||
| 71 | 13 | ▶ | sub _connect { | ||||||||||||||||||||
|
Subroutine
_connect: called CC=4 SCAR=14.1
| |||||||||||||||||||||||
| 72 | 13 | my ($self) = @_; | |||||||||||||||||||||
| 73 | |||||||||||||||||||||||
| 74 | 13 | $self->{telnet} = []; | |||||||||||||||||||||
| 75 | |||||||||||||||||||||||
| 76 | 13 | foreach my $server ( split /,/, $self->server ) { | |||||||||||||||||||||
| 77 | 13 | my ( $host, $port ) = split /:/, $server; | |||||||||||||||||||||
| 78 | |||||||||||||||||||||||
| 79 | 13 | ▶ | my $telnet = Net::Telnet::Gearman->new( | ||||||||||||||||||||
|
Condition50%2/4
$host || 'localhost'
$port || 4730
MC/DC0%0/1
$host || 'localhost'
$host
MC/DC0%0/1
$port || 4730
$port
Truth table50%1/2
A
A= $host
Truth table50%1/2
A
A= $port
| |||||||||||||||||||||||
| 80 | Host => $host || 'localhost', | ||||||||||||||||||||||
| 81 | Port => $port || 4730, | ||||||||||||||||||||||
| 82 | ); | ||||||||||||||||||||||
| 83 | |||||||||||||||||||||||
| 84 | 13 | push @{ $self->{telnet} }, $telnet; | |||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||
| 87 | |||||||||||||||||||||||
| 88 | 39 | ▶ | sub _fetch_status { | ||||||||||||||||||||
|
Subroutine
_fetch_status: called CC=6 SCAR=19.5
| |||||||||||||||||||||||
| 89 | 39 | my %data = (); | |||||||||||||||||||||
| 90 | 39 | my @error = (); | |||||||||||||||||||||
| 91 | |||||||||||||||||||||||
| 92 | 39 | foreach my $telnet ( $_[OBJECT]->telnet ) { | |||||||||||||||||||||
| 93 | 39 | eval { | |||||||||||||||||||||
| 94 | 39 | my $status = $telnet->status; | |||||||||||||||||||||
| 95 | |||||||||||||||||||||||
| 96 | 39 | foreach my $row (@$status) { | |||||||||||||||||||||
| 97 | 70 | ▶ | $data{ $row->name } ||= { | ||||||||||||||||||||
|
Condition50%1/2
$data{$row->name} ||= {"name", $row->name, "busy", 0, "free", 0, "queue", 0, "running", 0}
MC/DC0%0/1
$data{$row->name} ||= {"name", $row->name, "busy", 0, "free", 0, "queue", 0, "running", 0}
$data{$row->name}
Truth table50%1/2
A
A= $data{$row->name}
| |||||||||||||||||||||||
| 98 | name => $row->name, | ||||||||||||||||||||||
| 99 | busy => 0, | ||||||||||||||||||||||
| 100 | free => 0, | ||||||||||||||||||||||
| 101 | queue => 0, | ||||||||||||||||||||||
| 102 | running => 0, | ||||||||||||||||||||||
| 103 | }; | ||||||||||||||||||||||
| 104 | 70 | $data{ $row->name }{busy} += $row->busy; | |||||||||||||||||||||
| 105 | 70 | $data{ $row->name }{free} += $row->free; | |||||||||||||||||||||
| 106 | 70 | $data{ $row->name }{queue} += $row->queue; | |||||||||||||||||||||
| 107 | 70 | $data{ $row->name }{running} += $row->running; | |||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||
| 109 | }; | ||||||||||||||||||||||
| 110 | |||||||||||||||||||||||
| 111 | # Try to re-open the telnet connection | ||||||||||||||||||||||
| 112 | 39 | ▶ | if ($@) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 113 | 0 | ▶ | push @error, $@ if $@; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 114 | 0 | eval { $telnet->open }; | |||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||
| 117 | |||||||||||||||||||||||
| 118 | 39 | $_[OBJECT]->callback->( { data => [ values %data ], error => \@error } ); | |||||||||||||||||||||
| 119 | |||||||||||||||||||||||
| 120 | 39 | $_[KERNEL]->delay( fetch_status => $_[OBJECT]->interval ); | |||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||
| 122 | |||||||||||||||||||||||
| 123 | 28 | ▶ | no Moose; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 124 | |||||||||||||||||||||||
| 125 | 28 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||
| 126 | |||||||||||||||||||||||
| 127 | =head1 AUTHOR | ||||||||||||||||||||||
| 128 | |||||||||||||||||||||||
| 129 | See L<Gearman::Driver>. | ||||||||||||||||||||||
| 130 | |||||||||||||||||||||||
| 131 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||
| 132 | |||||||||||||||||||||||
| 133 | See L<Gearman::Driver>. | ||||||||||||||||||||||
| 134 | |||||||||||||||||||||||
| 135 | =head1 SEE ALSO | ||||||||||||||||||||||
| 136 | |||||||||||||||||||||||
| 137 | =over 4 | ||||||||||||||||||||||
| 138 | |||||||||||||||||||||||
| 139 | =item * L<Gearman::Driver> | ||||||||||||||||||||||
| 140 | |||||||||||||||||||||||
| 141 | =item * L<Gearman::Driver::Adaptor> | ||||||||||||||||||||||
| 142 | |||||||||||||||||||||||
| 143 | =item * L<Gearman::Driver::Console> | ||||||||||||||||||||||
| 144 | |||||||||||||||||||||||
| 145 | =item * L<Gearman::Driver::Console::Basic> | ||||||||||||||||||||||
| 146 | |||||||||||||||||||||||
| 147 | =item * L<Gearman::Driver::Console::Client> | ||||||||||||||||||||||
| 148 | |||||||||||||||||||||||
| 149 | =item * L<Gearman::Driver::Job> | ||||||||||||||||||||||
| 150 | |||||||||||||||||||||||
| 151 | =item * L<Gearman::Driver::Job::Method> | ||||||||||||||||||||||
| 152 | |||||||||||||||||||||||
| 153 | =item * L<Gearman::Driver::Loader> | ||||||||||||||||||||||
| 154 | |||||||||||||||||||||||
| 155 | =item * L<Gearman::Driver::Worker> | ||||||||||||||||||||||
| 156 | |||||||||||||||||||||||
| 157 | =back | ||||||||||||||||||||||
| 158 | |||||||||||||||||||||||
| 159 | =cut | ||||||||||||||||||||||
| 160 | |||||||||||||||||||||||
| 161 | 28 | 1; | |||||||||||||||||||||