package Plack::App::Gearman::Status; { $Plack::App::Gearman::Status::VERSION = '0.001001'; } use parent qw(Plack::Component); # ABSTRACT: Plack application to display the status of Gearman job servers use strict; use warnings; use Carp; use MRO::Compat; use Net::Telnet::Gearman; use Text::MicroTemplate; use Try::Tiny; use Plack::Util::Accessor qw(job_servers template connections); chomp(my $template_string = <<'EOTPL'); Gearman Server Status

Gearman Server Status

<% for my $job_server_status (@{$_[0]}) { %>

Job server <%= $job_server_status->{job_server} %>

<% if ($job_server_status->{error}) { %>

<%= $job_server_status->{error} %>

<% } else { %>

Server Version: <%= $job_server_status->{version} %>

Workers

<% for my $worker (@{$job_server_status->{workers}}) { %> <% } %>
File Descriptor IP Address Client ID Functions
<%= $worker->file_descriptor() %> <%= $worker->ip_address() %> <%= $worker->client_id() %> <%= join(', ', sort @{$worker->functions()}) %>

Status

<% for my $status (@{$job_server_status->{status}}) { %> <% } %>
Function Total Running Available Workers Queue
<%= $status->name() %> <%= $status->running() %> <%= $status->busy() %> <%= $status->free() %> <%= $status->queue() %>
<% } %> <% } %> EOTPL sub new { my ($class, @arg) = @_; my $self = $class->next::method(@arg); unless (ref $self->job_servers() eq 'ARRAY') { $self->job_servers(['127.0.0.1:4730']); } $self->connections({}); $self->template(Text::MicroTemplate->new( template => $template_string, tag_start => '<%', tag_end => '%>', line_start => '%', )->build()); return $self; } sub parse_job_server_address { my ($self, $address) = @_; unless (defined $address) { croak("Required job server address parameter not passed"); } $address =~ m{^ # IPv6 address or hostname/IPv4 address (?:\[([\d:]+)\]|([\w.-]+)) # Optional port (?::(\d+))? $}xms or croak("Unable to parse address '$address'"); my $host = $1 || $2; my $port = $3 || 4730; return ($host, $port); } sub connection { my ($self, $address) = @_; my ($host, $port) = $self->parse_job_server_address($address); my $connection; try { $connection = Net::Telnet::Gearman->new( Host => $host, Port => $port, ); } catch { carp $_; }; return $connection; } sub get_status { my ($self) = @_; my @result; for my $job_server (@{$self->job_servers()}) { unless (defined $self->connections()->{$job_server}) { $self->connections()->{$job_server} = $self->connection($job_server); } try { push @result, { job_server => $job_server, workers => [ $self->connections()->{$job_server}->workers() ], status => [ $self->connections()->{$job_server}->status() ], version => $self->connections()->{$job_server}->version(), }; } catch { delete $self->connections()->{$job_server}; push @result, { job_server => $job_server, error => 'Failed to fetch status information from '.$job_server, } }; } return \@result; } sub call { my ($self, $env) = @_; return [ 200, [ 'Content-Type' => 'text/html; charset=utf-8' ], [ $self->template()->($self->get_status()) ] ]; } 1; __END__ =pod =head1 NAME Plack::App::Gearman::Status - Plack application to display the status of Gearman job servers =head1 VERSION version 0.001001 =head1 SYNOPSIS In a C<.psgi> file: use Plack::App::Gearman::Status; my $app = Plack::App::Gearman::Status->new({ job_servers => ['127.0.0.1:4730'], }); As one-liner on the command line: plackup -MPlack::App::Gearman::Status \ -e 'Plack::App::Gearman::Status->new({ job_servers => ["127.0.0.1:4730"] })->to_app' =head1 DESCRIPTION Plack::App::Gearman::Status displays the status of the configured Gearman job servers by fetching it using L and turning it into a simple HTML page. This page contains information about the available workers and the status of the registered functions. =head2 new Constructor, creates new L instance. =head3 Parameters This method expects its parameters as a hash reference. =over =item job_servers Array reference with the addresses of the job servers the application should connect to. =back =head2 parse_job_server_address Parses a job server address of the form C with optional C. If no port is given, it defaults to C<4730>. =head3 Parameters This method expects positional parameters. =over =item address The address to parse. =back =head3 Result A list with host and port. =head2 connection Connects to the given job server and returns the L object. =head3 Parameters This method expects positional parameters. =over =item address Address of the job server to connect to. =back =head3 Result The L instance on success, C otherwise. =head2 get_status Fetch status information from configured Gearman job servers. =head3 Result An array reference with hash references containing status information. =head2 call Specialized call method which retrieves the job server status information and transforms it to HTML. =head3 Result A L response. =head1 SEE ALSO =over =item * L and L. =item * L which is used to access a Gearman job server. =item * C (L) by TOKUHIROM which inspired this application. =back =head1 AUTHOR Manfred Stock =head1 COPYRIGHT AND LICENSE This software is copyright (c) 2012 by Manfred Stock. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. =cut