5 use lib '/srv/MojoX-Gearman/lib';
8 use Data::Dump qw(dump);
10 # Documentation browser under "/perldoc" (this plugin requires Perl 5.10)
11 plugin 'pod_renderer';
15 $self->render('index');
18 get '/ping_http' => sub {
20 my $gearman = $self->client;
21 my $pong = $gearman->post( "http://localhost:4780/ping" => { Connection => 'close' } => "some data" )->res->body;
23 $self->render( 'ping', pong => $pong );
26 my $gearman = MojoX::Gearman->new; #( ioloop => Mojo::IOLoop->singleton );
27 $gearman->server( $ENV{GEARMAN} || 'localhost:4730' );
29 get '/ping_g' => sub {
31 my $pong = $gearman->req( 'SUBMIT_JOB', 'ping', '', "some data 2" );
33 $self->render( 'ping', pong => $pong );
37 my ( $self, $json ) = @_;
38 #my $data = $self->render( json => $json, partial => 1 );
40 if ( my $callback = $self->param('callback') ) {
41 $data = "$callback($data)";
43 $self->render( data => $data, format => 'js' );
47 get '/g/:call/:args' => [ args => qr/.*/ ] => sub {
49 my $ret = $gearman->req( 'SUBMIT_JOB', $self->param('call'), '', $self->param('args') );
50 warn $self->param('call'), " = ", dump($ret), "\n";
51 die "no result for ", $self->param('call'), ' args: ', $self->param('args') unless defined $ret;
52 _render_jsonp( $self, $ret );
55 get '/_g/status' => sub {
58 my $ret = $gearman->req( 'TEXT' => 'status' );
59 warn "# status:\n$ret";
61 my @c = qw(function total running available);
64 foreach my $l ( split(/\n/,$ret) ) {
65 my @v = split(/\t/, $l);
67 $h->{$c[$_]} = $v[$_] foreach 0 .. $#v;
70 warn "## ", dump $status;
71 _render_jsonp( $self, Mojo::JSON->new->encode({ status => $status }) );
74 get '/gnuplot' => sub {
77 my $sql = $self->param('sql') || die "sql required";
79 my $ret = $gearman->req( 'SUBMIT_JOB', 'Store_sql', '', $sql );
80 die "no result for $sql" unless $ret;
82 $ret = Mojo::JSON->new->decode( $ret );
84 my $dir = $self->app->home->rel_dir('public');
90 warn "# $sql -> $name";
92 mkdir "$dir/gnuplot" unless -e "$dir/gnuplot";
93 my $png = "$dir/gnuplot/$name"; # FIXME
95 open(my $gnuplot, '|-', 'gnuplot') || die "gnuplot: $!";
96 open($gnuplot, '>', '/tmp/gnuplot') if $self->param('debug');
98 my @c = @{ $ret->{columns} };
99 warn "first column not timestamp" unless shift @c eq 'timestamp';
106 set timefmt "%Y-%m-%d %H:%M:%S"
107 set format x "%H%M%S"
109 plot '-' using 1:3 title "$c[0]"
111 foreach my $row ( @{ $ret->{rows} } ) {
112 print $gnuplot join("\t", @$row),"\n";
114 print $gnuplot "e\n";
117 $self->redirect_to("/gnuplot/$name");
125 % title 'Gearman demo';
128 <li><a href="/gearman.html#ping/127.0.0.1">Gearman</a> web interface
129 <li><%= link_to 'CRM' => 'CRM.html' %> search with tabular output
135 <li><a href="/gnuplot?sql=select timestamp,rtt from ping where ip << inet '10.17/16' order by timestamp desc limit 1000">ttl from 10.17 network</a>
140 <%= link_to 'HTTP ping' => 'ping_http' %>
141 <%= link_to 'Gearman ping' => 'ping_g' %>
145 pong: <tt><%= $pong %>
149 <pre><%= $dump %></pre>
151 @@ layouts/default.html.ep
152 <!doctype html><html>
153 <head><title><%= title %></title></head>
154 <body><%= content %></body>