| 1 | package Gearman::Driver::Console; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 28 | ▶ | use Moose; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 28 | ▶ | use POE qw(Component::Server::TCP); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 28 | ▶ | use Module::Find; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 28 | ▶ | use Moose::Util qw(apply_all_roles); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 28 | ▶ | use Try::Tiny; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | =head1 NAME | ||||||||||||||
| 10 | |||||||||||||||
| 11 | Gearman::Driver::Console - Management console | ||||||||||||||
| 12 | |||||||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||||||
| 14 | |||||||||||||||
| 15 | $ ~/Gearman-Driver$ gearman_driver.pl --console_port 12345 & | ||||||||||||||
| 16 | [1] 32890 | ||||||||||||||
| 17 | $ ~/Gearman-Driver$ telnet localhost 12345 | ||||||||||||||
| 18 | Trying ::1... | ||||||||||||||
| 19 | telnet: connect to address ::1: Connection refused | ||||||||||||||
| 20 | Trying fe80::1... | ||||||||||||||
| 21 | telnet: connect to address fe80::1: Connection refused | ||||||||||||||
| 22 | Trying 127.0.0.1... | ||||||||||||||
| 23 | Connected to localhost. | ||||||||||||||
| 24 | Escape character is '^]'. | ||||||||||||||
| 25 | status | ||||||||||||||
| 26 | GDExamples::Convert::convert_to_jpeg 0 5 0 2010-01-29T20:37:17 1970-01-01T00:00:00 | ||||||||||||||
| 27 | GDExamples::Convert::convert_to_gif 0 5 0 2010-01-29T20:37:12 2010-01-29T20:37:12 some error | ||||||||||||||
| 28 | . | ||||||||||||||
| 29 | |||||||||||||||
| 30 | =head1 DESCRIPTION | ||||||||||||||
| 31 | |||||||||||||||
| 32 | By default L<Gearman::Driver> provides a management console which can | ||||||||||||||
| 33 | be used with a standard telnet client. It's possible to list all | ||||||||||||||
| 34 | running worker processes as well as changing min/max processes | ||||||||||||||
| 35 | on runtime. | ||||||||||||||
| 36 | |||||||||||||||
| 37 | Each successful L<command|/COMMANDS> ends with a dot. If a | ||||||||||||||
| 38 | command throws an error, a line starting with 'ERR' will be | ||||||||||||||
| 39 | returned. | ||||||||||||||
| 40 | |||||||||||||||
| 41 | =cut | ||||||||||||||
| 42 | |||||||||||||||
| 43 | 28 | has 'port' => ( | |||||||||||||
| 44 | is => 'rw', | ||||||||||||||
| 45 | isa => 'Int', | ||||||||||||||
| 46 | required => 1, | ||||||||||||||
| 47 | ); | ||||||||||||||
| 48 | |||||||||||||||
| 49 | 28 | has 'server' => ( | |||||||||||||
| 50 | is => 'ro', | ||||||||||||||
| 51 | isa => 'POE::Component::Server::TCP', | ||||||||||||||
| 52 | ); | ||||||||||||||
| 53 | |||||||||||||||
| 54 | 28 | has 'driver' => ( | |||||||||||||
| 55 | handles => { log => 'log' }, | ||||||||||||||
| 56 | is => 'rw', | ||||||||||||||
| 57 | isa => 'Gearman::Driver', | ||||||||||||||
| 58 | required => 1, | ||||||||||||||
| 59 | weak_ref => 1, | ||||||||||||||
| 60 | ); | ||||||||||||||
| 61 | |||||||||||||||
| 62 | 13 | ▶ | sub BUILD { | ||||||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 63 | 13 | my ($self) = @_; | |||||||||||||
| 64 | |||||||||||||||
| 65 | 13 | my @commands = grep $_ ne 'Gearman::Driver::Console::Client', findallmod Gearman::Driver::Console; | |||||||||||||
| 66 | 13 | apply_all_roles( $self => @commands ); | |||||||||||||
| 67 | |||||||||||||||
| 68 | $self->{server} = POE::Component::Server::TCP->new( | ||||||||||||||
| 69 | Alias => "server", | ||||||||||||||
| 70 | Port => $self->port, | ||||||||||||||
| 71 | 24 | ▶ | ClientError => sub { }, | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 72 | 40 | ▶ | ClientInput => sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.1
| |||||||||||||||
| 73 | 40 | my ( $session, $heap, $input ) = @_[ SESSION, HEAP, ARG0 ]; | |||||||||||||
| 74 | 40 | my ( $command, @params ) = split /\s+/, $input; | |||||||||||||
| 75 | 40 | ▶ | $command ||= ''; | ||||||||||||
|
Condition50%1/2
$command ||= ""
MC/DC0%0/1
$command ||= ""
$command
Truth table50%1/2
A
A= $command
| |||||||||||||||
| 76 | |||||||||||||||
| 77 | 40 | ▶ | if ( $self->can($command) ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 78 | 39 | ▶ | try { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 79 | 39 | my @result = $self->$command(@params); | |||||||||||||
| 80 | 28 | ▶ | try { | ||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||
| 81 | 28 | $heap->{client}->put($_) for @result; | |||||||||||||
| 82 | 28 | }; | |||||||||||||
| 83 | } | ||||||||||||||
| 84 | 11 | ▶ | catch { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 85 | 11 | chomp($_); | |||||||||||||
| 86 | 11 | ▶ | try { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 87 | 11 | $heap->{client}->put($_); | |||||||||||||
| 88 | 11 | }; | |||||||||||||
| 89 | 39 | }; | |||||||||||||
| 90 | } | ||||||||||||||
| 91 | |||||||||||||||
| 92 | elsif ( $command eq 'quit' ) { | ||||||||||||||
| 93 | 0 | delete $heap->{client}; | |||||||||||||
| 94 | 0 | return; | |||||||||||||
| 95 | } | ||||||||||||||
| 96 | |||||||||||||||
| 97 | else { | ||||||||||||||
| 98 | 1 | ▶ | try { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 99 | 1 | $heap->{client}->put("ERR unknown_command: $command"); | |||||||||||||
| 100 | 1 | }; | |||||||||||||
| 101 | } | ||||||||||||||
| 102 | |||||||||||||||
| 103 | 40 | ▶ | try { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 104 | 40 | $heap->{client}->put('.'); | |||||||||||||
| 105 | 40 | }; | |||||||||||||
| 106 | } | ||||||||||||||
| 107 | 13 | ); | |||||||||||||
| 108 | } | ||||||||||||||
| 109 | |||||||||||||||
| 110 | =head1 COMMANDS | ||||||||||||||
| 111 | |||||||||||||||
| 112 | All basic commands are implemented in | ||||||||||||||
| 113 | L<Gearman::Driver::Console::Basic>. It's very easy to extend this | ||||||||||||||
| 114 | console with new commands. Every module found in namespace | ||||||||||||||
| 115 | C<Gearman::Driver::Console::*> will be loaded. Each of those | ||||||||||||||
| 116 | modules has to be implemented as a L<Moose::Role>. You've got | ||||||||||||||
| 117 | access to two attributes/methods there: | ||||||||||||||
| 118 | |||||||||||||||
| 119 | =over 4 | ||||||||||||||
| 120 | |||||||||||||||
| 121 | =item * C<driver> - reference to the L<Gearman::Driver> object | ||||||||||||||
| 122 | |||||||||||||||
| 123 | =item * C<server> - reference to the L<POE::Component::TCP::Server> object | ||||||||||||||
| 124 | |||||||||||||||
| 125 | =item * C<get_job($name)> - returns a L<Gearman::Driver::Job> object | ||||||||||||||
| 126 | |||||||||||||||
| 127 | =back | ||||||||||||||
| 128 | |||||||||||||||
| 129 | So a new command could look like: | ||||||||||||||
| 130 | |||||||||||||||
| 131 | package Gearman::Driver::Console::List; | ||||||||||||||
| 132 | |||||||||||||||
| 133 | use Moose::Role; | ||||||||||||||
| 134 | |||||||||||||||
| 135 | sub list { | ||||||||||||||
| 136 | my ($self) = @_; | ||||||||||||||
| 137 | my @result = (); | ||||||||||||||
| 138 | foreach my $job ( $self->driver->get_jobs ) { | ||||||||||||||
| 139 | push @result, $job->name; | ||||||||||||||
| 140 | } | ||||||||||||||
| 141 | return @result; | ||||||||||||||
| 142 | } | ||||||||||||||
| 143 | |||||||||||||||
| 144 | 1; | ||||||||||||||
| 145 | |||||||||||||||
| 146 | If you need to throw an error, just die and everything will work as | ||||||||||||||
| 147 | expected (as long as you do not forget the C<\n>): | ||||||||||||||
| 148 | |||||||||||||||
| 149 | package Gearman::Driver::Console::Broken; | ||||||||||||||
| 150 | |||||||||||||||
| 151 | use Moose::Role; | ||||||||||||||
| 152 | |||||||||||||||
| 153 | sub broken { | ||||||||||||||
| 154 | my ($self) = @_; | ||||||||||||||
| 155 | die "ERR this is a broken command\n"; | ||||||||||||||
| 156 | } | ||||||||||||||
| 157 | |||||||||||||||
| 158 | sub get_max_processes { | ||||||||||||||
| 159 | my ( $self, $job_name ) = @_; | ||||||||||||||
| 160 | my $job = $self->get_job($job_name); # this automatically dies if job is not found | ||||||||||||||
| 161 | return $job->max_processes; | ||||||||||||||
| 162 | } | ||||||||||||||
| 163 | |||||||||||||||
| 164 | 1; | ||||||||||||||
| 165 | |||||||||||||||
| 166 | Yes, that's all... | ||||||||||||||
| 167 | |||||||||||||||
| 168 | $ ~/Gearman-Driver$ telnet localhost 47300 | ||||||||||||||
| 169 | Trying ::1... | ||||||||||||||
| 170 | telnet: connect to address ::1: Connection refused | ||||||||||||||
| 171 | Trying fe80::1... | ||||||||||||||
| 172 | telnet: connect to address fe80::1: Connection refused | ||||||||||||||
| 173 | Trying 127.0.0.1... | ||||||||||||||
| 174 | Connected to localhost. | ||||||||||||||
| 175 | Escape character is '^]'. | ||||||||||||||
| 176 | list | ||||||||||||||
| 177 | GDExamples::Convert::convert_to_jpeg | ||||||||||||||
| 178 | GDExamples::Convert::convert_to_gif | ||||||||||||||
| 179 | . | ||||||||||||||
| 180 | broken | ||||||||||||||
| 181 | ERR this is a broken command | ||||||||||||||
| 182 | get_max_processes asdf | ||||||||||||||
| 183 | ERR invalid_job_name: asdf | ||||||||||||||
| 184 | get_max_processes GDExamples::Convert::convert_to_jpeg | ||||||||||||||
| 185 | 6 | ||||||||||||||
| 186 | . | ||||||||||||||
| 187 | |||||||||||||||
| 188 | =cut | ||||||||||||||
| 189 | |||||||||||||||
| 190 | 22 | ▶ | sub get_job { | ||||||||||||
|
Subroutine
get_job: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 191 | 22 | my ( $self, $job_name ) = @_; | |||||||||||||
| 192 | 22 | ▶ | return $self->driver->get_job($job_name) || die "ERR invalid_job_name: $job_name\n"; | ||||||||||||
|
Condition100%2/2
$self->driver->get_job($job_name) || die("ERR invalid_job_name: $job_name\n")
MC/DC100%1/1
$self->driver->get_job($job_name) || die("ERR invalid_job_name: $job_name\n")
$self->driver->get_job($job_name)
Truth table100%2/2
A
A= $self->driver->get_job($job_name)
| |||||||||||||||
| 193 | } | ||||||||||||||
| 194 | |||||||||||||||
| 195 | 28 | ▶ | no Moose; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 196 | |||||||||||||||
| 197 | 28 | __PACKAGE__->meta->make_immutable; | |||||||||||||
| 198 | |||||||||||||||
| 199 | =head1 AUTHOR | ||||||||||||||
| 200 | |||||||||||||||
| 201 | See L<Gearman::Driver>. | ||||||||||||||
| 202 | |||||||||||||||
| 203 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||
| 204 | |||||||||||||||
| 205 | See L<Gearman::Driver>. | ||||||||||||||
| 206 | |||||||||||||||
| 207 | =head1 SEE ALSO | ||||||||||||||
| 208 | |||||||||||||||
| 209 | =over 4 | ||||||||||||||
| 210 | |||||||||||||||
| 211 | =item * L<Gearman::Driver> | ||||||||||||||
| 212 | |||||||||||||||
| 213 | =item * L<Gearman::Driver::Adaptor> | ||||||||||||||
| 214 | |||||||||||||||
| 215 | =item * L<Gearman::Driver::Console::Basic> | ||||||||||||||
| 216 | |||||||||||||||
| 217 | =item * L<Gearman::Driver::Console::Client> | ||||||||||||||
| 218 | |||||||||||||||
| 219 | =item * L<Gearman::Driver::Job> | ||||||||||||||
| 220 | |||||||||||||||
| 221 | =item * L<Gearman::Driver::Job::Method> | ||||||||||||||
| 222 | |||||||||||||||
| 223 | =item * L<Gearman::Driver::Loader> | ||||||||||||||
| 224 | |||||||||||||||
| 225 | =item * L<Gearman::Driver::Observer> | ||||||||||||||
| 226 | |||||||||||||||
| 227 | =item * L<Gearman::Driver::Worker> | ||||||||||||||
| 228 | |||||||||||||||
| 229 | =back | ||||||||||||||
| 230 | |||||||||||||||
| 231 | =cut | ||||||||||||||
| 232 | |||||||||||||||
| 233 | 28 | 1; | |||||||||||||